Completed
Pull Request — master (#195)
by
unknown
05:01
created

AssignmentInCondition::pass()   C

Complexity

Conditions 12
Paths 5

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 23
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 35
ccs 28
cts 28
cp 1
crap 12
rs 5.1612

How to fix   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
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Statement;
7
8
use PhpParser\Node\Expr;
9
use PhpParser\Node\Expr\Assign;
10
use PhpParser\Node\Stmt;
11
use PhpParser\Node\Stmt\Do_;
12
use PhpParser\Node\Stmt\ElseIf_;
13
use PhpParser\Node\Stmt\For_;
14
use PhpParser\Node\Stmt\If_;
15
use PhpParser\Node\Stmt\Switch_;
16
use PhpParser\Node\Stmt\While_;
17
use PHPSA\Analyzer\Pass;
18
use PHPSA\Context;
19
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
20
21
class AssignmentInCondition implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface
22
{
23
    /**
24
     * @param $stmt
25
     * @param Context $context
26
     * @return bool
27
     */
28 14
    public function pass($stmt, Context $context)
29
    {
30 14
        $result = false;
31
32 14
        if ($stmt instanceof If_) {
33 5
            $this->checkAssignment($stmt, $context);
34
35 5
            $elseifStmts = $stmt->elseifs;
36 5
            foreach ($elseifStmts as $elseif) {
37 2
                $result = $this->checkAssignment($elseif, $context) || $result;
38 5
            }
39 14
        } elseif ($stmt instanceof While_ || $stmt instanceof Do_) {
40 5
            $this->checkAssignment($stmt, $context);
41 10
        } elseif ($stmt instanceof Switch_) {
42 2
            $caseStmts = $stmt->cases;
43 2
            foreach ($caseStmts as $case) {
44 2
                $result = $this->checkAssignment($case, $context) || $result;
45 2
            }
46 6
        } elseif ($stmt instanceof For_) {
47 5
            $conds = $stmt->cond;
48
49 5
            foreach ($conds as $cond) {
50 2
                if ($cond instanceof Assign) {
51 2
                    $context->notice(
52 2
                        'assignment_in_condition',
53 2
                        'An assignment statement has been made instead of conditional statement',
54
                        $stmt
55 2
                    );
56 2
                    $result = true;
57 2
                }
58 5
            }
59 5
        }
60
61 14
        return $result;
62
    }
63
64
    /**
65
     * @return TreeBuilder
66
     */
67
    public function getConfiguration()
68
    {
69
        $treeBuilder = new TreeBuilder();
70
        $treeBuilder->root('assignment_in_condition')
71
            ->canBeDisabled();
72
73
        return $treeBuilder;
74
    }
75
76
    /**
77
     * @return array
78
     */
79 1
    public function getRegister()
80
    {
81
        return [
82 1
            If_::class,
83 1
            ElseIf_::class,
84 1
            While_::class,
85 1
            Do_::class,
86 1
            Switch_::class,
87 1
            For_::class,
88 1
        ];
89
    }
90
91
    /**
92
     * @param Stmt $stmt
93
     * @param Context $context
94
     * @return bool
95
     */
96 10
    private function checkAssignment(Stmt $stmt, Context $context)
97
    {
98 10
        if ($stmt->cond instanceof Assign) {
0 ignored issues
show
Bug introduced by
The property cond does not seem to exist in PhpParser\Node\Stmt.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99 4
            $context->notice(
100 4
                'assignment_in_condition',
101 4
                'An assignment statement has been made instead of conditional statement',
102
                $stmt
103 4
            );
104 4
            return true;
105
        }
106 7
    }
107
}
108