From 71013bc41eec44b9a5796ec6dd8c84b5e01b847e Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 9 Aug 2020 16:00:58 +0200 Subject: [PATCH] Update LexerFactory to take advantage of the new Emulative lexer capabilities --- src/Parser/LexerFactory.php | 6 ++++-- src/Php/PhpVersion.php | 9 +++++++++ tests/PHPStan/Php/PhpVersionFactoryTest.php | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Parser/LexerFactory.php b/src/Parser/LexerFactory.php index 4ab1b2c242..83283841a4 100644 --- a/src/Parser/LexerFactory.php +++ b/src/Parser/LexerFactory.php @@ -17,11 +17,13 @@ public function __construct(PhpVersion $phpVersion) public function create(): Lexer { - if ($this->phpVersion->getVersionId() <= PHP_VERSION_ID) { + if ($this->phpVersion->getVersionId() === PHP_VERSION_ID) { return new Lexer(); } - return new Lexer\Emulative(); + return new Lexer\Emulative([ + 'phpVersion' => $this->phpVersion->getVersionString(), + ]); } } diff --git a/src/Php/PhpVersion.php b/src/Php/PhpVersion.php index 1c5ea894b1..ba46cb811f 100644 --- a/src/Php/PhpVersion.php +++ b/src/Php/PhpVersion.php @@ -17,6 +17,15 @@ public function getVersionId(): int return $this->versionId; } + public function getVersionString(): string + { + $first = (int) floor($this->versionId / 10000); + $second = (int) floor(($this->versionId % 10000) / 100); + $third = (int) floor($this->versionId % 100); + + return $first . '.' . $second . ($third !== 0 ? '.' . $third : ''); + } + public function supportsNullCoalesceAssign(): bool { return $this->versionId >= 70400; diff --git a/tests/PHPStan/Php/PhpVersionFactoryTest.php b/tests/PHPStan/Php/PhpVersionFactoryTest.php index a243b1c4db..d762f850b5 100644 --- a/tests/PHPStan/Php/PhpVersionFactoryTest.php +++ b/tests/PHPStan/Php/PhpVersionFactoryTest.php @@ -14,41 +14,49 @@ public function dataCreate(): array null, null, PHP_VERSION_ID, + null, ], [ 70200, null, 70200, + '7.2', ], [ 70200, '7.4.6', 70200, + '7.2', ], [ null, '7.4.6', 70406, + '7.4.6', ], [ null, '7.0', 70100, + '7.1', ], [ null, '7.1.1', 70101, + '7.1.1', ], [ null, '5.4.1', 70100, + '7.1', ], [ null, '8.1', 80000, + '8.0', ], ]; } @@ -58,16 +66,24 @@ public function dataCreate(): array * @param int|null $versionId * @param string|null $composerPhpVersion * @param int $expectedVersion + * @param string|null $expectedVersionString */ public function testCreate( ?int $versionId, ?string $composerPhpVersion, - int $expectedVersion + int $expectedVersion, + ?string $expectedVersionString ): void { $factory = new PhpVersionFactory($versionId, $composerPhpVersion); $phpVersion = $factory->create(); $this->assertSame($expectedVersion, $phpVersion->getVersionId()); + + if ($expectedVersionString === null) { + return; + } + + $this->assertSame($expectedVersionString, $phpVersion->getVersionString()); } }