-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathEchoDeprecatedToStringRule.php
107 lines (86 loc) · 2.26 KB
/
EchoDeprecatedToStringRule.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Deprecations;
use PhpParser\Node;
use PhpParser\Node\Stmt\Echo_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
/**
* @implements \PHPStan\Rules\Rule<Echo_>
*/
class EchoDeprecatedToStringRule implements \PHPStan\Rules\Rule
{
/** @var RuleLevelHelper */
private $ruleLevelHelper;
public function __construct(RuleLevelHelper $ruleLevelHelper)
{
$this->ruleLevelHelper = $ruleLevelHelper;
}
public function getNodeType(): string
{
return Echo_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
return [];
}
$messages = [];
foreach ($node->exprs as $key => $expr) {
if ($expr instanceof Node\Expr\Variable) {
$message = $this->checkExpr($expr, $scope);
if ($message) {
$messages[] = $message;
}
} elseif ($expr instanceof Node\Expr\BinaryOp\Concat) {
$message = $this->checkExpr($expr->left, $scope);
if ($message) {
$messages[] = $message;
}
$message = $this->checkExpr($expr->right, $scope);
if ($message) {
$messages[] = $message;
}
}
}
return $messages;
}
private function checkExpr(Node\Expr $expr, Scope $scope): ?string
{
$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$expr,
'',
static function (Type $type): bool {
return !$type->toString() instanceof ErrorType;
}
)->getType();
if (!$type instanceof ObjectType) {
return null;
}
$classReflection = $type->getClassReflection();
if ($classReflection === null) {
return null;
}
$methodReflection = $classReflection->getNativeMethod('__toString');
if (!$methodReflection->isDeprecated()->yes()) {
return null;
}
$description = $methodReflection->getDeprecatedDescription();
if ($description === null) {
return sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
}
return sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$description
);
}
}