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

DuplicatedVariablesInUseClosure   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 4
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 20 3
A getRegister() 0 6 1
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