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

CompareWithArray::pass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 2
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.4285
c 0
b 0
f 0
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