Skip to content

Commit 91d644b

Browse files
[Naming] Skip protected property on non-final class on RenamePropertyToMatchTypeRector (#6872)
* [Naming] Skip protected property on non-final class on RenamePropertyToMatchTypeRector * [ci-review] Rector Rectify * cleanup fixture * Fix --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 1f1cb35 commit 91d644b

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;
4+
5+
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\GitWrapper;
6+
7+
class SkipProtectedPropertyOnNonFinalClass
8+
{
9+
/**
10+
* @var \Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\GitWrapper
11+
*/
12+
protected $wrapper;
13+
14+
public function __construct(GitWrapper $wrapper)
15+
{
16+
$this->wrapper = $wrapper;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;
4+
5+
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\GitWrapper;
6+
7+
class SkipProtectedPropertyPromotionOnNonFinalClass
8+
{
9+
public function __construct(protected GitWrapper $wrapper)
10+
{
11+
}
12+
}

rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Stmt\Class_;
1111
use PhpParser\Node\Stmt\ClassLike;
1212
use PhpParser\Node\Stmt\ClassMethod;
13-
use PhpParser\Node\Stmt\Interface_;
1413
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
1514
use PHPStan\Reflection\ClassReflection;
1615
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@@ -45,20 +44,20 @@ public function __construct(
4544
) {
4645
}
4746

48-
public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
47+
public function renamePropertyPromotion(Class_ $class): bool
4948
{
5049
$hasChanged = false;
5150

5251
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::PROPERTY_PROMOTION)) {
5352
return false;
5453
}
5554

56-
$constructClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
55+
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
5756
if (! $constructClassMethod instanceof ClassMethod) {
5857
return false;
5958
}
6059

61-
$classReflection = $this->reflectionResolver->resolveClassReflection($classLike);
60+
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
6261
if (! $classReflection instanceof ClassReflection) {
6362
return false;
6463
}
@@ -76,6 +75,10 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
7675
continue;
7776
}
7877

78+
if (! $class->isFinal() && $param->isProtected()) {
79+
continue;
80+
}
81+
7982
// promoted property
8083
$desiredPropertyName = $this->matchParamTypeExpectedNameResolver->resolve($param);
8184
if ($desiredPropertyName === null) {
@@ -95,7 +98,7 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
9598
continue;
9699
}
97100

98-
$this->renameParamVarNameAndVariableUsage($classLike, $constructClassMethod, $desiredPropertyName, $param);
101+
$this->renameParamVarNameAndVariableUsage($class, $constructClassMethod, $desiredPropertyName, $param);
99102
$hasChanged = true;
100103
}
101104

rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Name;
99
use PhpParser\Node\Stmt\Class_;
10-
use PhpParser\Node\Stmt\ClassLike;
11-
use PhpParser\Node\Stmt\Interface_;
1210
use PhpParser\Node\Stmt\Property;
1311
use PHPStan\Type\ObjectType;
1412
use Rector\Enum\ClassName;
@@ -81,11 +79,11 @@ public function __construct(EntityManager $entityManager)
8179
*/
8280
public function getNodeTypes(): array
8381
{
84-
return [Class_::class, Interface_::class];
82+
return [Class_::class];
8583
}
8684

8785
/**
88-
* @param Class_|Interface_ $node
86+
* @param Class_ $node
8987
*/
9088
public function refactor(Node $node): ?Node
9189
{
@@ -105,21 +103,25 @@ public function refactor(Node $node): ?Node
105103
return null;
106104
}
107105

108-
private function refactorClassProperties(ClassLike $classLike): void
106+
private function refactorClassProperties(Class_ $class): void
109107
{
110-
foreach ($classLike->getProperties() as $property) {
108+
foreach ($class->getProperties() as $property) {
111109
// skip public properties, as they can be used in external code
112110
if ($property->isPublic()) {
113111
continue;
114112
}
115113

116-
$expectedPropertyName = $this->matchPropertyTypeExpectedNameResolver->resolve($property, $classLike);
114+
if (!$class->isFinal() && $property->isProtected()) {
115+
continue;
116+
}
117+
118+
$expectedPropertyName = $this->matchPropertyTypeExpectedNameResolver->resolve($property, $class);
117119
if ($expectedPropertyName === null) {
118120
continue;
119121
}
120122

121123
$propertyRename = $this->propertyRenameFactory->createFromExpectedName(
122-
$classLike,
124+
$class,
123125
$property,
124126
$expectedPropertyName
125127
);

rules/Naming/ValueObjectFactory/PropertyRenameFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Rector\Naming\ValueObjectFactory;
66

7-
use PhpParser\Node\Stmt\ClassLike;
7+
use PhpParser\Node\Stmt\Class_;
88
use PhpParser\Node\Stmt\Property;
99
use Rector\Naming\ValueObject\PropertyRename;
1010
use Rector\NodeNameResolver\NodeNameResolver;
@@ -18,19 +18,19 @@ public function __construct(
1818
}
1919

2020
public function createFromExpectedName(
21-
ClassLike $classLike,
21+
Class_ $class,
2222
Property $property,
2323
string $expectedName
2424
): ?PropertyRename {
2525
$currentName = $this->nodeNameResolver->getName($property);
26-
$className = (string) $this->nodeNameResolver->getName($classLike);
26+
$className = (string) $this->nodeNameResolver->getName($class);
2727

2828
try {
2929
return new PropertyRename(
3030
$property,
3131
$expectedName,
3232
$currentName,
33-
$classLike,
33+
$class,
3434
$className,
3535
$property->props[0]
3636
);

0 commit comments

Comments
 (0)