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

FixedCondition   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 45
ccs 0
cts 21
cp 0
rs 10
wmc 5
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pass() 0 22 4
A getRegister() 0 11 1
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