Completed
Push — master ( 9b293b...400279 )
by Дмитрий
02:39
created

CompareWithArray   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

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

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 $expr
18
     * @param Context $context
19
     * @return bool
20
     */
21 1
    public function pass(Expr $expr, Context $context)
22
    {
23 1
        $compiler = $context->getExpressionCompiler();
24 1
        $left = $compiler->compile($expr->left);
0 ignored issues
show
Bug introduced by
The property left does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
25 1
        $right = $compiler->compile($expr->right);
0 ignored issues
show
Bug introduced by
The property right does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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