Completed
Push — master ( 3708b9...fb0aff )
by Enrico
03:36
created

AssignmentInCondition   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 61
ccs 32
cts 32
cp 1
rs 10
c 1
b 0
f 0
wmc 12
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
C pass() 0 22 9
A getRegister() 0 10 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\If_;
14
use PhpParser\Node\Stmt\Switch_;
15
use PhpParser\Node\Stmt\While_;
16
use PHPSA\Analyzer\Pass;
17
use PHPSA\Context;
18
19
class AssignmentInCondition implements Pass\AnalyzerPassInterface
20
{
21
    /**
22
     * @param $stmt
23
     * @param Context $context
24
     * @return bool
25
     */
26 12
    public function pass($stmt, Context $context)
27
    {
28 12
        $result = false;
29
30 12
        if ($stmt instanceof If_) {
31 7
            $this->checkAssignment($stmt, $context);
32
33 7
            $elseifStmts = $stmt->elseifs;
34 7
            foreach ($elseifStmts as $elseif) {
35 2
                $result = $this->checkAssignment($elseif, $context) || $result;
36 7
            }
37 12
        } elseif ($stmt instanceof While_ || $stmt instanceof Do_) {
38 5
            $this->checkAssignment($stmt, $context);
39 6
        } elseif ($stmt instanceof Switch_) {
40 2
            $caseStmts = $stmt->cases;
41 2
            foreach ($caseStmts as $case) {
42 2
                $result = $this->checkAssignment($case, $context) || $result;
43 2
            }
44 2
        }
45
46 12
        return $result;
47
    }
48
49
    /**
50
     * @return array
51
     */
52 1
    public function getRegister()
53
    {
54
        return [
55 1
            If_::class,
56 1
            ElseIf_::class,
57 1
            While_::class,
58 1
            Do_::class,
59 1
            Switch_::class,
60 1
        ];
61
    }
62
63
    /**
64
     * @param Stmt $stmt
65
     * @param Context $context
66
     * @return bool
67
     */
68 12
    private function checkAssignment(Stmt $stmt, Context $context)
69
    {
70 12
        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...
71 4
            $context->notice(
72 4
                'assignment_in_condition',
73 4
                'An assignment statement has been made instead of conditional statement',
74
                $stmt
75 4
            );
76 4
            return true;
77
        }
78 9
    }
79
}
80