Skip to content

Commit 3fd1741

Browse files
committed
Updated Rector to commit 6a3101644b93c439234f00d00a4bde62467e3b75
rectorphp/rector-src@6a31016 Add AddOverrideAttributeToOverriddenPropertiesRector for PHP 8.5 (#7880)
1 parent 42b41f2 commit 3fd1741

6 files changed

Lines changed: 167 additions & 3 deletions

File tree

config/set/php85.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
1818
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
1919
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
20+
use Rector\Php85\Rector\Property\AddOverrideAttributeToOverriddenPropertiesRector;
2021
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
2122
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
2223
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
@@ -31,7 +32,7 @@
3132
use Rector\Renaming\ValueObject\RenameCast;
3233
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
3334
return static function (RectorConfig $rectorConfig): void {
34-
$rectorConfig->rules([ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class, NullDebugInfoReturnRector::class, ConstAndTraitDeprecatedAttributeRector::class, ColonAfterSwitchCaseRector::class, ArrayKeyExistsNullToEmptyStringRector::class, ChrArgModuloRector::class, SleepToSerializeRector::class, OrdSingleByteRector::class, WakeupToUnserializeRector::class, ShellExecFunctionCallOverBackticksRector::class]);
35+
$rectorConfig->rules([ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class, NullDebugInfoReturnRector::class, ConstAndTraitDeprecatedAttributeRector::class, ColonAfterSwitchCaseRector::class, ArrayKeyExistsNullToEmptyStringRector::class, ChrArgModuloRector::class, SleepToSerializeRector::class, OrdSingleByteRector::class, WakeupToUnserializeRector::class, ShellExecFunctionCallOverBackticksRector::class, AddOverrideAttributeToOverriddenPropertiesRector::class]);
3536
$rectorConfig->ruleWithConfiguration(RemoveFuncCallArgRector::class, [
3637
// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_key_length_parameter_of_openssl_pkey_derive
3738
new RemoveFuncCallArg('openssl_pkey_derive', 2),
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Php85\Rector\Property;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Attribute;
8+
use PhpParser\Node\AttributeGroup;
9+
use PhpParser\Node\Name\FullyQualified;
10+
use PhpParser\Node\Stmt\Class_;
11+
use PhpParser\Node\Stmt\Property;
12+
use PHPStan\Reflection\ClassReflection;
13+
use PHPStan\Reflection\ReflectionProvider;
14+
use Rector\NodeAnalyzer\ClassAnalyzer;
15+
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
16+
use Rector\Rector\AbstractRector;
17+
use Rector\ValueObject\PhpVersionFeature;
18+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
/**
22+
* @see https://wiki.php.net/rfc/override_properties
23+
*
24+
* @see \Rector\Tests\Php85\Rector\Property\AddOverrideAttributeToOverriddenPropertiesRector\AddOverrideAttributeToOverriddenPropertiesRectorTest
25+
*/
26+
final class AddOverrideAttributeToOverriddenPropertiesRector extends AbstractRector implements MinPhpVersionInterface
27+
{
28+
/**
29+
* @readonly
30+
*/
31+
private ReflectionProvider $reflectionProvider;
32+
/**
33+
* @readonly
34+
*/
35+
private ClassAnalyzer $classAnalyzer;
36+
/**
37+
* @readonly
38+
*/
39+
private PhpAttributeAnalyzer $phpAttributeAnalyzer;
40+
/**
41+
* @var string
42+
*/
43+
private const OVERRIDE_CLASS = 'Override';
44+
private bool $hasChanged = \false;
45+
public function __construct(ReflectionProvider $reflectionProvider, ClassAnalyzer $classAnalyzer, PhpAttributeAnalyzer $phpAttributeAnalyzer)
46+
{
47+
$this->reflectionProvider = $reflectionProvider;
48+
$this->classAnalyzer = $classAnalyzer;
49+
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
50+
}
51+
public function getRuleDefinition(): RuleDefinition
52+
{
53+
return new RuleDefinition('Add override attribute to overridden properties', [new CodeSample(<<<'CODE_SAMPLE'
54+
class ParentClass
55+
{
56+
public string $name;
57+
}
58+
59+
final class ChildClass extends ParentClass
60+
{
61+
public string $name;
62+
}
63+
CODE_SAMPLE
64+
, <<<'CODE_SAMPLE'
65+
class ParentClass
66+
{
67+
public string $name;
68+
}
69+
70+
final class ChildClass extends ParentClass
71+
{
72+
#[\Override]
73+
public string $name;
74+
}
75+
CODE_SAMPLE
76+
)]);
77+
}
78+
/**
79+
* @return array<class-string<Node>>
80+
*/
81+
public function getNodeTypes(): array
82+
{
83+
return [Class_::class];
84+
}
85+
public function provideMinPhpVersion(): int
86+
{
87+
return PhpVersionFeature::OVERRIDE_ATTRIBUTE_ON_PROPERTIES;
88+
}
89+
/**
90+
* @param Class_ $node
91+
*/
92+
public function refactor(Node $node): ?Node
93+
{
94+
if ($this->classAnalyzer->isAnonymousClass($node)) {
95+
return null;
96+
}
97+
$className = (string) $this->getName($node);
98+
if (!$this->reflectionProvider->hasClass($className)) {
99+
return null;
100+
}
101+
$classReflection = $this->reflectionProvider->getClass($className);
102+
$parentClassReflections = $classReflection->getParents();
103+
if ($parentClassReflections === []) {
104+
return null;
105+
}
106+
$this->hasChanged = \false;
107+
foreach ($node->getProperties() as $property) {
108+
$this->processProperty($property, $parentClassReflections);
109+
}
110+
if ($this->hasChanged) {
111+
return $node;
112+
}
113+
return null;
114+
}
115+
/**
116+
* @param ClassReflection[] $parentClassReflections
117+
*/
118+
private function processProperty(Property $property, array $parentClassReflections): void
119+
{
120+
if ($this->shouldSkipProperty($property)) {
121+
return;
122+
}
123+
foreach ($property->props as $propertyProperty) {
124+
$propertyName = $this->getName($propertyProperty);
125+
if ($propertyName === null) {
126+
continue;
127+
}
128+
if ($this->isPropertyOverridden($propertyName, $parentClassReflections)) {
129+
$property->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified(self::OVERRIDE_CLASS))]);
130+
$this->hasChanged = \true;
131+
return;
132+
}
133+
}
134+
}
135+
private function shouldSkipProperty(Property $property): bool
136+
{
137+
if ($property->isPrivate()) {
138+
return \true;
139+
}
140+
return $this->phpAttributeAnalyzer->hasPhpAttribute($property, self::OVERRIDE_CLASS);
141+
}
142+
/**
143+
* @param ClassReflection[] $parentClassReflections
144+
*/
145+
private function isPropertyOverridden(string $propertyName, array $parentClassReflections): bool
146+
{
147+
foreach ($parentClassReflections as $parentClassReflection) {
148+
if (!$parentClassReflection->hasNativeProperty($propertyName)) {
149+
continue;
150+
}
151+
$parentProperty = $parentClassReflection->getNativeProperty($propertyName);
152+
return !$parentProperty->isPrivate();
153+
}
154+
return \false;
155+
}
156+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '57b37ca42ccb8d5754e0cac786e4c60ef7f044d9';
22+
public const PACKAGE_VERSION = '6a3101644b93c439234f00d00a4bde62467e3b75';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-02-10 11:44:26';
27+
public const RELEASE_DATE = '2026-02-10 12:09:12';
2828
/**
2929
* @var int
3030
*/

src/ValueObject/PhpVersionFeature.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,9 @@ final class PhpVersionFeature
689689
* @var int
690690
*/
691691
public const PIPE_OPERATOER = \Rector\ValueObject\PhpVersion::PHP_85;
692+
/**
693+
* @see https://wiki.php.net/rfc/override_properties
694+
* @var int
695+
*/
696+
public const OVERRIDE_ATTRIBUTE_ON_PROPERTIES = \Rector\ValueObject\PhpVersion::PHP_85;
692697
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,7 @@
22872287
'Rector\\Php85\\Rector\\FuncCall\\ChrArgModuloRector' => $baseDir . '/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php',
22882288
'Rector\\Php85\\Rector\\FuncCall\\OrdSingleByteRector' => $baseDir . '/rules/Php85/Rector/FuncCall/OrdSingleByteRector.php',
22892289
'Rector\\Php85\\Rector\\FuncCall\\RemoveFinfoBufferContextArgRector' => $baseDir . '/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php',
2290+
'Rector\\Php85\\Rector\\Property\\AddOverrideAttributeToOverriddenPropertiesRector' => $baseDir . '/rules/Php85/Rector/Property/AddOverrideAttributeToOverriddenPropertiesRector.php',
22902291
'Rector\\Php85\\Rector\\ShellExec\\ShellExecFunctionCallOverBackticksRector' => $baseDir . '/rules/Php85/Rector/ShellExec/ShellExecFunctionCallOverBackticksRector.php',
22912292
'Rector\\Php85\\Rector\\StmtsAwareInterface\\SequentialAssignmentsToPipeOperatorRector' => $baseDir . '/rules/Php85/Rector/StmtsAwareInterface/SequentialAssignmentsToPipeOperatorRector.php',
22922293
'Rector\\Php85\\Rector\\Switch_\\ColonAfterSwitchCaseRector' => $baseDir . '/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,7 @@ class ComposerStaticInit6374fd21a3f525a40760d79e3596b56b
25472547
'Rector\\Php85\\Rector\\FuncCall\\ChrArgModuloRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/FuncCall/ChrArgModuloRector.php',
25482548
'Rector\\Php85\\Rector\\FuncCall\\OrdSingleByteRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/FuncCall/OrdSingleByteRector.php',
25492549
'Rector\\Php85\\Rector\\FuncCall\\RemoveFinfoBufferContextArgRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php',
2550+
'Rector\\Php85\\Rector\\Property\\AddOverrideAttributeToOverriddenPropertiesRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/Property/AddOverrideAttributeToOverriddenPropertiesRector.php',
25502551
'Rector\\Php85\\Rector\\ShellExec\\ShellExecFunctionCallOverBackticksRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/ShellExec/ShellExecFunctionCallOverBackticksRector.php',
25512552
'Rector\\Php85\\Rector\\StmtsAwareInterface\\SequentialAssignmentsToPipeOperatorRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/StmtsAwareInterface/SequentialAssignmentsToPipeOperatorRector.php',
25522553
'Rector\\Php85\\Rector\\Switch_\\ColonAfterSwitchCaseRector' => __DIR__ . '/../..' . '/rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php',

0 commit comments

Comments
 (0)