Completed
Pull Request — master (#202)
by
unknown
05:20
created

AssignmentInCondition   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 89.8%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 87
ccs 44
cts 49
cp 0.898
rs 10
wmc 16
lcom 0
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
C pass() 0 35 12
A getConfiguration() 0 8 1
A getRegister() 0 11 1
A checkAssignment() 0 11 2
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