Skip to content

Commit

Permalink
Throw more informative error when file has PHP syntax error (#18)
Browse files Browse the repository at this point in the history
* Throw more informative error when file has PHP syntax error

* Fix warning, passing generator to count
  • Loading branch information
cspray authored May 13, 2023
1 parent 8d1a597 commit 0446267
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
11 changes: 11 additions & 0 deletions fixture_src/BadPhpFileFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTargetFixture;

class BadPhpFileFixture implements Fixture {

public function getPath() : string {
return __DIR__ . '/InvalidPhpSyntax';
}

}
4 changes: 4 additions & 0 deletions fixture_src/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ public static function classOnlyAttributeSingleInterface() : ClassOnlyAttributeS
public static function targetAttributeInterface() : TargetAttributeInterfaceFixture {
return new TargetAttributeInterfaceFixture();
}

public static function invalidSyntax() : BadPhpFileFixture {
return new BadPhpFileFixture();
}
}
11 changes: 11 additions & 0 deletions fixture_src/InvalidPhpSyntax/BadPhpFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTargetFixture\InvalidPhpSyntax;

class BadPhpFile {

public function method() : string {
return '':
}

}
7 changes: 7 additions & 0 deletions src/Exception/InvalidPhpSyntax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTarget\Exception;

final class InvalidPhpSyntax extends Exception {

}
16 changes: 13 additions & 3 deletions src/PhpParserAnnotatedTargetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Cspray\AnnotatedTarget;

use Cspray\AnnotatedTarget\Exception\InvalidPhpSyntax;
use FilesystemIterator;
use Generator;
use Iterator;
use PhpParser\Error;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
Expand Down Expand Up @@ -42,9 +44,17 @@ public function parse(AnnotatedTargetParserOptions $options) : Generator {
));

foreach ($this->getSourceIterator($options) as $sourceFile) {
$nodes = $this->parser->parse(file_get_contents($sourceFile->getPathname()));
$nodeTraverser->traverse($nodes);
unset($nodes);
try {
$nodes = $this->parser->parse(file_get_contents($sourceFile->getPathname()));
$nodeTraverser->traverse($nodes);
} catch (Error $error) {
throw new InvalidPhpSyntax(
message: sprintf('Encountered error parsing %s. Message: %s', $sourceFile, $error->getMessage()),
previous: $error
);
} finally {
unset($nodes);
}
}

yield from $data->targets;
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/InvalidPhpSyntaxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedTarget\Unit;

use Cspray\AnnotatedTarget\Exception\InvalidPhpSyntax;
use Cspray\AnnotatedTargetFixture\Fixtures;

uses(AnnotatedTargetParserTestCase::class);

beforeEach()->withFixtures(Fixtures::invalidSyntax());


it('throws an exception if invalid PHP syntax is encountered', fn() => $this->getTargets())
->throws(
InvalidPhpSyntax::class,
'Encountered error parsing ' . Fixtures::invalidSyntax()->getPath() . '/BadPhpFile.php. Message: Syntax error, unexpected \':\', expecting \';\' on line 8'
);
2 changes: 1 addition & 1 deletion tests/Unit/ParseAttributesMultipleDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use function Cspray\AnnotatedTarget\parseAttributes;
use function Cspray\Typiphy\objectType;

$targets = fn() => parseAttributes([Fixtures::classOnlyAttributeSingleClass()->getPath(), Fixtures::propertyOnlyAttributeSingleClass()->getPath()]);
$targets = fn() => iterator_to_array(parseAttributes([Fixtures::classOnlyAttributeSingleClass()->getPath(), Fixtures::propertyOnlyAttributeSingleClass()->getPath()]));

it('counts targets for single class')
->expect($targets)
Expand Down

0 comments on commit 0446267

Please sign in to comment.