Skip to content

Commit 014b976

Browse files
authored
[CodeQuality] Skip used by other property hooks on InlineConstructorDefaultToPropertyRector (#7920)
* [CodeQuality] Skip used by other property hooks on InlineConstructorDefaultToPropertyRector * [CodeQuality] Skip used by other property hooks on InlineConstructorDefaultToPropertyRector * clean up loop
1 parent a22afd1 commit 014b976

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-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: 28 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,26 @@ private function matchAssignedLocalPropertyName(Assign $assign): ?string
150154
return $propertyName;
151155
}
152156

157+
private function isFoundInAnyPropertyHooks(Class_ $class, string $propertyName): bool
158+
{
159+
$propertyHooks = array_reduce(
160+
$class->getProperties(),
161+
static fn (array $hooks, Property $property): array => [...$hooks, ...$property->hooks],
162+
[]
163+
);
164+
165+
return (bool) $this->betterNodeFinder->findFirst($propertyHooks, function (Node $subNode) use (
166+
$class,
167+
$propertyName
168+
): bool {
169+
if (! $subNode instanceof PropertyFetch) {
170+
return false;
171+
}
172+
173+
return $this->propertyFetchFinder->isLocalPropertyFetchByName($subNode, $class, $propertyName);
174+
});
175+
}
176+
153177
private function refactorProperty(
154178
Class_ $class,
155179
string $propertyName,
@@ -161,6 +185,10 @@ private function refactorProperty(
161185
return false;
162186
}
163187

188+
if ($this->isFoundInAnyPropertyHooks($class, $propertyName)) {
189+
return false;
190+
}
191+
164192
foreach ($class->stmts as $classStmt) {
165193
if (! $classStmt instanceof Property) {
166194
continue;

0 commit comments

Comments
 (0)