Completed
Push — master ( 41783b...6c90aa )
by Enrico
03:55
created

FixedCondition::pass()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 12
c 1
b 1
f 0
nc 4
nop 2
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 20
rs 8.9197
1
<?php
2
/**
3
 * @author Christian Kraus <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Statement;
7
8
use PhpParser\Node\Stmt\Do_;
9
use PhpParser\Node\Stmt\If_;
10
use PhpParser\Node\Stmt\ElseIf_;
11
use PhpParser\Node\Stmt\Switch_;
12
use PhpParser\Node\Stmt\While_;
13
use PhpParser\Node\Stmt\For_;
14
use PHPSA\Analyzer\Pass;
15
use PHPSA\Context;
16
17
class FixedCondition implements Pass\AnalyzerPassInterface
18
{
19
    /**
20
     * @param $stmt
21
     * @param Context $context
22
     * @return bool
23
     */
24
    public function pass($stmt, Context $context)
25
    {
26
        $condition = $stmt->cond;
27
28
        if ($stmt instanceof For_ && count($stmt->cond) > 0) { // For is the only one that has an array as condition
29
            $condition = $condition[0];
30
        }
31
32
        $expression = $context->getExpressionCompiler()->compile($condition);
33
34
        if ($expression->hasValue()) { // @todo implement isStatic() method to see if expression changes or not
35
            $context->notice(
36
                'fixed_condition',
37
                'The condition will always result in the same boolean value',
38
                $stmt
39
            );
40
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getRegister()
51
    {
52
        return [
53
            If_::class,
54
            ElseIf_::class,
55
            While_::class,
56
            For_::class,
57
            Do_::class,
58
            Switch_::class,
59
        ];
60
    }
61
}
62