Skip to content

Commit 203da46

Browse files
committed
move stmt key to property candidate value object
1 parent e1a225e commit 203da46

3 files changed

Lines changed: 32 additions & 17 deletions

File tree

rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ public function resolveFromClass(
4747
}
4848

4949
$propertyPromotionCandidates = [];
50-
foreach ($class->getProperties() as $property) {
51-
$propertyCount = count($property->props);
52-
if ($propertyCount !== 1) {
50+
foreach ($class->stmts as $classStmtPosition => $classStmt) {
51+
if (! $classStmt instanceof Property) {
5352
continue;
5453
}
5554

56-
$propertyPromotionCandidate = $this->matchPropertyPromotionCandidate($property, $constructClassMethod);
55+
if (count($classStmt->props) !== 1) {
56+
continue;
57+
}
58+
59+
$propertyPromotionCandidate = $this->matchPropertyPromotionCandidate(
60+
$classStmt,
61+
$constructClassMethod,
62+
$classStmtPosition
63+
);
5764
if (! $propertyPromotionCandidate instanceof PropertyPromotionCandidate) {
5865
continue;
5966
}
6067

61-
if (! $allowModelBasedClasses && $this->hasModelTypeCheck($property, ClassName::JMS_TYPE)) {
68+
if (! $allowModelBasedClasses && $this->hasModelTypeCheck($classStmt, ClassName::JMS_TYPE)) {
6269
continue;
6370
}
6471

@@ -80,7 +87,8 @@ private function hasModelTypeCheck(Class_|Property $node, string $modelType): bo
8087

8188
private function matchPropertyPromotionCandidate(
8289
Property $property,
83-
ClassMethod $constructClassMethod
90+
ClassMethod $constructClassMethod,
91+
int $propertyStmtPosition
8492
): ?PropertyPromotionCandidate {
8593
if ($property->flags === 0) {
8694
return null;
@@ -92,7 +100,7 @@ private function matchPropertyPromotionCandidate(
92100
$firstParamAsVariable = $this->resolveFirstParamUses($constructClassMethod);
93101

94102
// match property name to assign in constructor
95-
foreach ((array) $constructClassMethod->stmts as $stmt) {
103+
foreach ((array) $constructClassMethod->stmts as $assignStmtPosition => $stmt) {
96104
if (! $stmt instanceof Expression) {
97105
continue;
98106
}
@@ -127,7 +135,12 @@ private function matchPropertyPromotionCandidate(
127135
continue;
128136
}
129137

130-
return new PropertyPromotionCandidate($property, $matchedParam, $stmt);
138+
return new PropertyPromotionCandidate(
139+
$property,
140+
$matchedParam,
141+
$propertyStmtPosition,
142+
$assignStmtPosition
143+
);
131144
}
132145

133146
return null;

rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Rector\Naming\PropertyRenamer\PropertyPromotionRenamer;
2727
use Rector\Naming\VariableRenamer;
2828
use Rector\NodeAnalyzer\ParamAnalyzer;
29-
use Rector\NodeTypeResolver\Node\AttributeKey;
3029
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
3130
use Rector\Php80\DocBlock\PropertyPromotionDocBlockMerger;
3231
use Rector\Php80\Guard\MakePropertyPromotionGuard;
@@ -216,12 +215,10 @@ public function refactor(Node $node): ?Node
216215
$hasChanged = true;
217216

218217
// remove property from class
219-
$propertyStmtKey = $property->getAttribute(AttributeKey::STMT_KEY);
220-
unset($node->stmts[$propertyStmtKey]);
218+
unset($node->stmts[$promotionCandidate->getPropertyStmtPosition()]);
221219

222220
// remove assign in constructor
223-
$assignStmtPosition = $promotionCandidate->getStmtPosition();
224-
unset($constructClassMethod->stmts[$assignStmtPosition]);
221+
unset($constructClassMethod->stmts[$promotionCandidate->getConstructorAssignStmtPosition()]);
225222

226223
$oldParamName = $this->getName($param);
227224
$this->variableRenamer->renameVariableInFunctionLike($constructClassMethod, $oldParamName, $propertyName);

rules/Php80/ValueObject/PropertyPromotionCandidate.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
use PhpParser\Node\Stmt\Expression;
1010
use PhpParser\Node\Stmt\Property;
1111
use Rector\Exception\ShouldNotHappenException;
12-
use Rector\NodeTypeResolver\Node\AttributeKey;
1312

1413
final readonly class PropertyPromotionCandidate
1514
{
1615
public function __construct(
1716
private Property $property,
1817
private Param $param,
19-
private Expression $expression,
18+
private int $propertyStmtPosition,
19+
private int $constructorAssignStmtPosition,
2020
) {
2121
}
2222

@@ -45,8 +45,13 @@ public function getParamName(): string
4545
return $paramVar->name;
4646
}
4747

48-
public function getStmtPosition(): int
48+
public function getPropertyStmtPosition(): int
4949
{
50-
return $this->expression->getAttribute(AttributeKey::STMT_KEY);
50+
return $this->propertyStmtPosition;
51+
}
52+
53+
public function getConstructorAssignStmtPosition(): int
54+
{
55+
return $this->constructorAssignStmtPosition;
5156
}
5257
}

0 commit comments

Comments
 (0)