|
15 | 15 | use OpenCodeModeling\CodeAst\Code\DocBlock\Tag\ReturnTag; |
16 | 16 | use OpenCodeModeling\CodeAst\Exception; |
17 | 17 | use PhpParser\Comment\Doc; |
| 18 | +use PhpParser\Error; |
18 | 19 | use PhpParser\Node\Stmt\ClassMethod; |
19 | 20 |
|
20 | 21 | /** |
@@ -215,21 +216,33 @@ public function overrideDocBlock(?DocBlock $docBlock): void |
215 | 216 |
|
216 | 217 | public function generate(): ClassMethod |
217 | 218 | { |
218 | | - return new ClassMethod( |
219 | | - $this->getName(), |
220 | | - [ |
221 | | - 'flags' => $this->flags, |
222 | | - 'params' => \array_map( |
223 | | - static function (ParameterGenerator $parameter) { |
224 | | - return $parameter->generate(); |
225 | | - }, |
226 | | - $this->getParameters() |
| 219 | + try { |
| 220 | + return new ClassMethod( |
| 221 | + $this->getName(), |
| 222 | + [ |
| 223 | + 'flags' => $this->flags, |
| 224 | + 'params' => \array_map( |
| 225 | + static function (ParameterGenerator $parameter) { |
| 226 | + return $parameter->generate(); |
| 227 | + }, |
| 228 | + $this->getParameters() |
| 229 | + ), |
| 230 | + 'stmts' => $this->body ? $this->body->generate() : null, |
| 231 | + 'returnType' => $this->returnType ? $this->returnType->generate() : null, |
| 232 | + ], |
| 233 | + $this->generateAttributes() |
| 234 | + ); |
| 235 | + } catch (Error $e) { |
| 236 | + throw new Exception\RuntimeException( |
| 237 | + \sprintf( |
| 238 | + 'Could not generate method "%s" due the following error: %s', |
| 239 | + $this->getName(), |
| 240 | + $e->getMessage() |
227 | 241 | ), |
228 | | - 'stmts' => $this->body ? $this->body->generate() : null, |
229 | | - 'returnType' => $this->returnType ? $this->returnType->generate() : null, |
230 | | - ], |
231 | | - $this->generateAttributes() |
232 | | - ); |
| 242 | + (int) $e->getCode(), |
| 243 | + $e |
| 244 | + ); |
| 245 | + } |
233 | 246 | } |
234 | 247 |
|
235 | 248 | private function generateAttributes(): array |
|
0 commit comments