From 9f0d65f4bbed8e945e5d3e606f873a61f56e13d3 Mon Sep 17 00:00:00 2001 From: Thomas Rieschl Date: Mon, 14 Sep 2020 15:25:52 +0200 Subject: [PATCH 1/2] use a non-emulative Lexer for PhpParser --- src/ComposerRequireChecker/Cli/CheckCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ComposerRequireChecker/Cli/CheckCommand.php b/src/ComposerRequireChecker/Cli/CheckCommand.php index b6e26c44..06c1eda8 100644 --- a/src/ComposerRequireChecker/Cli/CheckCommand.php +++ b/src/ComposerRequireChecker/Cli/CheckCommand.php @@ -14,6 +14,7 @@ use ComposerRequireChecker\JsonLoader; use ComposerRequireChecker\UsedSymbolsLocator\LocateUsedSymbolsFromASTRoots; use PhpParser\ErrorHandler\Collecting as CollectingErrorHandler; +use PhpParser\Lexer; use PhpParser\ParserFactory; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\Table; @@ -155,8 +156,8 @@ private function getComposerData(string $jsonFile): array private function getASTFromFilesLocator(InputInterface $input): LocateASTFromFiles { $errorHandler = $input->getOption('ignore-parse-errors') ? new CollectingErrorHandler() : null; - $sourcesASTs = new LocateASTFromFiles((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $errorHandler); - return $sourcesASTs; + $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new Lexer()); + return new LocateASTFromFiles($parser, $errorHandler); } From 0296c64a0ceb6ab7d225d8a7e64e721f155b4227 Mon Sep 17 00:00:00 2001 From: Thomas Rieschl Date: Thu, 1 Oct 2020 22:44:01 +0200 Subject: [PATCH 2/2] add a test --- .../Cli/CheckCommandTest.php | 30 +++++++++++++++++++ test/fixtures/noUnknownSymbols/composer.json | 11 +++++++ .../noUnknownSymbols/src/SomeClass.php | 15 ++++++++++ .../vendor/composer/installed.json | 1 + 4 files changed, 57 insertions(+) create mode 100644 test/fixtures/noUnknownSymbols/composer.json create mode 100644 test/fixtures/noUnknownSymbols/src/SomeClass.php create mode 100644 test/fixtures/noUnknownSymbols/vendor/composer/installed.json diff --git a/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php b/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php index ef6d86c9..a683c632 100644 --- a/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php +++ b/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php @@ -5,9 +5,15 @@ use ComposerRequireChecker\Cli\Application; use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; +use function dirname; +use function file_put_contents; +use function unlink; +use function version_compare; + final class CheckCommandTest extends TestCase { @@ -122,4 +128,28 @@ public function testSourceFileThatUsesDevDependency(): void $this->assertNotEquals(0, $exitCode); $this->assertRegExp('/The following unknown symbols were found.*PHPUnit\\\\Framework\\\\TestCase/s', $this->commandTester->getDisplay()); } + + public function testNoUnknownSymbolsFound(): void + { + $baseDir = dirname(__DIR__, 2) . '/fixtures/noUnknownSymbols/'; + $this->commandTester->execute(['composer-json' => $baseDir . 'composer.json']); + + self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode()); + self::assertStringContainsString('There were no unknown symbols found.', $this->commandTester->getDisplay()); + } + + public function testReservedKeywordInPhp8DoesNotThrowExceptionInPhp7(): void + { + if (version_compare(PHP_VERSION, '8.0.0') >= 0) { + self::markTestSkipped('This test does not work in PHP8'); + } + $baseDir = dirname(__DIR__, 2) . '/fixtures/noUnknownSymbols/'; + $tmpFile = $baseDir . 'src/Match.php'; + file_put_contents($tmpFile, 'commandTester->execute(['composer-json' => $baseDir . 'composer.json']); + + unlink($tmpFile); + self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode()); + } } diff --git a/test/fixtures/noUnknownSymbols/composer.json b/test/fixtures/noUnknownSymbols/composer.json new file mode 100644 index 00000000..844c5af7 --- /dev/null +++ b/test/fixtures/noUnknownSymbols/composer.json @@ -0,0 +1,11 @@ +{ + "name": "example/library", + "require": { + "php": "^7.2" + }, + "autoload": { + "psr-4": { + "Example\\Library\\": "src/" + } + } +} diff --git a/test/fixtures/noUnknownSymbols/src/SomeClass.php b/test/fixtures/noUnknownSymbols/src/SomeClass.php new file mode 100644 index 00000000..621217cf --- /dev/null +++ b/test/fixtures/noUnknownSymbols/src/SomeClass.php @@ -0,0 +1,15 @@ +