1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace UsageFinder; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PhpParser\Node\Expr; |
9
|
|
|
use PhpParser\Node\Expr\MethodCall; |
10
|
|
|
use PhpParser\Node\Expr\StaticCall; |
11
|
|
|
use Psalm\Codebase; |
12
|
|
|
use Psalm\CodeLocation; |
13
|
|
|
use Psalm\Context; |
14
|
|
|
use Psalm\FileManipulation; |
15
|
|
|
use Psalm\IssueBuffer; |
16
|
|
|
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface; |
17
|
|
|
use Psalm\StatementsSource; |
18
|
|
|
use Psalm\Type\Union; |
19
|
|
|
use UsageFinder\Issue\ClassMethodUsageFound; |
20
|
|
|
use function explode; |
21
|
|
|
use function getenv; |
22
|
|
|
use function sprintf; |
23
|
|
|
use function strtolower; |
24
|
|
|
|
25
|
|
|
class FindClassMethodUsagesPlugin implements AfterMethodCallAnalysisInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param MethodCall|StaticCall $expr |
29
|
|
|
* @param FileManipulation[] $file_replacements |
30
|
|
|
*/ |
31
|
4 |
|
public static function afterMethodCallAnalysis( |
32
|
|
|
Expr $expr, |
33
|
|
|
string $method_id, |
34
|
|
|
string $appearing_method_id, |
35
|
|
|
string $declaring_method_id, |
36
|
|
|
Context $context, |
37
|
|
|
StatementsSource $statements_source, |
38
|
|
|
Codebase $codebase, |
39
|
|
|
array &$file_replacements = [], |
40
|
|
|
?Union &$return_type_candidate = null |
41
|
|
|
) : void { |
42
|
4 |
|
if (! $expr instanceof MethodCall) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
if (! self::isMethodWeWant($declaring_method_id, $codebase)) { |
47
|
2 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
static::bufferClassMethodUsage($expr, $statements_source, $declaring_method_id); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
protected static function bufferClassMethodUsage( |
54
|
|
|
MethodCall $methodCall, |
55
|
|
|
StatementsSource $statements_source, |
56
|
|
|
string $declaring_method_id |
57
|
|
|
) : void { |
58
|
|
|
IssueBuffer::accepts( |
59
|
|
|
new ClassMethodUsageFound( |
60
|
|
|
sprintf('Found reference to %s', $declaring_method_id), |
61
|
|
|
new CodeLocation($statements_source, $methodCall->name) |
62
|
|
|
), |
63
|
|
|
$statements_source->getSuppressedIssues() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
private static function isMethodWeWant(string $declaring_method_id, Codebase $codebase) : bool |
68
|
|
|
{ |
69
|
4 |
|
[$className, $methodName] = explode('::', $declaring_method_id); |
70
|
|
|
|
71
|
4 |
|
if (strtolower($declaring_method_id) === strtolower(self::getFindName())) { |
72
|
1 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return strtolower($methodName) === strtolower(self::getFindMethodName()) |
76
|
3 |
|
&& $codebase->classImplements($className, self::getFindClassName()); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
private static function getFindName() : string |
80
|
|
|
{ |
81
|
4 |
|
return self::getEnv('USAGE_FINDER_NAME'); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
private static function getFindClassName() : string |
85
|
|
|
{ |
86
|
2 |
|
return self::getEnv('USAGE_FINDER_CLASS_NAME'); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
private static function getFindMethodName() : string |
90
|
|
|
{ |
91
|
3 |
|
return self::getEnv('USAGE_FINDER_METHOD_NAME'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws InvalidArgumentException |
96
|
|
|
*/ |
97
|
4 |
|
private static function getEnv(string $name) : string |
98
|
|
|
{ |
99
|
4 |
|
$value = getenv($name); |
100
|
|
|
|
101
|
4 |
|
if ($value === false) { |
102
|
|
|
throw new InvalidArgumentException(sprintf('Missing env variable %s.', $name)); |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
return $value; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|