Completed
Push — master ( f1eb50...6d2eba )
by Enrico
05:39 queued 05:34
created

FixedCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 44
ccs 22
cts 22
cp 1
rs 10
c 1
b 1
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pass() 0 23 5
A getRegister() 0 9 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\Switch_;
11
use PhpParser\Node\Stmt\While_;
12
use PHPSA\Analyzer\Pass;
13
use PHPSA\Context;
14
15
class FixedCondition implements Pass\AnalyzerPassInterface
16
{
17
    /**
18
     * @param $stmt
19
     * @param Context $context
20
     * @return bool
21
     */
22 13
    public function pass($stmt, Context $context)
23
    {
24 13
        $result = false;
25 13
        $expression = $context->getExpressionCompiler()->compile($stmt->cond);
26
27 13
        if ($expression->hasValue()) {
28 13
            $context->notice(
29 13
                'fixed_condition',
30 13
                'The condition will always result in the same boolean value',
31
                $stmt
32 13
            );
33
34 13
            $result = true;
35 13
        }
36
37 13
        if ($stmt instanceof If_) {
38 8
            foreach ($stmt->elseifs as $elseif) {
39 3
                $result = $this->pass($elseif, $context) || $result;
40 8
            }
41 8
        }
42
43 13
        return $result;
44
    }
45
46
    /**
47
     * @return array
48
     */
49 1
    public function getRegister()
50
    {
51
        return [
52 1
            If_::class,
53 1
            While_::class,
54 1
            Do_::class,
55 1
            Switch_::class,
56 1
        ];
57
    }
58
}
59