From 11e2dcd96c830ee934fa7b0243f4d67d8a8821ab Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 19 May 2023 22:17:09 +0200 Subject: [PATCH] Add makeReadonly() to param builder --- lib/PhpParser/Builder/Param.php | 17 ++++++++++++++--- test/PhpParser/Builder/ParamTest.php | 12 ++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/PhpParser/Builder/Param.php b/lib/PhpParser/Builder/Param.php index f86d2ac06b..69f353326a 100644 --- a/lib/PhpParser/Builder/Param.php +++ b/lib/PhpParser/Builder/Param.php @@ -98,7 +98,7 @@ public function makeVariadic() { } /** - * Makes the parameter public. + * Makes the (promoted) parameter public. * * @return $this The builder instance (for fluid interface) */ @@ -109,7 +109,7 @@ public function makePublic() { } /** - * Makes the parameter protected. + * Makes the (promoted) parameter protected. * * @return $this The builder instance (for fluid interface) */ @@ -120,7 +120,7 @@ public function makeProtected() { } /** - * Makes the parameter private. + * Makes the (promoted) parameter private. * * @return $this The builder instance (for fluid interface) */ @@ -130,6 +130,17 @@ public function makePrivate() { return $this; } + /** + * Makes the (promoted) parameter readonly. + * + * @return $this The builder instance (for fluid interface) + */ + public function makeReadonly() { + $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_READONLY); + + return $this; + } + /** * Adds an attribute group. * diff --git a/test/PhpParser/Builder/ParamTest.php b/test/PhpParser/Builder/ParamTest.php index bbee164d32..7c6ca4f180 100644 --- a/test/PhpParser/Builder/ParamTest.php +++ b/test/PhpParser/Builder/ParamTest.php @@ -241,6 +241,18 @@ public function testMakePrivate() { ); } + public function testMakeReadonly() { + $node = $this->createParamBuilder('test') + ->makeReadonly() + ->getNode() + ; + + $this->assertEquals( + new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_READONLY), + $node + ); + } + public function testAddAttribute() { $attribute = new Attribute( new Name('Attr'),