1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 12:46:47 +01:00

Use "public" visibility when not explicitly given

Fixes issue #143
This commit is contained in:
nikic 2014-11-13 20:18:49 +01:00
parent a8ffc6fcfc
commit 0f69f12b94
6 changed files with 107 additions and 15 deletions

View File

@ -27,9 +27,15 @@ class ClassMethod extends Node\Stmt
* @param array $attributes Additional attributes * @param array $attributes Additional attributes
*/ */
public function __construct($name, array $subNodes = array(), array $attributes = array()) { public function __construct($name, array $subNodes = array(), array $attributes = array()) {
$type = isset($subNodes['type']) ? $subNodes['type'] : 0;
if (0 === ($type & Class_::VISIBILITY_MODIFER_MASK)) {
// If no visibility modifier given, PHP defaults to public
$type |= Class_::MODIFIER_PUBLIC;
}
parent::__construct( parent::__construct(
array( array(
'type' => isset($subNodes['type']) ? $subNodes['type'] : Class_::MODIFIER_PUBLIC, 'type' => $type,
'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false, 'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'name' => $name, 'name' => $name,
'params' => isset($subNodes['params']) ? $subNodes['params'] : array(), 'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),

View File

@ -21,6 +21,8 @@ class Class_ extends Node\Stmt
const MODIFIER_ABSTRACT = 16; const MODIFIER_ABSTRACT = 16;
const MODIFIER_FINAL = 32; const MODIFIER_FINAL = 32;
const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4
protected static $specialNames = array( protected static $specialNames = array(
'self' => true, 'self' => true,
'parent' => true, 'parent' => true,
@ -87,7 +89,7 @@ class Class_ extends Node\Stmt
* @internal * @internal
*/ */
public static function verifyModifier($a, $b) { public static function verifyModifier($a, $b) {
if ($a & 7 && $b & 7) { if ($a & self::VISIBILITY_MODIFER_MASK && $b & self::VISIBILITY_MODIFER_MASK) {
throw new Error('Multiple access type modifiers are not allowed'); throw new Error('Multiple access type modifiers are not allowed');
} }

View File

@ -18,6 +18,11 @@ class Property extends Node\Stmt
* @param array $attributes Additional attributes * @param array $attributes Additional attributes
*/ */
public function __construct($type, array $props, array $attributes = array()) { public function __construct($type, array $props, array $attributes = array()) {
if (0 === ($type & Class_::VISIBILITY_MODIFER_MASK)) {
// If no visibility modifier given, PHP defaults to public
$type |= Class_::MODIFIER_PUBLIC;
}
parent::__construct( parent::__construct(
array( array(
'type' => $type, 'type' => $type,
@ -42,4 +47,4 @@ class Property extends Node\Stmt
public function isStatic() { public function isStatic() {
return (bool) ($this->type & Class_::MODIFIER_STATIC); return (bool) ($this->type & Class_::MODIFIER_STATIC);
} }
} }

View File

@ -15,13 +15,15 @@ class ClassMethodTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($node->{'is' . $modifier}()); $this->assertTrue($node->{'is' . $modifier}());
} }
/** public function testNoModifiers() {
* @dataProvider provideModifiers
*/
public function testNoModifiers($modifier) {
$node = new ClassMethod('foo', array('type' => 0)); $node = new ClassMethod('foo', array('type' => 0));
$this->assertFalse($node->{'is' . $modifier}()); $this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertFalse($node->isAbstract());
$this->assertFalse($node->isFinal());
$this->assertFalse($node->isStatic());
} }
public function provideModifiers() { public function provideModifiers() {
@ -34,4 +36,4 @@ class ClassMethodTest extends \PHPUnit_Framework_TestCase
array('static'), array('static'),
); );
} }
} }

View File

@ -16,13 +16,13 @@ class PropertyTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($node->{'is' . $modifier}()); $this->assertTrue($node->{'is' . $modifier}());
} }
/** public function testNoModifiers() {
* @dataProvider provideModifiers
*/
public function testNoModifiers($modifier) {
$node = new Property(0, array()); $node = new Property(0, array());
$this->assertFalse($node->{'is' . $modifier}()); $this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertFalse($node->isStatic());
} }
public function provideModifiers() { public function provideModifiers() {
@ -33,4 +33,4 @@ class PropertyTest extends \PHPUnit_Framework_TestCase
array('static'), array('static'),
); );
} }
} }

View File

@ -0,0 +1,77 @@
Implicitly public properties and methods
-----
<?php
abstract class A {
var $a;
static $b;
abstract function c();
final function d() {}
static function e() {}
final static function f() {}
}
-----
array(
0: Stmt_Class(
type: 16
name: A
extends: null
implements: array(
)
stmts: array(
0: Stmt_Property(
type: 1
props: array(
0: Stmt_PropertyProperty(
name: a
default: null
)
)
)
1: Stmt_Property(
type: 9
props: array(
0: Stmt_PropertyProperty(
name: b
default: null
)
)
)
2: Stmt_ClassMethod(
type: 17
byRef: false
name: c
params: array(
)
stmts: null
)
3: Stmt_ClassMethod(
type: 33
byRef: false
name: d
params: array(
)
stmts: array(
)
)
4: Stmt_ClassMethod(
type: 9
byRef: false
name: e
params: array(
)
stmts: array(
)
)
5: Stmt_ClassMethod(
type: 41
byRef: false
name: f
params: array(
)
stmts: array(
)
)
)
)
)