|
1
|
|
|
<?php |
|
2
|
|
|
namespace Psalm\Checker\Statements\Block; |
|
3
|
|
|
|
|
4
|
|
|
use PhpParser; |
|
5
|
|
|
use Psalm\Checker\Statements\ExpressionChecker; |
|
6
|
|
|
use Psalm\Checker\StatementsChecker; |
|
7
|
|
|
use Psalm\Context; |
|
8
|
|
|
use Psalm\Scope\LoopScope; |
|
9
|
|
|
use Psalm\Type; |
|
10
|
|
|
|
|
11
|
|
|
class DoChecker |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return false|null |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function analyze( |
|
17
|
|
|
StatementsChecker $statements_checker, |
|
18
|
|
|
PhpParser\Node\Stmt\Do_ $stmt, |
|
19
|
|
|
Context $context |
|
20
|
|
|
) { |
|
21
|
|
|
$do_context = clone $context; |
|
22
|
|
|
|
|
23
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker; |
|
24
|
|
|
|
|
25
|
|
|
if ($project_checker->alter_code) { |
|
26
|
|
|
$do_context->branch_point = $do_context->branch_point ?: (int) $stmt->getAttribute('startFilePos'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$loop_scope = new LoopScope($do_context, $context); |
|
30
|
|
|
$loop_scope->protected_var_ids = $context->protected_var_ids; |
|
31
|
|
|
|
|
32
|
|
|
$statements_checker->analyze($stmt->stmts, $do_context, $loop_scope); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
foreach ($context->vars_in_scope as $var => $type) { |
|
35
|
|
|
if ($type->isMixed()) { |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($do_context->hasVariable($var)) { |
|
40
|
|
|
if ($context->vars_in_scope[$var]->isMixed()) { |
|
41
|
|
|
$do_context->vars_in_scope[$var] = $do_context->vars_in_scope[$var]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($do_context->vars_in_scope[$var]->getId() !== $type->getId()) { |
|
45
|
|
|
$do_context->vars_in_scope[$var] = Type::combineUnionTypes($do_context->vars_in_scope[$var], $type); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
LoopChecker::analyze( |
|
51
|
|
|
$statements_checker, |
|
52
|
|
|
$stmt->stmts, |
|
|
|
|
|
|
53
|
|
|
$stmt->cond ? [$stmt->cond] : [], |
|
54
|
|
|
[], |
|
55
|
|
|
$loop_scope, |
|
56
|
|
|
$inner_loop_context |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($do_context->vars_in_scope as $var_id => $type) { |
|
60
|
|
|
if (!isset($context->vars_in_scope[$var_id])) { |
|
61
|
|
|
$context->vars_in_scope[$var_id] = $type; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// because it's a do {} while, inner loop vars belong to the main context |
|
66
|
|
|
if ($inner_loop_context) { |
|
67
|
|
|
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) { |
|
68
|
|
|
if (!isset($context->vars_in_scope[$var_id])) { |
|
69
|
|
|
$context->vars_in_scope[$var_id] = $type; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$context->vars_possibly_in_scope = array_merge( |
|
75
|
|
|
$context->vars_possibly_in_scope, |
|
76
|
|
|
$do_context->vars_possibly_in_scope |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$context->referenced_var_ids = array_merge( |
|
80
|
|
|
$context->referenced_var_ids, |
|
81
|
|
|
$do_context->referenced_var_ids |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if ($context->collect_references) { |
|
85
|
|
|
$context->unreferenced_vars = array_intersect_key( |
|
86
|
|
|
$do_context->unreferenced_vars, |
|
87
|
|
|
$context->unreferenced_vars |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return ExpressionChecker::analyze($statements_checker, $stmt->cond, $context); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: