Skip to content
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
11 changes: 11 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use PHPStan\Parser\NewAssignedToPropertyVisitor;
use PHPStan\Parser\Parser;
use PHPStan\Php\PhpVersion;
use PHPStan\Php\PhpVersions;
use PHPStan\PhpDoc\Tag\TemplateTag;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
Expand Down Expand Up @@ -5721,4 +5722,14 @@ public function getIterableValueType(Type $iteratee): Type
return $iteratee->getIterableValueType();
}

public function getPhpVersion(): PhpVersions
{
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID'));
if (!$this->hasExpressionType($versionExpr)->yes()) {
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
Copy link
Member

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.

}

return new PhpVersions($this->getType($versionExpr));
}

}
3 changes: 3 additions & 0 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PHPStan\Php\PhpVersions;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
Expand Down Expand Up @@ -136,4 +137,6 @@ public function filterByFalseyValue(Expr $expr): self;

public function isInFirstLevelStatement(): bool;

public function getPhpVersion(): PhpVersions;

}
26 changes: 26 additions & 0 deletions src/Php/PhpVersions.php
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
{

public function __construct(
private Type $phpVersions,
)
{
}

public function producesWarningForFinalPrivateMethods(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
}

}
9 changes: 1 addition & 8 deletions src/Rules/Methods/FinalPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
Expand All @@ -14,12 +13,6 @@
final class FinalPrivateMethodRule implements Rule
{

public function __construct(
private PhpVersion $phpVersion,
)
{
}

public function getNodeType(): string
{
return InClassMethodNode::class;
Expand All @@ -28,7 +21,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$method = $node->getMethodReflection();
if (!$this->phpVersion->producesWarningForFinalPrivateMethods()) {
if ($scope->getPhpVersion()->producesWarningForFinalPrivateMethods()->no()) {
return [];
}

Expand Down
68 changes: 68 additions & 0 deletions tests/PHPStan/Php/PhpVersionsTest.php
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),
]),
];
}

}
36 changes: 30 additions & 6 deletions tests/PHPStan/Rules/Methods/FinalPrivateMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,8 +41,35 @@ public function dataRule(): array
*/
public function testRule(int $phpVersion, array $errors): void
{
$this->phpVersionId = $phpVersion;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@staabm staabm Nov 20, 2024

Choose a reason for hiding this comment

The 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
{
}
}
}
Loading