Skip to content

Commit 286fdb5

Browse files
authored
[Privatization] Skip Symfony Command protected static property in PrivatizeFinalClassPropertyRector (#8201)
1 parent 29c7d78 commit 286fdb5

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector\Fixture;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
7+
final class SendWinnerEmailCommand extends Command
8+
{
9+
protected static $defaultDescription = 'Send winner email variant to remaining contacts';
10+
}

‎rules/Privatization/Guard/ParentPropertyLookupGuard.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
final readonly class ParentPropertyLookupGuard
2323
{
24+
/**
25+
* Symfony Console Command reserved static properties, read by the parent class via reflection
26+
* even when not declared on the parent class itself
27+
* @see https://github.com/symfony/console/blob/7.1/Command/Command.php
28+
*
29+
* @var string[]
30+
*/
31+
private const SYMFONY_COMMAND_RESERVED_PROPERTY_NAMES = ['defaultName', 'defaultDescription'];
32+
2433
public function __construct(
2534
private BetterNodeFinder $betterNodeFinder,
2635
private NodeNameResolver $nodeNameResolver,
@@ -49,6 +58,10 @@ public function isLegal(Property|string $property, ?ClassReflection $classReflec
4958
return false;
5059
}
5160

61+
if ($this->isSymfonyCommandReservedProperty($classReflection, $propertyName)) {
62+
return false;
63+
}
64+
5265
$parentClassName = $this->classReflectionAnalyzer->resolveParentClassName($classReflection);
5366
if ($parentClassName === null) {
5467
return true;
@@ -65,6 +78,15 @@ public function isLegal(Property|string $property, ?ClassReflection $classReflec
6578
return $this->isGuardedByParents($parentClassReflections, $propertyName, $className);
6679
}
6780

81+
private function isSymfonyCommandReservedProperty(ClassReflection $classReflection, string $propertyName): bool
82+
{
83+
if (! in_array($propertyName, self::SYMFONY_COMMAND_RESERVED_PROPERTY_NAMES, true)) {
84+
return false;
85+
}
86+
87+
return $classReflection->is('Symfony\Component\Console\Command\Command');
88+
}
89+
6890
private function isFoundInParentClassMethods(
6991
ClassReflection $parentClassReflection,
7092
string $propertyName,

0 commit comments

Comments
 (0)