-
Notifications
You must be signed in to change notification settings - Fork 660
/
IssetAnalyzer.php
75 lines (65 loc) · 2.6 KB
/
IssetAnalyzer.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
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Issue\InvalidArgument;
use Psalm\IssueBuffer;
use Psalm\Type;
/**
* @internal
*/
final class IssetAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\Isset_ $stmt,
Context $context
): void {
foreach ($stmt->vars as $isset_var) {
if ($isset_var instanceof PhpParser\Node\Expr\PropertyFetch
&& $isset_var->var instanceof PhpParser\Node\Expr\Variable
&& $isset_var->var->name === 'this'
&& $isset_var->name instanceof PhpParser\Node\Identifier
) {
$var_id = '$this->' . $isset_var->name->name;
if (!isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;
}
} elseif (!self::isValidStatement($isset_var)) {
IssueBuffer::maybeAdd(
new InvalidArgument(
'Isset only works with variables and array elements',
new CodeLocation($statements_analyzer->getSource(), $isset_var),
'empty',
),
$statements_analyzer->getSuppressedIssues(),
);
}
self::analyzeIssetVar($statements_analyzer, $isset_var, $context);
}
$statements_analyzer->node_data->setType($stmt, Type::getBool());
}
public static function analyzeIssetVar(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr $stmt,
Context $context
): void {
$context->inside_isset = true;
ExpressionAnalyzer::analyze($statements_analyzer, $stmt, $context);
$context->inside_isset = false;
}
private static function isValidStatement(PhpParser\Node\Expr $stmt): bool
{
return $stmt instanceof PhpParser\Node\Expr\Variable
|| $stmt instanceof PhpParser\Node\Expr\ArrayDimFetch
|| $stmt instanceof PhpParser\Node\Expr\PropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\ClassConstFetch
|| $stmt instanceof PhpParser\Node\Expr\AssignRef;
}
}