Completed
Pull Request — master (#194)
by Enrico
07:43
created

AssignmentInCondition::checkAssignment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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