forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringChecker.php
73 lines (66 loc) · 2.86 KB
/
StringChecker.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
<?php
namespace Psalm\Example\Plugin;
use PhpParser;
use Psalm\Checker;
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
class StringChecker implements AfterExpressionAnalysisInterface
{
/**
* Called after an expression has been checked
*
* @return null|false
*/
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool {
$expr = $event->getExpr();
$statements_source = $event->getStatementsSource();
$codebase = $event->getCodebase();
if ($expr instanceof PhpParser\Node\Scalar\String_) {
$class_or_class_method = '/^\\\?Psalm(\\\[A-Z][A-Za-z0-9]+)+(::[A-Za-z0-9]+)?$/';
if (strpos($statements_source->getFileName(), 'base/DefinitionManager.php') === false
&& strpos($expr->value, 'TestController') === false
&& preg_match($class_or_class_method, $expr->value)
) {
$absolute_class = preg_split('/[:]/', $expr->value)[0];
IssueBuffer::maybeAdd(
new InvalidClass(
'Use ::class constants when representing class names',
new CodeLocation($statements_source, $expr),
$absolute_class
),
$statements_source->getSuppressedIssues()
);
}
} elseif ($expr instanceof PhpParser\Node\Expr\BinaryOp\Concat
&& $expr->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $expr->left->class instanceof PhpParser\Node\Name
&& $expr->left->name instanceof PhpParser\Node\Identifier
&& strtolower($expr->left->name->name) === 'class'
&& !in_array(strtolower($expr->left->class->parts[0]), ['self', 'static', 'parent'])
&& $expr->right instanceof PhpParser\Node\Scalar\String_
&& preg_match('/^::[A-Za-z0-9]+$/', $expr->right->value)
) {
$method_id = ((string) $expr->left->class->getAttribute('resolvedName')) . $expr->right->value;
$appearing_method_id = $codebase->getAppearingMethodId($method_id);
if (!$appearing_method_id) {
if (IssueBuffer::accepts(
new UndefinedMethod(
'Method ' . $method_id . ' does not exist',
new CodeLocation($statements_source, $expr),
$method_id
),
$statements_source->getSuppressedIssues()
)) {
return false;
}
return null;
}
}
return null;
}
}