FixedCondition   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A 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\Helper\DefaultMetadataPassTrait;
15
use PHPSA\Analyzer\Pass;
16
use PHPSA\Context;
17
18
class FixedCondition implements Pass\AnalyzerPassInterface
19
{
20
    use DefaultMetadataPassTrait;
21
22
    /**
23
     * @param $stmt
24
     * @param Context $context
25
     * @return bool
26
     */
27
    public function pass($stmt, Context $context)
28
    {
29
        $condition = $stmt->cond;
30
31
        if ($stmt instanceof For_ && count($stmt->cond) > 0) { // For is the only one that has an array as condition
32
            $condition = $condition[0];
33
        }
34
35
        $expression = $context->getExpressionCompiler()->compile($condition);
36
37
        if ($expression->hasValue()) { // @todo implement isStatic() method to see if expression changes or not
38
            $context->notice(
39
                'fixed_condition',
40
                'The condition will always result in the same boolean value',
41
                $stmt
42
            );
43
44
            return true;
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getRegister()
54
    {
55
        return [
56
            If_::class,
57
            ElseIf_::class,
58
            While_::class,
59
            For_::class,
60
            Do_::class,
61
            Switch_::class,
62
        ];
63
    }
64
}
65