From 0ffddce52d816f72d0efc4d9b02e276d3309ef01 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 6 Mar 2023 18:42:32 +0100 Subject: [PATCH] [4.x] Add constructor property promotion By making flags on the Param builder configurable by providing make(Public|Protected|Private) methods we can promote parameters to properties from the constructor --- lib/PhpParser/Builder/Param.php | 37 +++++++++++++++++++++++++++- test/PhpParser/Builder/ParamTest.php | 36 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/Builder/Param.php b/lib/PhpParser/Builder/Param.php index de9aae7e5e..f86d2ac06b 100644 --- a/lib/PhpParser/Builder/Param.php +++ b/lib/PhpParser/Builder/Param.php @@ -19,6 +19,8 @@ class Param implements PhpParser\Builder protected $variadic = false; + protected $flags = 0; + /** @var Node\AttributeGroup[] */ protected $attributeGroups = []; @@ -95,6 +97,39 @@ public function makeVariadic() { return $this; } + /** + * Makes the parameter public. + * + * @return $this The builder instance (for fluid interface) + */ + public function makePublic() { + $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PUBLIC); + + return $this; + } + + /** + * Makes the parameter protected. + * + * @return $this The builder instance (for fluid interface) + */ + public function makeProtected() { + $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PROTECTED); + + return $this; + } + + /** + * Makes the parameter private. + * + * @return $this The builder instance (for fluid interface) + */ + public function makePrivate() { + $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PRIVATE); + + return $this; + } + /** * Adds an attribute group. * @@ -116,7 +151,7 @@ public function addAttribute($attribute) { public function getNode() : Node { return new Node\Param( new Node\Expr\Variable($this->name), - $this->default, $this->type, $this->byRef, $this->variadic, [], 0, $this->attributeGroups + $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups ); } } diff --git a/test/PhpParser/Builder/ParamTest.php b/test/PhpParser/Builder/ParamTest.php index 9fe8160785..bbee164d32 100644 --- a/test/PhpParser/Builder/ParamTest.php +++ b/test/PhpParser/Builder/ParamTest.php @@ -205,6 +205,42 @@ public function testVariadic() { ); } + public function testMakePublic() { + $node = $this->createParamBuilder('test') + ->makePublic() + ->getNode() + ; + + $this->assertEquals( + new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PUBLIC), + $node + ); + } + + public function testMakeProtected() { + $node = $this->createParamBuilder('test') + ->makeProtected() + ->getNode() + ; + + $this->assertEquals( + new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PROTECTED), + $node + ); + } + + public function testMakePrivate() { + $node = $this->createParamBuilder('test') + ->makePrivate() + ->getNode() + ; + + $this->assertEquals( + new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PRIVATE), + $node + ); + } + public function testAddAttribute() { $attribute = new Attribute( new Name('Attr'),