-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathInterpolatedStringRule.php
More file actions
68 lines (56 loc) · 1.72 KB
/
InterpolatedStringRule.php
File metadata and controls
68 lines (56 loc) · 1.72 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\String;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\InterpolatedString;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Parser\DeprecatedInterpolatedStringVisitor;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
/**
* @implements Rule<InterpolatedString>
*/
#[RegisteredRule(level: 0)]
final class InterpolatedStringRule implements Rule
{
public function __construct(
private PhpVersion $phpVersion,
)
{
}
public function getNodeType(): string
{
return InterpolatedString::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$this->phpVersion->deprecatesStringInterpolation()) {
return [];
}
if ($node->getAttribute(DeprecatedInterpolatedStringVisitor::ATTRIBUTE_NAME) !== true) {
return [];
}
foreach ($node->parts as $part) {
if (!$part instanceof Variable && !($part instanceof ArrayDimFetch && $part->var instanceof Variable)) {
continue;
}
if ($part instanceof ArrayDimFetch || (is_string($part->name))) {
$deprecatedMessage = 'Using ${var} in strings is deprecated in PHP 8.2. Use {$var} instead.';
} else {
$deprecatedMessage = 'Using ${expr} (variable variables) in strings is deprecated in PHP 8.2. Use {${expr}} instead.';
}
return [
RuleErrorBuilder::message($deprecatedMessage)
->identifier('interpolatedstring.deprecated')
->line($node->getStartLine())
->fixNode($node, static fn () => new InterpolatedString($node->parts, $node->getAttributes()))
->build(),
];
}
return [];
}
}