Skip to content

Commit e041151

Browse files
committed
Improve error for method body syntax errors - close #78
1 parent 9dfb9a0 commit e041151

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/Code/MethodGenerator.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OpenCodeModeling\CodeAst\Code\DocBlock\Tag\ReturnTag;
1616
use OpenCodeModeling\CodeAst\Exception;
1717
use PhpParser\Comment\Doc;
18+
use PhpParser\Error;
1819
use PhpParser\Node\Stmt\ClassMethod;
1920

2021
/**
@@ -215,21 +216,33 @@ public function overrideDocBlock(?DocBlock $docBlock): void
215216

216217
public function generate(): ClassMethod
217218
{
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()
227241
),
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+
}
233246
}
234247

235248
private function generateAttributes(): array

tests/Code/MethodGeneratorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
namespace OpenCodeModelingTest\CodeAst\Code;
1212

13+
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
1314
use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock;
1415
use OpenCodeModeling\CodeAst\Code\MethodGenerator;
1516
use OpenCodeModeling\CodeAst\Code\ParameterGenerator;
17+
use OpenCodeModeling\CodeAst\Exception\RuntimeException;
1618
use PhpParser\Parser;
1719
use PhpParser\ParserFactory;
1820
use PhpParser\PrettyPrinter\Standard;
@@ -262,4 +264,26 @@ public function setType(string $type, ?int $value);
262264

263265
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
264266
}
267+
268+
/**
269+
* @test
270+
*/
271+
public function it_catches_body_parse_errors_and_throws_runtime_exception(): void
272+
{
273+
$method = new MethodGenerator(
274+
'setType',
275+
[
276+
new ParameterGenerator('type', '?string'),
277+
],
278+
MethodGenerator::FLAG_PUBLIC,
279+
new BodyGenerator($this->parser, '$test=1; error=true')
280+
);
281+
$method->setTyped(true);
282+
$method->setReturnType('void');
283+
284+
$this->expectException(RuntimeException::class);
285+
$this->expectExceptionMessage('Could not generate method "setType" due the following error');
286+
287+
$this->printer->prettyPrintFile([$method->generate()]);
288+
}
265289
}

0 commit comments

Comments
 (0)