Repository navigation
Expand file tree
/
Copy pathPropertyWriteonlyAnalyzer.php
More file actions
75 lines (63 loc) · 2.34 KB
/
Copy pathPropertyWriteonlyAnalyzer.php
File metadata and controls
75 lines (63 loc) · 2.34 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
<?php
declare(strict_types=1);
namespace Rector\DeadCode\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
final readonly class PropertyWriteonlyAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver
) {
}
public function hasClassDynamicPropertyNames(Class_ $class): bool
{
return (bool) $this->betterNodeFinder->findFirst($class, function (Node $node) : bool {
if ($node instanceof FuncCall && $this->nodeNameResolver->isName(
$node,
'get_object_vars'
) && ! $node->isFirstClassCallable()) {
$firstArg = $node->getArgs()[0] ?? null;
if ($firstArg instanceof Arg && $firstArg->value instanceof Variable && $firstArg->value->name === 'this') {
return true;
}
}
if (! $node instanceof PropertyFetch && ! $node instanceof NullsafePropertyFetch) {
return false;
}
// has dynamic name - could be anything
return $node->name instanceof Expr;
});
}
/**
* The property fetches are always only assigned to, nothing else
*
* @param array<PropertyFetch|StaticPropertyFetch|NullsafePropertyFetch> $propertyFetches
*/
public function arePropertyFetchesExclusivelyBeingAssignedTo(array $propertyFetches): bool
{
foreach ($propertyFetches as $propertyFetch) {
if ((bool) $propertyFetch->getAttribute(AttributeKey::IS_MULTI_ASSIGN, false)) {
return false;
}
if ((bool) $propertyFetch->getAttribute(AttributeKey::IS_ASSIGNED_TO, false)) {
return false;
}
if ((bool) $propertyFetch->getAttribute(AttributeKey::IS_BEING_ASSIGNED, false)) {
continue;
}
return false;
}
return true;
}
}