|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\CodeAst\Builder; |
| 12 | + |
| 13 | +use OpenCodeModeling\CodeAst\Code\BodyGenerator; |
| 14 | +use OpenCodeModeling\CodeAst\Code\ClassConstGenerator; |
| 15 | +use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock; |
| 16 | +use OpenCodeModeling\CodeAst\Code\MethodGenerator; |
| 17 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod; |
| 18 | +use PhpParser\Node; |
| 19 | +use PhpParser\NodeTraverser; |
| 20 | +use PhpParser\NodeVisitor; |
| 21 | +use PhpParser\Parser; |
| 22 | + |
| 23 | +final class ClassMethodBuilder |
| 24 | +{ |
| 25 | + /** @var string */ |
| 26 | + private $name; |
| 27 | + |
| 28 | + /** @var ParameterBuilder[] */ |
| 29 | + private $parameters = []; |
| 30 | + |
| 31 | + /** @var string */ |
| 32 | + private $body = ''; |
| 33 | + |
| 34 | + /** @var string|null */ |
| 35 | + private $returnType; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var int |
| 39 | + */ |
| 40 | + private $visibility; |
| 41 | + |
| 42 | + /** @var bool */ |
| 43 | + private $typed = false; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string|null |
| 47 | + */ |
| 48 | + private $docBlockComment; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string|null |
| 52 | + */ |
| 53 | + private $returnTypeDocBlockHint; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var DocBlock|null |
| 57 | + */ |
| 58 | + private $docBlock; |
| 59 | + |
| 60 | + private function __construct() |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | + public static function fromNode(Node\Stmt\ClassMethod $node): self |
| 65 | + { |
| 66 | + $self = new self(); |
| 67 | + |
| 68 | + $self->name = $node->name->toString(); |
| 69 | + $self->returnType = $node->returnType ? $node->returnType->toString() : null; |
| 70 | + $self->visibility = $node->flags; |
| 71 | + |
| 72 | + foreach ($node->params as $param) { |
| 73 | + $self->parameters[] = ParameterBuilder::fromNode($param); |
| 74 | + } |
| 75 | + |
| 76 | + if ($self->returnType !== null) { |
| 77 | + $self->typed = true; |
| 78 | + } |
| 79 | + |
| 80 | + return $self; |
| 81 | + } |
| 82 | + |
| 83 | + public static function fromScratch( |
| 84 | + string $name, |
| 85 | + bool $typed = true |
| 86 | + ): self { |
| 87 | + $self = new self(); |
| 88 | + $self->name = $name; |
| 89 | + $self->typed = $typed; |
| 90 | + $self->visibility = ClassConstGenerator::FLAG_PUBLIC; |
| 91 | + |
| 92 | + return $self; |
| 93 | + } |
| 94 | + |
| 95 | + public function setParameters(ParameterBuilder ...$parameters): self |
| 96 | + { |
| 97 | + $this->parameters = $parameters; |
| 98 | + |
| 99 | + return $this; |
| 100 | + } |
| 101 | + |
| 102 | + public function setBody(string $body): self |
| 103 | + { |
| 104 | + $this->body = $body; |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + |
| 109 | + public function setReturnType(?string $returnType): self |
| 110 | + { |
| 111 | + $this->returnType = $returnType; |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + public function getName(): string |
| 117 | + { |
| 118 | + return $this->name; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return ParameterBuilder[] |
| 123 | + */ |
| 124 | + public function getParameters(): array |
| 125 | + { |
| 126 | + return $this->parameters; |
| 127 | + } |
| 128 | + |
| 129 | + public function isTyped(): bool |
| 130 | + { |
| 131 | + return $this->typed; |
| 132 | + } |
| 133 | + |
| 134 | + public function setPrivate(): self |
| 135 | + { |
| 136 | + $this->visibility = ClassConstGenerator::FLAG_PRIVATE; |
| 137 | + |
| 138 | + return $this; |
| 139 | + } |
| 140 | + |
| 141 | + public function setProtected(): self |
| 142 | + { |
| 143 | + $this->visibility = ClassConstGenerator::FLAG_PROTECTED; |
| 144 | + |
| 145 | + return $this; |
| 146 | + } |
| 147 | + |
| 148 | + public function setPublic(): self |
| 149 | + { |
| 150 | + $this->visibility = ClassConstGenerator::FLAG_PUBLIC; |
| 151 | + |
| 152 | + return $this; |
| 153 | + } |
| 154 | + |
| 155 | + public function getDocBlockComment(): ?string |
| 156 | + { |
| 157 | + return $this->docBlockComment; |
| 158 | + } |
| 159 | + |
| 160 | + public function setDocBlockComment(?string $docBlockComment): void |
| 161 | + { |
| 162 | + $this->docBlockComment = $docBlockComment; |
| 163 | + } |
| 164 | + |
| 165 | + public function getReturnTypeDocBlockHint(): string |
| 166 | + { |
| 167 | + return $this->returnTypeDocBlockHint; |
| 168 | + } |
| 169 | + |
| 170 | + public function setReturnTypeDocBlockHint(?string $typeDocBlockHint): void |
| 171 | + { |
| 172 | + $this->returnTypeDocBlockHint = $typeDocBlockHint; |
| 173 | + } |
| 174 | + |
| 175 | + public function getDocBlock(): ?DocBlock |
| 176 | + { |
| 177 | + return $this->docBlock; |
| 178 | + } |
| 179 | + |
| 180 | + public function overrideDocBlock(?DocBlock $docBlock): void |
| 181 | + { |
| 182 | + $this->docBlock = $docBlock; |
| 183 | + } |
| 184 | + |
| 185 | + public function generate(Parser $parser): NodeVisitor |
| 186 | + { |
| 187 | + return new ClassMethod($this->methodGenerator($parser)); |
| 188 | + } |
| 189 | + |
| 190 | + private function methodGenerator(Parser $parser): MethodGenerator |
| 191 | + { |
| 192 | + $flags = $this->visibility; |
| 193 | + |
| 194 | + $body = new BodyGenerator($parser, $this->body); |
| 195 | + |
| 196 | + $methodGenerator = new MethodGenerator( |
| 197 | + $this->name, |
| 198 | + \array_map(static function (ParameterBuilder $builder) { |
| 199 | + return $builder->generate(); |
| 200 | + }, $this->parameters), |
| 201 | + $flags, |
| 202 | + $body |
| 203 | + ); |
| 204 | + |
| 205 | + $methodGenerator->setReturnType($this->returnType); |
| 206 | + $methodGenerator->setTyped($this->typed); |
| 207 | + $methodGenerator->setDocBlockComment($this->docBlockComment); |
| 208 | + $methodGenerator->setReturnTypeDocBlockHint($this->returnTypeDocBlockHint); |
| 209 | + $methodGenerator->overrideDocBlock($this->docBlock); |
| 210 | + |
| 211 | + return $methodGenerator; |
| 212 | + } |
| 213 | + |
| 214 | + public function injectVisitors(NodeTraverser $nodeTraverser, Parser $parser): void |
| 215 | + { |
| 216 | + $nodeTraverser->addVisitor($this->generate($parser)); |
| 217 | + } |
| 218 | +} |
0 commit comments