Completed
Pull Request — master (#86)
by Kévin
03:01
created

ArrayDuplicateKeys   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 79.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
ccs 19
cts 24
cp 0.7917
rs 10
wmc 6
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B pass() 0 28 4
A getRegister() 0 6 1
A getConfiguration() 0 9 1
1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression;
7
8
use PhpParser\Node\Expr;
9
use PhpParser\Node\Scalar;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
14
class ArrayDuplicateKeys implements Pass\AnalyzerPassInterface, Pass\ConfigurablePassInterface
15
{
16
    /**
17
     * @param Expr\Array_ $expr
18
     * @param Context $context
19
     * @return bool
20
     */
21 15
    public function pass(Expr\Array_ $expr, Context $context)
22
    {
23 15
        $result = false;
24 15
        $keys = [];
25
26
        /** @var Expr\ArrayItem $item */
27 15
        foreach ($expr->items as $item) {
28 7
            if (!$item->key instanceof Scalar) {
29 5
                continue;
30
            }
31
32 2
            $key = $item->key->value;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in PhpParser\Node\Scalar.

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...
33
34 2
            if (isset($keys[$key])) {
35 1
                $context->notice(
36 1
                    'array.duplicate_keys',
37 1
                    sprintf('Duplicate array key "%s" in array definition (previously declared in line %d).', $key, $keys[$key]->getLine()),
38
                    $item
39 1
                );
40
41 1
                $result = true;
42 1
            }
43
44 2
            $keys[$key] = $item;
45 15
        }
46
47 15
        return $result;
48
    }
49
50
    /**
51
     * @return array
52
     */
53 1
    public function getRegister()
54
    {
55
        return [
56
            Expr\Array_::class
57 1
        ];
58
    }
59
60
    /**
61
     * @return TreeBuilder
62
     */
63
    public function getConfiguration()
64
    {
65
        $treeBuilder = new TreeBuilder();
66
        $treeBuilder->root('array.duplicate_keys')
67
            ->canBeDisabled()
68
        ;
69
70
        return $treeBuilder;
71
    }
72
}
73