-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathEchoDeprecatedBinaryOpToStringRule.php
88 lines (72 loc) · 2.1 KB
/
EchoDeprecatedBinaryOpToStringRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Deprecations;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Type\TypeUtils;
/**
* @implements \PHPStan\Rules\Rule<BinaryOp>
*/
class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule
{
/** @var Broker */
private $broker;
public function __construct(Broker $broker)
{
$this->broker = $broker;
}
public function getNodeType(): string
{
return BinaryOp::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
return [];
}
$messages = [];
$message = $this->checkExpr($node->left, $scope);
if ($message) {
$messages[] = $message;
}
$message = $this->checkExpr($node->right, $scope);
if ($message) {
$messages[] = $message;
}
return $messages;
}
private function checkExpr(Node\Expr $node, Scope $scope): ?string
{
$methodCalledOnType = $scope->getType($node);
$referencedClasses = TypeUtils::getDirectClassNames($methodCalledOnType);
foreach ($referencedClasses as $referencedClass) {
try {
$classReflection = $this->broker->getClass($referencedClass);
$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
);
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
// Other rules will notify if the class is not found
} catch (\PHPStan\Reflection\MissingMethodFromReflectionException $e) {
// Other rules will notify if the the method is not found
}
}
return null;
}
}