forked from vimeo/psalm
/
MethodCallPurityAnalyzer.php
175 lines (163 loc) · 6.96 KB
/
MethodCallPurityAnalyzer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\Call\Method;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\InstancePropertyAssignmentAnalyzer as AssignmentAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\MethodIdentifier;
use Psalm\Issue\ImpureMethodCall;
use Psalm\IssueBuffer;
use Psalm\Type;
class MethodCallPurityAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
PhpParser\Node\Expr\MethodCall $stmt,
?string $lhs_var_id,
string $cased_method_id,
MethodIdentifier $method_id,
\Psalm\Storage\MethodStorage $method_storage,
\Psalm\Storage\ClassLikeStorage $class_storage,
Context $context,
\Psalm\Config $config,
AtomicMethodCallAnalysisResult $result
) : void {
$method_pure_compatible = $method_storage->external_mutation_free
&& $statements_analyzer->node_data->isPureCompatible($stmt->var);
if ($context->pure
&& !$method_storage->mutation_free
&& !$method_pure_compatible
) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call a non-mutation-free method '
. $cased_method_id . ' from a pure context',
new CodeLocation($statements_analyzer, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($context->mutation_free
&& !$method_storage->mutation_free
&& !$method_pure_compatible
) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call a possibly-mutating method '
. $cased_method_id . ' from a mutation-free context',
new CodeLocation($statements_analyzer, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($context->external_mutation_free
&& !$method_storage->mutation_free
&& $method_id->fq_class_name !== $context->self
&& !$method_pure_compatible
) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call a possibly-mutating method '
. $cased_method_id . ' from a mutation-free context',
new CodeLocation($statements_analyzer, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif (($method_storage->mutation_free
|| ($method_storage->external_mutation_free
&& ($stmt->var->getAttribute('external_mutation_free', false)
|| $stmt->var->getAttribute('pure', false))
))
&& !$context->inside_unset
) {
if ($method_storage->mutation_free
&& (!$method_storage->mutation_free_inferred
|| $method_storage->final
|| $method_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE)
&& ($method_storage->immutable || $config->remember_property_assignments_after_call)
) {
if ($context->inside_conditional
&& !$method_storage->assertions
&& !$method_storage->if_true_assertions
) {
$stmt->setAttribute('memoizable', true);
if ($method_storage->immutable) {
$stmt->setAttribute('pure', true);
}
}
$result->can_memoize = true;
}
if ($codebase->find_unused_variables
&& !$context->inside_conditional
&& !$context->inside_general_use
&& !$context->inside_throw
) {
if (!$context->inside_assignment
&& !$context->inside_call
&& !$context->inside_return
&& !$method_storage->assertions
&& !$method_storage->if_true_assertions
&& !$method_storage->if_false_assertions
&& !$method_storage->throws
) {
if (IssueBuffer::accepts(
new \Psalm\Issue\UnusedMethodCall(
'The call to ' . $cased_method_id . ' is not used',
new CodeLocation($statements_analyzer, $stmt->name),
(string) $method_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif (!$method_storage->mutation_free_inferred) {
$stmt->setAttribute('pure', true);
}
}
}
if ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
&& !$method_storage->mutation_free
&& !$method_pure_compatible
) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
$statements_analyzer->getSource()->inferred_impure = true;
}
if (!$config->remember_property_assignments_after_call
&& !$method_storage->mutation_free
&& !$method_pure_compatible
) {
$context->removeMutableObjectVars();
} elseif ($method_storage->this_property_mutations) {
if (!$method_pure_compatible) {
$context->removeMutableObjectVars(true);
}
foreach ($method_storage->this_property_mutations as $name => $_) {
$mutation_var_id = $lhs_var_id . '->' . $name;
$this_property_didnt_exist = $lhs_var_id === '$this'
&& isset($context->vars_in_scope[$mutation_var_id])
&& !isset($class_storage->declaring_property_ids[$name]);
if ($this_property_didnt_exist) {
$context->vars_in_scope[$mutation_var_id] = Type::getMixed();
} else {
$new_type = AssignmentAnalyzer::getExpandedPropertyType(
$codebase,
$class_storage->name,
$name,
$class_storage
) ?? Type::getMixed();
$context->vars_in_scope[$mutation_var_id] = $new_type;
$context->possibly_assigned_var_ids[$mutation_var_id] = true;
}
}
}
}
}