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

LogicInversion   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
ccs 10
cts 10
cp 1
rs 10
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegister() 0 6 1
A pass() 0 13 2
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