AssignmentInCondition::getRegister()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 2
cp 0
crap 2
rs 9.9
c 0
b 0
f 0
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\Case_;
15
use PhpParser\Node\Stmt\While_;
16
use PhpParser\Node\Stmt\For_;
17
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
18
use PHPSA\Analyzer\Pass;
19
use PHPSA\Context;
20
21
class AssignmentInCondition implements Pass\AnalyzerPassInterface
22
{
23
    use DefaultMetadataPassTrait;
24
25
    const DESCRIPTION = 'Checks for assignments in conditions. (= instead of ==)';
26
27
    /**
28
     * @param $stmt
29
     * @param Context $context
30
     * @return bool
31
     */
32
    public function pass($stmt, Context $context)
33
    {
34
        $condition = $stmt->cond;
35
36
        if ($stmt instanceof For_ && count($stmt->cond) > 0) { // For is the only one that has an array as condition
37
            $condition = $condition[0];
38
        }
39
40
        if ($condition instanceof Assign) {
41
            $context->notice(
42
                'assignment_in_condition',
43
                'An assignment statement has been made instead of conditional statement',
44
                $stmt
45
            );
46
            return true;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getRegister()
56
    {
57
        return [
58
            If_::class,
59
            ElseIf_::class,
60
            For_::class,
61
            While_::class,
62
            Do_::class,
63
            Case_::class,
64
        ];
65
    }
66
}
67