diff --git a/doc/4_Code_generation.markdown b/doc/4_Code_generation.markdown index fbf8b40..7b05a83 100644 --- a/doc/4_Code_generation.markdown +++ b/doc/4_Code_generation.markdown @@ -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('/** diff --git a/grammar/zend_language_parser.phpy b/grammar/zend_language_parser.phpy index 7daa77f..9267e04 100644 --- a/grammar/zend_language_parser.phpy +++ b/grammar/zend_language_parser.phpy @@ -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; } ; diff --git a/lib/PhpParser/Builder/Method.php b/lib/PhpParser/Builder/Method.php index b3ab60d..60dd4b9 100644 --- a/lib/PhpParser/Builder/Method.php +++ b/lib/PhpParser/Builder/Method.php @@ -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); } -} \ No newline at end of file +} diff --git a/lib/PhpParser/Node/Stmt/ClassMethod.php b/lib/PhpParser/Node/Stmt/ClassMethod.php index 8bbc9e6..2fec923 100644 --- a/lib/PhpParser/Node/Stmt/ClassMethod.php +++ b/lib/PhpParser/Node/Stmt/ClassMethod.php @@ -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() { diff --git a/lib/PhpParser/Node/Stmt/Property.php b/lib/PhpParser/Node/Stmt/Property.php index 7ed034b..c73c6fe 100644 --- a/lib/PhpParser/Node/Stmt/Property.php +++ b/lib/PhpParser/Node/Stmt/Property.php @@ -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() { diff --git a/lib/PhpParser/Parser.php b/lib/PhpParser/Parser.php index c4b0d31..36e84dd 100644 --- a/lib/PhpParser/Parser.php +++ b/lib/PhpParser/Parser.php @@ -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) { diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index 0c4e10f..73f5030 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -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) { diff --git a/test/PhpParser/Builder/MethodTest.php b/test/PhpParser/Builder/MethodTest.php index a409087..4de1085 100644 --- a/test/PhpParser/Builder/MethodTest.php +++ b/test/PhpParser/Builder/MethodTest.php @@ -151,4 +151,4 @@ class MethodTest extends \PHPUnit_Framework_TestCase ->addParam(new Node\Name('foo')) ; } -} \ No newline at end of file +} diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 8441840..7915eb6 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -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. * diff --git a/test/code/parser/stmt/class/implicitPublic.test b/test/code/parser/stmt/class/implicitPublic.test index 70a90d2..06b0eba 100644 --- a/test/code/parser/stmt/class/implicitPublic.test +++ b/test/code/parser/stmt/class/implicitPublic.test @@ -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( + ) + ) ) ) ) diff --git a/test/code/parser/stmt/class/php4Style.test b/test/code/parser/stmt/class/php4Style.test index f694df2..bd75398 100644 --- a/test/code/parser/stmt/class/php4Style.test +++ b/test/code/parser/stmt/class/php4Style.test @@ -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( + ) + ) ) ) -) \ No newline at end of file +) diff --git a/test/code/prettyPrinter/class.test b/test/code/prettyPrinter/class.test new file mode 100644 index 0000000..a50799f --- /dev/null +++ b/test/code/prettyPrinter/class.test @@ -0,0 +1,40 @@ +Class +----- +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() + { + } +} \ No newline at end of file