Completed
Pull Request — master (#226)
by
unknown
06:27 queued 02:06
created

LogicInversion::pass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
1
<?php
2
3
/**
4
 * @author Medvedev Alexandr https://github.com/lexty <[email protected]>
5
 */
6
namespace PHPSA\Analyzer\Pass\Expression;
7
8
use PhpParser\Node\Expr\BooleanNot;
9
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
10
use PHPSA\Context;
11
12
class LogicInversion implements AnalyzerPassInterface
13
{
14
    protected $map = [
15
        'Expr_BinaryOp_Equal' => ['!=', '=='],
16
        'Expr_BinaryOp_NotEqual' => ['==', '!='],
17
        'Expr_BinaryOp_Identical' => ['===', '!=='],
18
        'Expr_BinaryOp_NotIdentical' => ['!==', '==='],
19
        'Expr_BinaryOp_Greater' => ['>', '<='],
20
        'Expr_BinaryOp_GreaterOrEqual' => ['>=', '<'],
21
        'Expr_BinaryOp_Smaller' => ['<', '>='],
22
        'Expr_BinaryOp_SmallerOrEqual' => ['<=', '>'],
23
    ];
24
25
    /**
26
     * @param BooleanNot $expr
27
     * @param Context $context
28
     * @return bool
29
     */
30 11
    public function pass(BooleanNot $expr, Context $context)
31
    {
32 11
        if (!array_key_exists($expr->expr->getType(), $this->map)) {
33 11
            return false;
34
        }
35
        
36 1
        list($use, $instead) = $this->map[$expr->expr->getType()];
37 1
        $msg = sprintf('Use "a %s b" expression instead of "!(a %s b)".', $use, $instead);
38
39 1
        $context->notice('logic_inversion', $msg, $expr);
40
41 1
        return true;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 1
    public function getRegister()
48
    {
49
        return [
50 1
            BooleanNot::class,
51 1
        ];
52
    }
53
}
54