OrAnalyzer   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 295
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
dl 59
loc 295
rs 9.52
c 0
b 0
f 0
wmc 36
lcom 1
cbo 18

1 Method

Rating   Name   Duplication   Size   Complexity  
F analyze() 59 292 36

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
3
4
use PhpParser;
5
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
6
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
7
use Psalm\Internal\Analyzer\StatementsAnalyzer;
8
use Psalm\Internal\Analyzer\Statements\Block\IfAnalyzer;
9
use Psalm\CodeLocation;
10
use Psalm\Context;
11
use Psalm\Type;
12
use Psalm\Type\Algebra;
13
use Psalm\Type\Reconciler;
14
use Psalm\Internal\Type\AssertionReconciler;
15
use function array_merge;
16
use function array_diff_key;
17
use function array_filter;
18
use function array_values;
19
use function array_map;
20
21
/**
22
 * @internal
23
 */
24
class OrAnalyzer
25
{
26
    public static function analyze(
27
        StatementsAnalyzer $statements_analyzer,
28
        PhpParser\Node\Expr\BinaryOp $stmt,
29
        Context $context,
30
        bool $from_stmt = false
31
    ) : bool {
32 View Code Duplication
        if ($from_stmt) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            $fake_if_stmt = new PhpParser\Node\Stmt\If_(
34
                new PhpParser\Node\Expr\BooleanNot($stmt->left, $stmt->left->getAttributes()),
35
                [
36
                    'stmts' => [
37
                        new PhpParser\Node\Stmt\Expression(
38
                            $stmt->right
39
                        )
40
                    ]
41
                ],
42
                $stmt->getAttributes()
43
            );
44
45
            return IfAnalyzer::analyze($statements_analyzer, $fake_if_stmt, $context) !== false;
46
        }
47
48
        $codebase = $statements_analyzer->getCodebase();
49
50
        if (!$stmt->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
51
            && !($stmt->left instanceof PhpParser\Node\Expr\BooleanNot
52
                && $stmt->left->expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd)
53
        ) {
54
            $if_scope = new \Psalm\Internal\Scope\IfScope();
55
56
            try {
57
                $if_conditional_scope = IfAnalyzer::analyzeIfConditional(
58
                    $statements_analyzer,
59
                    $stmt->left,
60
                    $context,
61
                    $codebase,
62
                    $if_scope,
63
                    $context->branch_point ?: (int) $stmt->getAttribute('startFilePos')
64
                );
65
66
                $left_context = $if_conditional_scope->if_context;
67
68
                $left_referenced_var_ids = $if_conditional_scope->cond_referenced_var_ids;
69
                $left_assigned_var_ids = $if_conditional_scope->cond_assigned_var_ids;
0 ignored issues
show
Unused Code introduced by
$left_assigned_var_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
            } catch (\Psalm\Exception\ScopeAnalysisException $e) {
71
                return false;
72
            }
73
        } else {
74
            $pre_referenced_var_ids = $context->referenced_var_ids;
75
            $context->referenced_var_ids = [];
76
77
            $pre_assigned_var_ids = $context->assigned_var_ids;
78
79
            $left_context = clone $context;
80
            $left_context->parent_context = $context;
81
            $left_context->if_context = null;
82
            $left_context->assigned_var_ids = [];
83
84
            if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $left_context) === false) {
85
                return false;
86
            }
87
88
            foreach ($left_context->vars_in_scope as $var_id => $type) {
89
                if (!isset($context->vars_in_scope[$var_id])) {
90
                    if (isset($left_context->assigned_var_ids[$var_id])) {
91
                        $context->vars_in_scope[$var_id] = clone $type;
92
                    }
93
                } else {
94
                    $context->vars_in_scope[$var_id] = Type::combineUnionTypes(
95
                        $context->vars_in_scope[$var_id],
96
                        $type,
97
                        $codebase
98
                    );
99
                }
100
            }
101
102
            if ($codebase->find_unused_variables) {
103
                $context->unreferenced_vars = $left_context->unreferenced_vars;
104
            }
105
106
            $left_referenced_var_ids = $left_context->referenced_var_ids;
107
            $left_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $left_referenced_var_ids);
108
109
            $left_assigned_var_ids = array_diff_key($left_context->assigned_var_ids, $pre_assigned_var_ids);
110
111
            $left_referenced_var_ids = array_diff_key($left_referenced_var_ids, $left_assigned_var_ids);
112
        }
113
114
        $left_clauses = Algebra::getFormula(
115
            \spl_object_id($stmt->left),
116
            $stmt->left,
117
            $context->self,
118
            $statements_analyzer,
119
            $codebase
120
        );
121
122
        try {
123
            $negated_left_clauses = Algebra::negateFormula($left_clauses);
124
        } catch (\Psalm\Exception\ComplicatedExpressionException $e) {
125
            return false;
126
        }
127
128 View Code Duplication
        if ($left_context->reconciled_expression_clauses) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
            $reconciled_expression_clauses = $left_context->reconciled_expression_clauses;
130
131
            $negated_left_clauses = array_values(
132
                array_filter(
133
                    $negated_left_clauses,
134
                    function ($c) use ($reconciled_expression_clauses) {
135
                        return !\in_array($c->getHash(), $reconciled_expression_clauses);
136
                    }
137
                )
138
            );
139
140
            if (\count($negated_left_clauses) === 1
141
                && $negated_left_clauses[0]->wedge
142
                && !$negated_left_clauses[0]->possibilities
143
            ) {
144
                $negated_left_clauses = [];
145
            }
146
        }
147
148
        $clauses_for_right_analysis = Algebra::simplifyCNF(
149
            array_merge(
150
                $context->clauses,
151
                $negated_left_clauses
152
            )
153
        );
154
155
        $active_negated_type_assertions = [];
156
157
        $negated_type_assertions = Algebra::getTruthsFromFormula(
158
            $clauses_for_right_analysis,
159
            \spl_object_id($stmt->left),
160
            $left_referenced_var_ids,
161
            $active_negated_type_assertions
162
        );
163
164
        $changed_var_ids = [];
165
166
        $right_context = clone $context;
167
168 View Code Duplication
        if ($negated_type_assertions) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            // while in an or, we allow scope to boil over to support
170
            // statements of the form if ($x === null || $x->foo())
171
            $right_vars_in_scope = Reconciler::reconcileKeyedTypes(
172
                $negated_type_assertions,
173
                $active_negated_type_assertions,
174
                $right_context->vars_in_scope,
175
                $changed_var_ids,
176
                $left_referenced_var_ids,
177
                $statements_analyzer,
178
                [],
179
                $left_context->inside_loop,
180
                new CodeLocation($statements_analyzer->getSource(), $stmt)
181
            );
182
            $right_context->vars_in_scope = $right_vars_in_scope;
183
        }
184
185
        $right_context->clauses = $clauses_for_right_analysis;
186
187
        if ($changed_var_ids) {
188
            $partitioned_clauses = Context::removeReconciledClauses($right_context->clauses, $changed_var_ids);
189
            $right_context->clauses = $partitioned_clauses[0];
190
            $right_context->reconciled_expression_clauses = array_merge(
191
                $context->reconciled_expression_clauses,
192
                array_map(
193
                    function ($c) {
194
                        return $c->getHash();
195
                    },
196
                    $partitioned_clauses[1]
197
                )
198
            );
199
200
            $partitioned_clauses = Context::removeReconciledClauses($context->clauses, $changed_var_ids);
201
            $context->clauses = $partitioned_clauses[0];
202
            $context->reconciled_expression_clauses = array_merge(
203
                $context->reconciled_expression_clauses,
204
                array_map(
205
                    function ($c) {
206
                        return $c->getHash();
207
                    },
208
                    $partitioned_clauses[1]
209
                )
210
            );
211
        }
212
213
        $right_context->if_context = null;
214
215
        if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $right_context) === false) {
216
            return false;
217
        }
218
219
        if (!($stmt->right instanceof PhpParser\Node\Expr\Exit_)) {
220 View Code Duplication
            foreach ($right_context->vars_in_scope as $var_id => $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
                if (isset($context->vars_in_scope[$var_id])) {
222
                    $context->vars_in_scope[$var_id] = Type::combineUnionTypes(
223
                        $context->vars_in_scope[$var_id],
224
                        $type,
225
                        $codebase
226
                    );
227
                }
228
            }
229
        } elseif ($stmt->left instanceof PhpParser\Node\Expr\Assign) {
230
            $var_id = ExpressionIdentifier::getVarId($stmt->left->var, $context->self);
231
232
            if ($var_id && isset($left_context->vars_in_scope[$var_id])) {
233
                $left_inferred_reconciled = AssertionReconciler::reconcile(
234
                    '!falsy',
235
                    clone $left_context->vars_in_scope[$var_id],
236
                    '',
237
                    $statements_analyzer,
238
                    $context->inside_loop,
239
                    [],
240
                    new CodeLocation($statements_analyzer->getSource(), $stmt->left),
241
                    $statements_analyzer->getSuppressedIssues()
242
                );
243
244
                $context->vars_in_scope[$var_id] = $left_inferred_reconciled;
245
            }
246
        }
247
248
        if ($context->inside_conditional) {
249
            $context->updateChecks($right_context);
250
        }
251
252
        $context->referenced_var_ids = array_merge(
253
            $right_context->referenced_var_ids,
254
            $context->referenced_var_ids
255
        );
256
257
        $context->assigned_var_ids = array_merge(
258
            $context->assigned_var_ids,
259
            $right_context->assigned_var_ids
260
        );
261
262
        if ($codebase->find_unused_variables) {
263
            foreach ($right_context->unreferenced_vars as $var_id => $locations) {
264
                if (!isset($context->unreferenced_vars[$var_id])) {
265
                    $context->unreferenced_vars[$var_id] = $locations;
266
                } else {
267
                    $new_locations = array_diff_key(
268
                        $locations,
269
                        $context->unreferenced_vars[$var_id]
270
                    );
271
272
                    if ($new_locations) {
273
                        $context->unreferenced_vars[$var_id] += $locations;
274
                    }
275
                }
276
            }
277
        }
278
279
        if ($context->if_context) {
280
            $if_context = $context->if_context;
281
282
            foreach ($right_context->vars_in_scope as $var_id => $type) {
283
                if (isset($if_context->vars_in_scope[$var_id])) {
284
                    $if_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
285
                        $type,
286
                        $if_context->vars_in_scope[$var_id],
287
                        $codebase
288
                    );
289
                } elseif (isset($left_context->vars_in_scope[$var_id])) {
290
                    $if_context->vars_in_scope[$var_id] = $left_context->vars_in_scope[$var_id];
291
                }
292
            }
293
294
            $if_context->referenced_var_ids = array_merge(
295
                $context->referenced_var_ids,
296
                $if_context->referenced_var_ids
297
            );
298
299
            $if_context->assigned_var_ids = array_merge(
300
                $context->assigned_var_ids,
301
                $if_context->assigned_var_ids
302
            );
303
304
            if ($codebase->find_unused_variables) {
305
                $if_context->unreferenced_vars = $context->unreferenced_vars;
306
            }
307
308
            $if_context->updateChecks($context);
309
        }
310
311
        $context->vars_possibly_in_scope = array_merge(
312
            $right_context->vars_possibly_in_scope,
313
            $context->vars_possibly_in_scope
314
        );
315
316
        return true;
317
    }
318
}
319