Skip to content

Commit cef40af

Browse files
committed
move SetterAndGetterFinder from doctrine to core, as useful in many extensions
1 parent e34e37a commit cef40af

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
99

1010
return RectorConfig::configure()
11+
->withTreatClassesAsFinal()
1112
->withPreparedSets(
1213
deadCode: true,
1314
codeQuality: true,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Rector\Php84\NodeFinder;
4+
5+
use PhpParser\Node\Stmt\Class_;
6+
use PhpParser\Node\Stmt\ClassMethod;
7+
use Rector\TypeDeclaration\NodeAnalyzer\ClassMethodAndPropertyAnalyzer;
8+
9+
final readonly class SetterAndGetterFinder
10+
{
11+
public function __construct(
12+
private ClassMethodAndPropertyAnalyzer $classMethodAndPropertyAnalyzer
13+
) {
14+
}
15+
16+
/**
17+
* @return ClassMethod[]
18+
*/
19+
public function findGetterAndSetterClassMethods(Class_ $class, string $propertyName): array
20+
{
21+
$classMethods = [];
22+
23+
$getterClassMethod = $this->findGetterClassMethod($class, $propertyName);
24+
if ($getterClassMethod instanceof ClassMethod) {
25+
$classMethods[] = $getterClassMethod;
26+
}
27+
28+
$setterClassMethod = $this->findSetterClassMethod($class, $propertyName);
29+
if ($setterClassMethod instanceof ClassMethod) {
30+
$classMethods[] = $setterClassMethod;
31+
}
32+
33+
return $classMethods;
34+
}
35+
36+
public function findGetterClassMethod(Class_ $class, string $propertyName): ?ClassMethod
37+
{
38+
foreach ($class->getMethods() as $classMethod) {
39+
if (! $this->classMethodAndPropertyAnalyzer->hasPropertyFetchReturn($classMethod, $propertyName)) {
40+
continue;
41+
}
42+
43+
return $classMethod;
44+
}
45+
46+
return null;
47+
}
48+
49+
public function findSetterClassMethod(Class_ $class, string $propertyName): ?ClassMethod
50+
{
51+
foreach ($class->getMethods() as $classMethod) {
52+
if (! $this->classMethodAndPropertyAnalyzer->hasOnlyPropertyAssign($classMethod, $propertyName)) {
53+
continue;
54+
}
55+
56+
return $classMethod;
57+
}
58+
59+
return null;
60+
}
61+
}

rules/Php84/Rector/Class_/PropertyHookRector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use PhpParser\Node\Stmt\Class_;
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use Rector\Configuration\Parameter\FeatureFlags;
12-
use Rector\Doctrine\CodeQuality\Helper\SetterGetterFinder;
1312
use Rector\Php84\NodeFactory\PropertyHookFactory;
13+
use Rector\Php84\NodeFinder\SetterAndGetterFinder;
1414
use Rector\Rector\AbstractRector;
1515
use Rector\ValueObject\PhpVersionFeature;
1616
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
@@ -24,7 +24,7 @@
2424
final class PropertyHookRector extends AbstractRector implements MinPhpVersionInterface
2525
{
2626
public function __construct(
27-
private readonly SetterGetterFinder $setterGetterFinder,
27+
private readonly SetterAndGetterFinder $setterAndGetterFinder,
2828
private readonly PropertyHookFactory $propertyHookFactory,
2929
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
3030
) {
@@ -91,7 +91,10 @@ public function refactor(Node $node): ?Node
9191
foreach ($node->getProperties() as $property) {
9292
$propertyName = $this->getName($property);
9393

94-
$candidateClassMethods = $this->setterGetterFinder->findGetterAndSetterClassMethods($node, $propertyName);
94+
$candidateClassMethods = $this->setterAndGetterFinder->findGetterAndSetterClassMethods(
95+
$node,
96+
$propertyName
97+
);
9598

9699
foreach ($candidateClassMethods as $candidateClassMethod) {
97100
if (count((array) $candidateClassMethod->stmts) !== 1) {

0 commit comments

Comments
 (0)