-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathExprUsedAsStringVisitor.php
More file actions
108 lines (99 loc) · 3.61 KB
/
Copy pathExprUsedAsStringVisitor.php
File metadata and controls
108 lines (99 loc) · 3.61 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use Override;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\Print_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\DependencyInjection\AutowiredService;
/**
* Marks expressions whose value is used as a string so that NodeScopeResolver
* can emit an {@see \PHPStan\Node\UsedAsStringNode} for them.
*
* Nested concatenations and interpolation parts are "claimed" by the enclosing
* concatenation so that a concatenation chain is reported once for the whole
* expression instead of once per operand.
*/
#[AutowiredService]
final class ExprUsedAsStringVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_NAME = 'isExprUsedAsString';
private const CLAIMED_ATTRIBUTE_NAME = 'claimedByStringConcat';
#[Override]
public function enterNode(Node $node): ?Node
{
if ($node instanceof Echo_) {
foreach ($node->exprs as $expr) {
$expr->setAttribute(self::ATTRIBUTE_NAME, true);
}
} elseif ($node instanceof Print_) {
$node->expr->setAttribute(self::ATTRIBUTE_NAME, true);
} elseif ($node instanceof Cast\String_) {
$node->expr->setAttribute(self::ATTRIBUTE_NAME, true);
} elseif ($node instanceof AssignOp\Concat) {
$node->setAttribute(self::ATTRIBUTE_NAME, true);
$node->expr->setAttribute(self::CLAIMED_ATTRIBUTE_NAME, true);
} elseif ($node instanceof Concat) {
if ($node->getAttribute(self::CLAIMED_ATTRIBUTE_NAME) !== true) {
$node->setAttribute(self::ATTRIBUTE_NAME, true);
}
$node->left->setAttribute(self::CLAIMED_ATTRIBUTE_NAME, true);
$node->right->setAttribute(self::CLAIMED_ATTRIBUTE_NAME, true);
} elseif ($node instanceof InterpolatedString) {
if ($node->getAttribute(self::CLAIMED_ATTRIBUTE_NAME) !== true) {
$node->setAttribute(self::ATTRIBUTE_NAME, true);
}
foreach ($node->parts as $part) {
if (!$part instanceof Expr) {
continue;
}
$part->setAttribute(self::CLAIMED_ATTRIBUTE_NAME, true);
}
} elseif (
$node instanceof PropertyFetch
|| $node instanceof NullsafePropertyFetch
|| $node instanceof MethodCall
|| $node instanceof NullsafeMethodCall
|| $node instanceof StaticPropertyFetch
|| $node instanceof StaticCall
|| $node instanceof ClassConstFetch
) {
if ($node->name instanceof Expr) {
$node->name->setAttribute(self::ATTRIBUTE_NAME, true);
}
} elseif ($node instanceof Variable) {
if ($node->name instanceof Expr) {
$node->name->setAttribute(self::ATTRIBUTE_NAME, true);
}
}
return null;
}
/**
* Whether the expression already produces its own UsedAsStringNode, so that an
* enclosing context (e.g. a string-typed assignment target) must not report it a
* second time. True for marked nodes (concatenation, interpolation, echo/print
* argument, …), the operand-emitting `(string)` cast and nested assignments.
*/
public static function isAlreadyUsedAsStringSite(Expr $expr): bool
{
return $expr->getAttribute(self::ATTRIBUTE_NAME) === true
|| $expr instanceof Cast\String_
|| $expr instanceof Assign
|| $expr instanceof AssignRef;
}
}