Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a non-emulative Lexer for PhpParser #199

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
30 changes: 30 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down Expand Up @@ -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, '<?php class Match { }');

$this->commandTester->execute(['composer-json' => $baseDir . 'composer.json']);

unlink($tmpFile);
self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
}
}
11 changes: 11 additions & 0 deletions test/fixtures/noUnknownSymbols/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "example/library",
"require": {
"php": "^7.2"
},
"autoload": {
"psr-4": {
"Example\\Library\\": "src/"
}
}
}
15 changes: 15 additions & 0 deletions test/fixtures/noUnknownSymbols/src/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Example\Library;

use function strlen;

final class SomeClass
{
public function someMethod(string $foo): int
{
return strlen($foo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]