Completed
Pull Request — master (#191)
by
unknown
04:25
created

AssignmentInCondition::getRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
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\While_;
15
use PHPSA\Analyzer\Pass;
16
use PHPSA\Context;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
19
class AssignmentInCondition implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface
20
{
21
    /**
22
     * @param $stmt
23
     * @param Context $context
24
     * @return bool
25
     */
26 9
    public function pass($stmt, Context $context)
27
    {
28 9
        $result = false;
29
30 9
        if ($stmt instanceof If_) {
31 5
            $this->checkAssignment($stmt, $context);
32
33 5
            $elseifStmts = $stmt->elseifs;
34 5
            foreach ($elseifStmts as $elseif) {
35 2
                $result = $this->checkAssignment($elseif, $context) || $result;
36 5
            }
37 9
        } elseif ($stmt instanceof While_ || $stmt instanceof Do_) {
38 5
            $this->checkAssignment($stmt, $context);
39 5
        }
40
41 9
        return $result;
42
    }
43
44
    /**
45
     * @return TreeBuilder
46
     */
47
    public function getConfiguration()
48
    {
49
        $treeBuilder = new TreeBuilder();
50
        $treeBuilder->root('assignment_in_condition')
51
            ->canBeDisabled();
52
53
        return $treeBuilder;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 1
    public function getRegister()
60
    {
61
        return [
62 1
            If_::class,
63 1
            ElseIf_::class,
64 1
            While_::class,
65 1
            Do_::class,
66 1
        ];
67
    }
68
69
    /**
70
     * @param Stmt $stmt
71
     * @param Context $context
72
     * @return bool
73
     */
74 9
    private function checkAssignment(Stmt $stmt, Context $context)
75
    {
76 9
        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...
77 4
            $context->notice(
78 4
                'assignment_in_condition',
79 4
                'An assignment statement has been made instead of conditional statement',
80
                $stmt
81 4
            );
82 4
            return true;
83
        }
84 6
    }
85
}
86