| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace PhpParser\Builder; |
| 4 |
|
| 5 |
use PhpParser; |
| 6 |
use PhpParser\BuilderHelpers; |
| 7 |
use PhpParser\Modifiers; |
| 8 |
use PhpParser\Node; |
| 9 |
|
| 10 |
class Param implements PhpParser\Builder { |
| 11 |
protected string $name; |
| 12 |
protected ?Node\Expr $default = null; |
| 13 |
/** @var Node\Identifier|Node\Name|Node\ComplexType|null */ |
| 14 |
protected ?Node $type = null; |
| 15 |
protected bool $byRef = false; |
| 16 |
protected int $flags = 0; |
| 17 |
protected bool $variadic = false; |
| 18 |
/** @var list<Node\AttributeGroup> */ |
| 19 |
protected array $attributeGroups = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Creates a parameter builder. |
| 23 |
* |
| 24 |
* @param string $name Name of the parameter |
| 25 |
*/ |
| 26 |
public function __construct(string $name) { |
| 27 |
$this->name = $name; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Sets default value for the parameter. |
| 32 |
* |
| 33 |
* @param mixed $value Default value to use |
| 34 |
* |
| 35 |
* @return $this The builder instance (for fluid interface) |
| 36 |
*/ |
| 37 |
public function setDefault($value) { |
| 38 |
$this->default = BuilderHelpers::normalizeValue($value); |
| 39 |
|
| 40 |
return $this; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Sets type for the parameter. |
| 45 |
* |
| 46 |
* @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type |
| 47 |
* |
| 48 |
* @return $this The builder instance (for fluid interface) |
| 49 |
*/ |
| 50 |
public function setType($type) { |
| 51 |
$this->type = BuilderHelpers::normalizeType($type); |
| 52 |
if ($this->type == 'void') { |
| 53 |
throw new \LogicException('Parameter type cannot be void'); |
| 54 |
} |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Make the parameter accept the value by reference. |
| 61 |
* |
| 62 |
* @return $this The builder instance (for fluid interface) |
| 63 |
*/ |
| 64 |
public function makeByRef() { |
| 65 |
$this->byRef = true; |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Make the parameter variadic |
| 72 |
* |
| 73 |
* @return $this The builder instance (for fluid interface) |
| 74 |
*/ |
| 75 |
public function makeVariadic() { |
| 76 |
$this->variadic = true; |
| 77 |
|
| 78 |
return $this; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Makes the (promoted) parameter public. |
| 83 |
* |
| 84 |
* @return $this The builder instance (for fluid interface) |
| 85 |
*/ |
| 86 |
public function makePublic() { |
| 87 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Makes the (promoted) parameter protected. |
| 94 |
* |
| 95 |
* @return $this The builder instance (for fluid interface) |
| 96 |
*/ |
| 97 |
public function makeProtected() { |
| 98 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Makes the (promoted) parameter private. |
| 105 |
* |
| 106 |
* @return $this The builder instance (for fluid interface) |
| 107 |
*/ |
| 108 |
public function makePrivate() { |
| 109 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
| 110 |
|
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Makes the (promoted) parameter readonly. |
| 116 |
* |
| 117 |
* @return $this The builder instance (for fluid interface) |
| 118 |
*/ |
| 119 |
public function makeReadonly() { |
| 120 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY); |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Adds an attribute group. |
| 127 |
* |
| 128 |
* @param Node\Attribute|Node\AttributeGroup $attribute |
| 129 |
* |
| 130 |
* @return $this The builder instance (for fluid interface) |
| 131 |
*/ |
| 132 |
public function addAttribute($attribute) { |
| 133 |
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 134 |
|
| 135 |
return $this; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns the built parameter node. |
| 140 |
* |
| 141 |
* @return Node\Param The built parameter node |
| 142 |
*/ |
| 143 |
public function getNode(): Node { |
| 144 |
return new Node\Param( |
| 145 |
new Node\Expr\Variable($this->name), |
| 146 |
$this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|