Completed
Push — master ( 354ecf...ba9f5a )
by Дмитрий
03:37
created

DuplicatedVariablesInUseClosure::pass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 3
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Strahinja Djuric https://github.com/kilgaloon <[email protected]>
4
 */
5
namespace PHPSA\Analyzer\Pass\Expression;
6
7
use PHPSA\Context;
8
use PHPSA\Analyzer\Pass;
9
use PhpParser\Node\Expr\Closure;
10
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
11
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
12
13
class DuplicatedVariablesInUseClosure implements AnalyzerPassInterface
14
{
15
    use DefaultMetadataPassTrait;
16
17
    const DESCRIPTION = 'Check for duplicate variables in use statement';
18
19
    /**
20
     * @param Closure $expr
21
     * @param Context $context
22
     * @return bool
23
     */
24 2
    public function pass(Closure $expr, Context $context)
25
    {
26 2
        $varUsed = [];
27 2
        foreach ($expr->uses as $use) {
28 1
            $var = $context->getExpressionCompiler()->compile($use->var);
29 1
            if (in_array($var->getValue(), $varUsed)) {
30 1
                $context->notice(
31 1
                    'duplicated_variable_in_use_closure',
32 1
                    sprintf("Duplicated variable $%s in use statement.", $use->var),
33
                    $expr
34 1
                );
35
36 1
                return false;
37
            } else {
38 1
                array_push($varUsed, $use->var);
39
            }
40 2
        }
41
42 2
        return true;
43
    }
44
45 2
    public function getRegister()
46
    {
47
        return [
48
            Closure::class
49 2
        ];
50
    }
51
}
52