-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathPropertyFetchUsageFinder.php
More file actions
80 lines (63 loc) · 2.21 KB
/
PropertyFetchUsageFinder.php
File metadata and controls
80 lines (63 loc) · 2.21 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
80
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeFinder;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
final readonly class PropertyFetchUsageFinder
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
) {
}
/**
* @return PropertyFetch[]
*/
public function findInCallLikes(Class_ $class, string $propertyName): array
{
/** @var CallLike[] $callLikes */
$callLikes = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), CallLike::class);
$propertyFetchesInNewArgs = [];
foreach ($callLikes as $callLike) {
if ($callLike->isFirstClassCallable()) {
continue;
}
foreach ($callLike->getArgs() as $arg) {
if (! $arg->value instanceof PropertyFetch) {
continue;
}
if (! $this->nodeNameResolver->isName($arg->value->name, $propertyName)) {
continue;
}
$propertyFetchesInNewArgs[] = $arg->value;
}
}
return $propertyFetchesInNewArgs;
}
/**
* @return PropertyFetch[]
*/
public function findInArrays(Class_ $class, string $propertyName): array
{
/** @var Array_[] $arrays */
$arrays = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), Array_::class);
$propertyFetchesInArrays = [];
foreach ($arrays as $array) {
foreach ($array->items as $arrayItem) {
if (! $arrayItem->value instanceof PropertyFetch) {
continue;
}
$propertyFetch = $arrayItem->value;
if (! $this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
continue;
}
$propertyFetchesInArrays[] = $propertyFetch;
}
}
return $propertyFetchesInArrays;
}
}