Skip to content

Commit 8b1ace2

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

3 files changed

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

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)