Skip to content

Commit

Permalink
[4.x] Add constructor property promotion
Browse files Browse the repository at this point in the history
By making flags on the Param builder configurable by providing make(Public|Protected|Private) methods we can promote parameters to properties from the constructor
  • Loading branch information
WyriHaximus authored and nikic committed Mar 6, 2023
1 parent 6bb5176 commit 0ffddce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
37 changes: 36 additions & 1 deletion lib/PhpParser/Builder/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Param implements PhpParser\Builder

protected $variadic = false;

protected $flags = 0;

/** @var Node\AttributeGroup[] */
protected $attributeGroups = [];

Expand Down Expand Up @@ -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.
*
Expand All @@ -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
);
}
}
36 changes: 36 additions & 0 deletions test/PhpParser/Builder/ParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 0ffddce

Please sign in to comment.