-
Notifications
You must be signed in to change notification settings - Fork 541
Implement Scope->getPhpVersion() #3642
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
Changes from all commits
9bb325b
935b88b
6928a88
3abd812
0a60270
7a0a78f
b4bbcee
45eb043
e6c97d3
7366fd2
6b0e638
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Php; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\IntegerRangeType; | ||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* @api | ||
*/ | ||
final class PhpVersions | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
|
||
public function __construct( | ||
private Type $phpVersions, | ||
) | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
public function producesWarningForFinalPrivateMethods(): TrinaryLogic | ||
{ | ||
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Php; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\IntegerRangeType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PhpVersionsTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider dataProducesWarningForFinalPrivateMethods | ||
*/ | ||
public function testProducesWarningForFinalPrivateMethods(TrinaryLogic $expected, Type $versionType): void | ||
{ | ||
$phpVersions = new PhpVersions($versionType); | ||
$this->assertSame( | ||
$expected->describe(), | ||
$phpVersions->producesWarningForFinalPrivateMethods()->describe(), | ||
); | ||
} | ||
|
||
public function dataProducesWarningForFinalPrivateMethods(): iterable | ||
{ | ||
yield [ | ||
TrinaryLogic::createNo(), | ||
new ConstantIntegerType(70400), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createYes(), | ||
new ConstantIntegerType(80000), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createYes(), | ||
new ConstantIntegerType(80100), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createYes(), | ||
IntegerRangeType::fromInterval(80000, null), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createMaybe(), | ||
IntegerRangeType::fromInterval(null, 80000), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createNo(), | ||
IntegerRangeType::fromInterval(70200, 70400), | ||
]; | ||
|
||
yield [ | ||
TrinaryLogic::createMaybe(), | ||
new UnionType([ | ||
IntegerRangeType::fromInterval(70200, 70400), | ||
IntegerRangeType::fromInterval(80200, 80400), | ||
]), | ||
]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,15 @@ | |
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** @extends RuleTestCase<FinalPrivateMethodRule> */ | ||
class FinalPrivateMethodRuleTest extends RuleTestCase | ||
{ | ||
|
||
private int $phpVersionId; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new FinalPrivateMethodRule( | ||
new PhpVersion($this->phpVersionId), | ||
); | ||
return new FinalPrivateMethodRule(); | ||
} | ||
|
||
public function dataRule(): array | ||
|
@@ -44,8 +41,35 @@ public function dataRule(): array | |
*/ | ||
public function testRule(int $phpVersion, array $errors): void | ||
{ | ||
$this->phpVersionId = $phpVersion; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not touch existing test, I can't read this diff. It'd be better to add a completely new file with the top-level ifs. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I now tried to keep the old test as much as possible |
||
$testVersion = new PhpVersion($phpVersion); | ||
$runtimeVersion = new PhpVersion(PHP_VERSION_ID); | ||
|
||
if ( | ||
$testVersion->getMajorVersionId() !== $runtimeVersion->getMajorVersionId() | ||
|| $testVersion->getMinorVersionId() !== $runtimeVersion->getMinorVersionId() | ||
) { | ||
$this->markTestSkipped('Test requires PHP version ' . $testVersion->getMajorVersionId() . '.' . $testVersion->getMinorVersionId() . '.*'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/final-private-method.php'], $errors); | ||
} | ||
|
||
public function testRulePhpVersions(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/final-private-method-phpversions.php'], [ | ||
[ | ||
'Private method FinalPrivateMethodPhpVersions\FooBarPhp8orHigher::foo() cannot be final as it is never overridden by other classes.', | ||
9, | ||
], | ||
[ | ||
'Private method FinalPrivateMethodPhpVersions\FooBarPhp74OrHigher::foo() cannot be final as it is never overridden by other classes.', | ||
29, | ||
], | ||
[ | ||
'Private method FinalPrivateMethodPhpVersions\FooBarBaz::foo() cannot be final as it is never overridden by other classes.', | ||
39, | ||
], | ||
]); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace FinalPrivateMethodPhpVersions; | ||
|
||
if (PHP_VERSION_ID >= 80000) { | ||
class FooBarPhp8orHigher | ||
{ | ||
|
||
final private function foo(): void | ||
{ | ||
} | ||
} | ||
} | ||
|
||
if (PHP_VERSION_ID < 80000) { | ||
class FooBarPhp7 | ||
{ | ||
|
||
final private function foo(): void | ||
{ | ||
} | ||
} | ||
} | ||
|
||
if (PHP_VERSION_ID > 70400) { | ||
class FooBarPhp74OrHigher | ||
{ | ||
|
||
final private function foo(): void | ||
{ | ||
} | ||
} | ||
} | ||
|
||
if (PHP_VERSION_ID < 70400 || PHP_VERSION_ID >= 80100) { | ||
class FooBarBaz | ||
{ | ||
|
||
final private function foo(): void | ||
{ | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can afford to start reading min/max values from the phpVersion config.