1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

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()
->addStmt($factory->method('someMethod')
->makePublic()
->makeAbstract() // ->makeFinal()
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
->setDocComment('/**

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -547,7 +547,7 @@ class Standard extends PrettyPrinterAbstract
}
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) {

View File

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

View File

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

View File

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