Skip to content

Commit 3f583cd

Browse files
Bump to PHPStan ^2.1.40 and utilize ClassConstantReflection->isFinalByKeyword() (#7911)
* Bump to PHPStan ^2.1.40 and utilize ClassConstantReflection->isFinalByKeyword() * [ci-review] Rector Rectify * add test to skip docblock @ final * add test to skip docblock @ final --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 23397d8 commit 3f583cd

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

build/target-repository/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"require": {
1111
"php": "^7.4|^8.0",
12-
"phpstan/phpstan": "^2.1.38"
12+
"phpstan/phpstan": "^2.1.40"
1313
},
1414
"autoload": {
1515
"files": [

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nikic/php-parser": "^5.7",
2424
"ondram/ci-detector": "^4.2",
2525
"phpstan/phpdoc-parser": "^2.3",
26-
"phpstan/phpstan": "^2.1.38",
26+
"phpstan/phpstan": "^2.1.40",
2727
"react/event-loop": "^1.6",
2828
"react/promise": "^3.3",
2929
"react/socket": "^1.17",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Fixture;
6+
7+
use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithFinalConstant;
8+
9+
final class SkipDocblockFinalConstant
10+
{
11+
public function run(ClassWithFinalConstant $classWithFinalConstant)
12+
{
13+
return $classWithFinalConstant::DOCBLOCK_FINAL;
14+
}
15+
}

rules-tests/CodeQuality/Rector/ClassConstFetch/VariableConstFetchToClassConstFetchRector/Source/ClassWithFinalConstant.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
class ClassWithFinalConstant
88
{
99
public final const NAME = 'SomeName';
10+
11+
/**
12+
* @final
13+
*/
14+
public const DOCBLOCK_FINAL = 'SomeDocblockFinal';
1015
}

rules/CodeQuality/Rector/ClassConstFetch/VariableConstFetchToClassConstFetchRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function refactor(Node $node): ?ClassConstFetch
109109
}
110110

111111
$constant = $classReflection->getConstant($constantName);
112-
if (! $constant->isFinal()) {
112+
if (! $constant->isFinalByKeyword()) {
113113
return null;
114114
}
115115
}

rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
use PHPStan\Reflection\ClassReflection;
1717
use Rector\Configuration\Parameter\FeatureFlags;
1818
use Rector\Enum\ObjectReference;
19-
use Rector\Php\PhpVersionProvider;
2019
use Rector\PHPStan\ScopeFetcher;
2120
use Rector\Rector\AbstractRector;
22-
use Rector\ValueObject\PhpVersionFeature;
23-
use ReflectionClassConstant;
2421
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2522
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2623

@@ -34,11 +31,6 @@
3431
*/
3532
final class ConvertStaticToSelfRector extends AbstractRector
3633
{
37-
public function __construct(
38-
private readonly PhpVersionProvider $phpVersionProvider
39-
) {
40-
}
41-
4234
public function getRuleDefinition(): RuleDefinition
4335
{
4436
return new RuleDefinition('Change `static::*` to `self::*` on final class or private static members', [
@@ -171,25 +163,8 @@ private function shouldSkip(
171163
}
172164

173165
if (! $isFinal) {
174-
// init
175-
$memberIsFinal = false;
176166
if ($reflection instanceof ClassConstantReflection) {
177-
// Get the native ReflectionClassConstant
178-
$declaringClass = $reflection->getDeclaringClass();
179-
$nativeReflectionClass = $declaringClass->getNativeReflection();
180-
$constantName = $reflection->getName();
181-
182-
if (
183-
// by feature config
184-
$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS) &&
185-
// ensure native ->isFinal() exists
186-
// @see https://3v4l.org/korKr#v8.0.11
187-
PHP_VERSION_ID >= PhpVersionFeature::FINAL_CLASS_CONSTANTS
188-
) {
189-
// PHP 8.1+
190-
$nativeReflection = $nativeReflectionClass->getReflectionConstant($constantName);
191-
$memberIsFinal = $nativeReflection instanceof ReflectionClassConstant && $nativeReflection->isFinal();
192-
}
167+
$memberIsFinal = $reflection->isFinalByKeyword();
193168
} else {
194169
$memberIsFinal = $reflection->isFinalByKeyword()
195170
->yes();

0 commit comments

Comments
 (0)