-
Notifications
You must be signed in to change notification settings - Fork 650
/
EchoChecker.php
62 lines (55 loc) · 2.14 KB
/
EchoChecker.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
<?php
namespace Psalm\Example\Plugin\ComposerBased;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Issue\ArgumentTypeCoercion;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterStatementAnalysisEvent;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TString;
class EchoChecker implements AfterStatementAnalysisInterface
{
/**
* Called after a statement has been checked
*
* @return null|false
*/
public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool
{
$stmt = $event->getStmt();
$statements_source = $event->getStatementsSource();
if ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
foreach ($stmt->exprs as $expr) {
$expr_type = $statements_source->getNodeTypeProvider()->getType($expr);
if (!$expr_type || $expr_type->hasMixed()) {
IssueBuffer::maybeAdd(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
'echo'
),
$statements_source->getSuppressedIssues()
);
continue;
}
$types = $expr_type->getAtomicTypes();
foreach ($types as $type) {
if ($type instanceof TString
&& !$type instanceof TLiteralString
) {
IssueBuffer::maybeAdd(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
'echo'
),
$statements_source->getSuppressedIssues()
);
}
}
}
}
return null;
}
}