| 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 |
/** @var list<Stmt>|null */ |
| 17 |
protected ?array $stmts = []; |
| 18 |
|
| 19 |
/** @var list<Node\AttributeGroup> */ |
| 20 |
protected array $attributeGroups = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* Creates a method builder. |
| 24 |
* |
| 25 |
* @param string $name Name of the method |
| 26 |
*/ |
| 27 |
public function __construct(string $name) { |
| 28 |
$this->name = $name; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Makes the method public. |
| 33 |
* |
| 34 |
* @return $this The builder instance (for fluid interface) |
| 35 |
*/ |
| 36 |
public function makePublic() { |
| 37 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Makes the method protected. |
| 44 |
* |
| 45 |
* @return $this The builder instance (for fluid interface) |
| 46 |
*/ |
| 47 |
public function makeProtected() { |
| 48 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Makes the method private. |
| 55 |
* |
| 56 |
* @return $this The builder instance (for fluid interface) |
| 57 |
*/ |
| 58 |
public function makePrivate() { |
| 59 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Makes the method static. |
| 66 |
* |
| 67 |
* @return $this The builder instance (for fluid interface) |
| 68 |
*/ |
| 69 |
public function makeStatic() { |
| 70 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC); |
| 71 |
|
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Makes the method abstract. |
| 77 |
* |
| 78 |
* @return $this The builder instance (for fluid interface) |
| 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; // abstract methods don't have statements |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Makes the method final. |
| 93 |
* |
| 94 |
* @return $this The builder instance (for fluid interface) |
| 95 |
*/ |
| 96 |
public function makeFinal() { |
| 97 |
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL); |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds a statement. |
| 104 |
* |
| 105 |
* @param Node|PhpParser\Builder $stmt The statement to add |
| 106 |
* |
| 107 |
* @return $this The builder instance (for fluid interface) |
| 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 |
* Adds an attribute group. |
| 121 |
* |
| 122 |
* @param Node\Attribute|Node\AttributeGroup $attribute |
| 123 |
* |
| 124 |
* @return $this The builder instance (for fluid interface) |
| 125 |
*/ |
| 126 |
public function addAttribute($attribute) { |
| 127 |
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the built method node. |
| 134 |
* |
| 135 |
* @return Stmt\ClassMethod The built method node |
| 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 |
} |