| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Builder; |
| 4: | |
| 5: | use PhpParser; |
| 6: | use PhpParser\BuilderHelpers; |
| 7: | use PhpParser\Node; |
| 8: | |
| 9: | class Param implements PhpParser\Builder |
| 10: | { |
| 11: | protected $name; |
| 12: | |
| 13: | protected $default = null; |
| 14: | |
| 15: | |
| 16: | protected $type = null; |
| 17: | |
| 18: | protected $byRef = false; |
| 19: | |
| 20: | protected $variadic = false; |
| 21: | |
| 22: | protected $flags = 0; |
| 23: | |
| 24: | |
| 25: | protected $attributeGroups = []; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public function __construct(string $name) { |
| 33: | $this->name = $name; |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public function setDefault($value) { |
| 44: | $this->default = BuilderHelpers::normalizeValue($value); |
| 45: | |
| 46: | return $this; |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function setType($type) { |
| 57: | $this->type = BuilderHelpers::normalizeType($type); |
| 58: | if ($this->type == 'void') { |
| 59: | throw new \LogicException('Parameter type cannot be void'); |
| 60: | } |
| 61: | |
| 62: | return $this; |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | public function setTypeHint($type) { |
| 75: | return $this->setType($type); |
| 76: | } |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | public function makeByRef() { |
| 84: | $this->byRef = true; |
| 85: | |
| 86: | return $this; |
| 87: | } |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | public function makeVariadic() { |
| 95: | $this->variadic = true; |
| 96: | |
| 97: | return $this; |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | public function makePublic() { |
| 106: | $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PUBLIC); |
| 107: | |
| 108: | return $this; |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | public function makeProtected() { |
| 117: | $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PROTECTED); |
| 118: | |
| 119: | return $this; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | public function makePrivate() { |
| 128: | $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_PRIVATE); |
| 129: | |
| 130: | return $this; |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | public function makeReadonly() { |
| 139: | $this->flags = BuilderHelpers::addModifier($this->flags, Node\Stmt\Class_::MODIFIER_READONLY); |
| 140: | |
| 141: | return $this; |
| 142: | } |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | public function addAttribute($attribute) { |
| 152: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 153: | |
| 154: | return $this; |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | public function getNode() : Node { |
| 163: | return new Node\Param( |
| 164: | new Node\Expr\Variable($this->name), |
| 165: | $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups |
| 166: | ); |
| 167: | } |
| 168: | } |
| 169: | |