Implicit public should not generate as explicit public

This commit is contained in:
Thomas Ruiz 2015-03-02 10:33:41 +00:00 committed by Nikita Popov
parent c96636d192
commit 73cace360d
12 changed files with 86 additions and 28 deletions

View File

@ -18,6 +18,7 @@ $node = $factory->namespace('Name\Space')
->makeAbstract() // ->makeFinal() ->makeAbstract() // ->makeFinal()
->addStmt($factory->method('someMethod') ->addStmt($factory->method('someMethod')
->makePublic()
->makeAbstract() // ->makeFinal() ->makeAbstract() // ->makeFinal()
->addParam($factory->param('someParam')->setTypeHint('SomeClass')) ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
->setDocComment('/** ->setDocComment('/**

View File

@ -480,11 +480,11 @@ method_body:
variable_modifiers: variable_modifiers:
non_empty_member_modifiers { $$ = $1; } non_empty_member_modifiers { $$ = $1; }
| T_VAR { $$ = Stmt\Class_::MODIFIER_PUBLIC; } | T_VAR { $$ = 0; }
; ;
method_modifiers: method_modifiers:
/* empty */ { $$ = Stmt\Class_::MODIFIER_PUBLIC; } /* empty */ { $$ = 0; }
| non_empty_member_modifiers { $$ = $1; } | non_empty_member_modifiers { $$ = $1; }
; ;

View File

@ -116,10 +116,10 @@ class Method extends FunctionLike
*/ */
public function getNode() { public function getNode() {
return new Stmt\ClassMethod($this->name, array( return new Stmt\ClassMethod($this->name, array(
'type' => $this->type !== 0 ? $this->type : Stmt\Class_::MODIFIER_PUBLIC, 'type' => $this->type,
'byRef' => $this->returnByRef, 'byRef' => $this->returnByRef,
'params' => $this->params, 'params' => $this->params,
'stmts' => $this->stmts, 'stmts' => $this->stmts,
), $this->attributes); ), $this->attributes);
} }
} }

View File

@ -28,10 +28,6 @@ class ClassMethod extends Node\Stmt
*/ */
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; $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(
@ -57,7 +53,7 @@ class ClassMethod extends Node\Stmt
} }
public function isPublic() { public function isPublic() {
return (bool) ($this->type & Class_::MODIFIER_PUBLIC); return ($this->type & Class_::MODIFIER_PUBLIC) !== 0 || $this->type === 0;
} }
public function isProtected() { public function isProtected() {

View File

@ -19,11 +19,6 @@ 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;
}
if ($type & Class_::MODIFIER_ABSTRACT) { if ($type & Class_::MODIFIER_ABSTRACT) {
throw new Error('Properties cannot be declared abstract'); throw new Error('Properties cannot be declared abstract');
} }
@ -42,7 +37,7 @@ class Property extends Node\Stmt
} }
public function isPublic() { public function isPublic() {
return (bool) ($this->type & Class_::MODIFIER_PUBLIC); return ($this->type & Class_::MODIFIER_PUBLIC) !== 0 || $this->type === 0;
} }
public function isProtected() { public function isProtected() {

View File

@ -1673,11 +1673,11 @@ class Parser extends ParserAbstract
} }
protected function reduceRule168($attributes) { protected function reduceRule168($attributes) {
$this->semValue = Node\Stmt\Class_::MODIFIER_PUBLIC; $this->semValue = 0;
} }
protected function reduceRule169($attributes) { protected function reduceRule169($attributes) {
$this->semValue = Node\Stmt\Class_::MODIFIER_PUBLIC; $this->semValue = 0;
} }
protected function reduceRule170($attributes) { protected function reduceRule170($attributes) {

View File

@ -547,7 +547,7 @@ class Standard extends PrettyPrinterAbstract
} }
public function pStmt_Property(Stmt\Property $node) { public function pStmt_Property(Stmt\Property $node) {
return $this->pModifiers($node->type) . $this->pCommaSeparated($node->props) . ';'; return (0 === $node->type ? 'var ' : $this->pModifiers($node->type)) . $this->pCommaSeparated($node->props) . ';';
} }
public function pStmt_PropertyProperty(Stmt\PropertyProperty $node) { public function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {

View File

@ -151,4 +151,4 @@ class MethodTest extends \PHPUnit_Framework_TestCase
->addParam(new Node\Name('foo')) ->addParam(new Node\Name('foo'))
; ;
} }
} }

View File

@ -36,7 +36,10 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
->implement('A\Few', '\Interfaces') ->implement('A\Few', '\Interfaces')
->makeAbstract() ->makeAbstract()
->addStmt($factory->method('firstMethod'))
->addStmt($factory->method('someMethod') ->addStmt($factory->method('someMethod')
->makePublic()
->makeAbstract() ->makeAbstract()
->addParam($factory->param('someParam')->setTypeHint('SomeClass')) ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
->setDocComment('/** ->setDocComment('/**
@ -66,6 +69,9 @@ abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
{ {
protected $someProperty; protected $someProperty;
private $anotherProperty = array(1, 2, 3); private $anotherProperty = array(1, 2, 3);
function firstMethod()
{
}
/** /**
* This method does something. * This method does something.
* *

View File

@ -9,6 +9,7 @@ abstract class A {
final function d() {} final function d() {}
static function e() {} static function e() {}
final static function f() {} final static function f() {}
function g() {}
} }
----- -----
array( array(
@ -20,7 +21,7 @@ array(
) )
stmts: array( stmts: array(
0: Stmt_Property( 0: Stmt_Property(
type: 1 type: 0
props: array( props: array(
0: Stmt_PropertyProperty( 0: Stmt_PropertyProperty(
name: a name: a
@ -29,7 +30,7 @@ array(
) )
) )
1: Stmt_Property( 1: Stmt_Property(
type: 9 type: 8
props: array( props: array(
0: Stmt_PropertyProperty( 0: Stmt_PropertyProperty(
name: b name: b
@ -38,7 +39,7 @@ array(
) )
) )
2: Stmt_ClassMethod( 2: Stmt_ClassMethod(
type: 17 type: 16
byRef: false byRef: false
name: c name: c
params: array( params: array(
@ -46,7 +47,7 @@ array(
stmts: null stmts: null
) )
3: Stmt_ClassMethod( 3: Stmt_ClassMethod(
type: 33 type: 32
byRef: false byRef: false
name: d name: d
params: array( params: array(
@ -55,7 +56,7 @@ array(
) )
) )
4: Stmt_ClassMethod( 4: Stmt_ClassMethod(
type: 9 type: 8
byRef: false byRef: false
name: e name: e
params: array( params: array(
@ -64,7 +65,7 @@ array(
) )
) )
5: Stmt_ClassMethod( 5: Stmt_ClassMethod(
type: 41 type: 40
byRef: false byRef: false
name: f name: f
params: array( params: array(
@ -72,6 +73,15 @@ array(
stmts: array( stmts: array(
) )
) )
6: Stmt_ClassMethod(
type: 0
byRef: false
name: g
params: array(
)
stmts: array(
)
)
) )
) )
) )

View File

@ -5,6 +5,7 @@ PHP 4 style declarations
class A { class A {
var $foo; var $foo;
function bar() {} function bar() {}
static abstract function baz() {}
} }
----- -----
array( array(
@ -16,7 +17,7 @@ array(
) )
stmts: array( stmts: array(
0: Stmt_Property( 0: Stmt_Property(
type: 1 type: 0
props: array( props: array(
0: Stmt_PropertyProperty( 0: Stmt_PropertyProperty(
name: foo name: foo
@ -25,7 +26,7 @@ array(
) )
) )
1: Stmt_ClassMethod( 1: Stmt_ClassMethod(
type: 1 type: 0
byRef: false byRef: false
name: bar name: bar
params: array( params: array(
@ -33,6 +34,15 @@ array(
stmts: array( stmts: array(
) )
) )
2: Stmt_ClassMethod(
type: 24
byRef: false
name: baz
params: array(
)
stmts: array(
)
)
) )
) )
) )

View File

@ -0,0 +1,40 @@
Class
-----
<?php
class Foo
{
var $a = 'foo';
private $b = 'bar';
static $c = 'baz';
function test()
{
$this->a = 'bar';
echo 'test';
}
protected function baz() {}
public function foo() {}
abstract static function bar() {}
}
-----
class Foo
{
var $a = 'foo';
private $b = 'bar';
static $c = 'baz';
function test()
{
$this->a = 'bar';
echo 'test';
}
protected function baz()
{
}
public function foo()
{
}
static abstract function bar()
{
}
}