Completed
Push — master ( 69939e...ded049 )
by Enrico
02:49
created

CompareWithArray   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
ccs 16
cts 17
cp 0.9412
rs 10
wmc 4
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 18 3
A getRegister() 0 9 1
1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Expression;
4
5
use PhpParser\Node\Expr;
6
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
7
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
8
use PHPSA\Context;
9
10
class CompareWithArray implements AnalyzerPassInterface
11
{
12
    use DefaultMetadataPassTrait;
13
14
    const DESCRIPTION = 'Checks for `{type array} > 1` and similar and suggests use of `count()`.';
15
16
    /**
17
     * @param Expr\BinaryOp $expr
18
     * @param Context $context
19
     * @return bool
20
     */
21 1
    public function pass(Expr\BinaryOp $expr, Context $context)
22
    {
23 1
        $compiler = $context->getExpressionCompiler();
24 1
        $left = $compiler->compile($expr->left);
25 1
        $right = $compiler->compile($expr->right);
26
27 1
        if ($left->isArray() || $right->isArray()) {
28 1
            $context->notice(
29 1
                'compare_with_array',
30 1
                "You are comparing an array. Did you want to use count()?",
31
                $expr
32 1
            );
33
34 1
            return true;
35
        }
36
        
37
        return false;
38
    }
39
40
    /**
41
     * @return array
42
     */
43 1
    public function getRegister()
44
    {
45
        return [
46 1
            Expr\BinaryOp\Greater::class,
47 1
            Expr\BinaryOp\GreaterOrEqual::class,
48 1
            Expr\BinaryOp\Smaller::class,
49 1
            Expr\BinaryOp\SmallerOrEqual::class,
50 1
        ];
51
    }
52
}
53