Skip to content

Commit d02f407

Browse files
committed
[CodeQuality] Skip used by other property hooks on InlineConstructorDefaultToPropertyRector
1 parent 64ba1b3 commit d02f407

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Fixture;
4+
5+
final class SkipUsedByPropertyHook
6+
{
7+
private bool $isConstructed = false;
8+
9+
public mixed $value = null {
10+
get {
11+
return $this->value;
12+
}
13+
set {
14+
if (!$this->isConstructed) {
15+
$this->value = $value;
16+
return;
17+
}
18+
19+
if (!is_string($value)) {
20+
throw new \TypeError('$value must be a string when it is already constructed!');
21+
}
22+
23+
$this->value = $value;
24+
}
25+
}
26+
27+
public function __construct(
28+
mixed $value = null,
29+
) {
30+
$this->value = $value;
31+
$this->isConstructed = true;
32+
}
33+
}

rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PhpParser\Node\Stmt\Property;
1616
use Rector\NodeAnalyzer\ExprAnalyzer;
1717
use Rector\NodeTypeResolver\Node\AttributeKey;
18+
use Rector\PhpParser\Node\BetterNodeFinder;
19+
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
1820
use Rector\Rector\AbstractRector;
1921
use Rector\ValueObject\MethodName;
2022
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -27,6 +29,8 @@ final class InlineConstructorDefaultToPropertyRector extends AbstractRector
2729
{
2830
public function __construct(
2931
private readonly ExprAnalyzer $exprAnalyzer,
32+
private readonly BetterNodeFinder $betterNodeFinder,
33+
private readonly PropertyFetchFinder $propertyFetchFinder
3034
) {
3135
}
3236

@@ -150,6 +154,32 @@ private function matchAssignedLocalPropertyName(Assign $assign): ?string
150154
return $propertyName;
151155
}
152156

157+
private function isFoundInAnyPropertyHooks(Class_ $class, string $propertyName): bool
158+
{
159+
foreach ($class->getProperties() as $property) {
160+
if ($property->hooks === []) {
161+
continue;
162+
}
163+
164+
$isFoundInPropertyAnyHooks = $this->betterNodeFinder->findFirst($property->hooks, function (Node $subNode) use (
165+
$class,
166+
$propertyName
167+
): bool {
168+
if (! $subNode instanceof PropertyFetch) {
169+
return false;
170+
}
171+
172+
return $this->propertyFetchFinder->isLocalPropertyFetchByName($subNode, $class, $propertyName);
173+
});
174+
175+
if ($isFoundInPropertyAnyHooks instanceof Node) {
176+
return true;
177+
}
178+
}
179+
180+
return false;
181+
}
182+
153183
private function refactorProperty(
154184
Class_ $class,
155185
string $propertyName,
@@ -161,6 +191,10 @@ private function refactorProperty(
161191
return false;
162192
}
163193

194+
if ($this->isFoundInAnyPropertyHooks($class, $propertyName)) {
195+
return false;
196+
}
197+
164198
foreach ($class->stmts as $classStmt) {
165199
if (! $classStmt instanceof Property) {
166200
continue;

0 commit comments

Comments
 (0)