Passed
Push — master ( 450618...75fd14 )
by Matthew
05:47
created

DoChecker::analyze()   F

Complexity

Conditions 15
Paths 252

Size

Total Lines 77
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 43
c 1
b 0
f 0
nc 252
nop 3
dl 0
loc 77
rs 3.9271

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Documentation introduced by
$stmt->stmts is of type array<integer,object<PhpParser\Node>>, but the function expects a array<integer,object<Php...t<PhpParser\Node\Expr>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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,
0 ignored issues
show
Documentation introduced by
$stmt->stmts is of type array<integer,object<PhpParser\Node>>, but the function expects a array<integer,object<Php...t<PhpParser\Node\Expr>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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