Skip to content

Commit

Permalink
Provide better error message after syntax error (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Oct 29, 2024
1 parent a09ca06 commit b80fbe3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withSkip([
// invalid syntax test fixture
__DIR__ . '/tests/PhpParser/Finder/ClassConstantFetchFinder/Fixture/Error/ParseError.php',
])
->withPreparedSets(psr12: true, common: true, symplify: true)
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withRootFiles();
10 changes: 9 additions & 1 deletion src/PhpParser/CachedPhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ public function parseFile(string $filePath): array
}

$fileContents = FileSystem::read($filePath);
$stmts = $this->phpParser->parse($fileContents);
try {
$stmts = $this->phpParser->parse($fileContents);
} catch (\Throwable $throwable) {
throw new \RuntimeException(sprintf(
'Could not parse file "%s": %s',
$filePath,
$throwable->getMessage()
), $throwable->getCode(), $throwable);
}

if (is_array($stmts)) {
$nodeTraverser = NodeTraverserFactory::create(new NameResolver());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function test(): void
$this->assertInstanceOf(ExternalClassAccessConstantFetch::class, $secondClassConstantFetch);
}

public function testParseError(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(
'Could not parse file "' . __DIR__ . '/Fixture/Error/ParseError.php": Syntax error, unexpected T_STRING on line 6'
);

$directory = __DIR__ . '/Fixture/Error';
$progressBar = new ProgressBar(new NullOutput());
$fileInfos = PhpFilesFinder::find([$directory]);
$this->classConstantsFetchFinder->find($fileInfos, $progressBar, false);
}

/**
* @return ClassConstantFetchInterface[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ParseError;

function doFoo() {
// this file intentionally contains this parse error
$x ABC
}

0 comments on commit b80fbe3

Please sign in to comment.