Completed
Pull Request — master (#226)
by
unknown
04:51
created

LogicInversion   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
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 45
ccs 12
cts 12
cp 1
rs 10
wmc 4
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 16 3
A getRegister() 0 6 1
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 static $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 ($expr->expr->getType() === 'Expr_BooleanNot') {
33
            // BooleanNot expression needs a different message.
34 2
            $msg = 'Use "a" expression instead of "!(!a)".';
35 11
        } else if (!array_key_exists($expr->expr->getType(), static::$map)) {
36 11
            return false;
37
        } else {
38 1
            list($use, $instead) = static::$map[$expr->expr->getType()];
39 1
            $msg = sprintf('Use "a %s b" expression instead of "!(a %s b)".', $use, $instead);
40
        }
41
42 2
        $context->notice('logic_inversion', $msg, $expr);
43
44 2
        return true;
45
    }
46
47
    /**
48
     * @return array
49
     */
50 1
    public function getRegister()
51
    {
52
        return [
53 1
            BooleanNot::class,
54 1
        ];
55
    }
56
}
57