-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathSetterAndGetterFinder.php
More file actions
79 lines (61 loc) · 2.14 KB
/
SetterAndGetterFinder.php
File metadata and controls
79 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Rector\Php84\NodeFinder;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\TypeDeclaration\NodeAnalyzer\ClassMethodAndPropertyAnalyzer;
use Rector\ValueObject\MethodName;
final readonly class SetterAndGetterFinder
{
public function __construct(
private ClassMethodAndPropertyAnalyzer $classMethodAndPropertyAnalyzer
) {
}
/**
* @return ClassMethod[]
*/
public function findGetterAndSetterClassMethods(Class_ $class, string $propertyName): array
{
$classMethods = [];
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if ($constructClassMethod instanceof ClassMethod) {
foreach ($constructClassMethod->params as $param) {
if ($param->isReadonly()) {
return [];
}
}
}
$getterClassMethod = $this->findGetterClassMethod($class, $propertyName);
if ($getterClassMethod instanceof ClassMethod) {
$classMethods[] = $getterClassMethod;
}
$setterClassMethod = $this->findSetterClassMethod($class, $propertyName);
if ($setterClassMethod instanceof ClassMethod) {
$classMethods[] = $setterClassMethod;
}
return $classMethods;
}
public function findGetterClassMethod(Class_ $class, string $propertyName): ?ClassMethod
{
foreach ($class->getMethods() as $classMethod) {
if (! $this->classMethodAndPropertyAnalyzer->hasPropertyFetchReturn($classMethod, $propertyName)) {
continue;
}
return $classMethod;
}
return null;
}
public function findSetterClassMethod(Class_ $class, string $propertyName): ?ClassMethod
{
foreach ($class->getMethods() as $classMethod) {
if ($classMethod->isMagic()) {
continue;
}
if (! $this->classMethodAndPropertyAnalyzer->hasOnlyPropertyAssign($classMethod, $propertyName)) {
continue;
}
return $classMethod;
}
return null;
}
}