| 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: | use PhpParser\Node\Stmt; |
| 10: | |
| 11: | class Method extends FunctionLike { |
| 12: | protected string $name; |
| 13: | |
| 14: | protected int $flags = 0; |
| 15: | |
| 16: | |
| 17: | protected ?array $stmts = []; |
| 18: | |
| 19: | |
| 20: | protected array $attributeGroups = []; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | public function __construct(string $name) { |
| 28: | $this->name = $name; |
| 29: | } |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public function makePublic() { |
| 37: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
| 38: | |
| 39: | return $this; |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public function makeProtected() { |
| 48: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
| 49: | |
| 50: | return $this; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | public function makePrivate() { |
| 59: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
| 60: | |
| 61: | return $this; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function makeStatic() { |
| 70: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC); |
| 71: | |
| 72: | return $this; |
| 73: | } |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public function makeAbstract() { |
| 81: | if (!empty($this->stmts)) { |
| 82: | throw new \LogicException('Cannot make method with statements abstract'); |
| 83: | } |
| 84: | |
| 85: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::ABSTRACT); |
| 86: | $this->stmts = null; |
| 87: | |
| 88: | return $this; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public function makeFinal() { |
| 97: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL); |
| 98: | |
| 99: | return $this; |
| 100: | } |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | public function addStmt($stmt) { |
| 110: | if (null === $this->stmts) { |
| 111: | throw new \LogicException('Cannot add statements to an abstract method'); |
| 112: | } |
| 113: | |
| 114: | $this->stmts[] = BuilderHelpers::normalizeStmt($stmt); |
| 115: | |
| 116: | return $this; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public function addAttribute($attribute) { |
| 127: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 128: | |
| 129: | return $this; |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | public function getNode(): Node { |
| 138: | return new Stmt\ClassMethod($this->name, [ |
| 139: | 'flags' => $this->flags, |
| 140: | 'byRef' => $this->returnByRef, |
| 141: | 'params' => $this->params, |
| 142: | 'returnType' => $this->returnType, |
| 143: | 'stmts' => $this->stmts, |
| 144: | 'attrGroups' => $this->attributeGroups, |
| 145: | ], $this->attributes); |
| 146: | } |
| 147: | } |
| 148: | |