Skip to content

Commit

Permalink
Update LexerFactory to take advantage of the new Emulative lexer capa…
Browse files Browse the repository at this point in the history
…bilities
  • Loading branch information
ondrejmirtes committed Aug 9, 2020
1 parent 65a74c6 commit 71013bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Parser/LexerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}

}
9 changes: 9 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion tests/PHPStan/Php/PhpVersionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
}
Expand All @@ -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());
}

}

0 comments on commit 71013bc

Please sign in to comment.