Completed
Push — master ( 28d1f7...8f0d0f )
by Enrico
05:45
created

CompareWithArray::pass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 2
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
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 16
    public function pass(Expr\BinaryOp $expr, Context $context)
22
    {
23 16
        $compiler = $context->getExpressionCompiler();
24 16
        $left = $compiler->compile($expr->left);
25 16
        $right = $compiler->compile($expr->right);
26
27 16
        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 15
        return false;
38
    }
39
40
    /**
41
     * @return array
42
     */
43 2
    public function getRegister()
44
    {
45
        return [
46 2
            Expr\BinaryOp\Greater::class,
47 2
            Expr\BinaryOp\GreaterOrEqual::class,
48 2
            Expr\BinaryOp\Smaller::class,
49 2
            Expr\BinaryOp\SmallerOrEqual::class,
50 2
        ];
51
    }
52
}
53