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

ArrayDuplicateKeys   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 84.85%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 67
ccs 28
cts 33
cp 0.8485
rs 10
wmc 7
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
B pass() 0 36 5
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 16
    public function pass(Expr\Array_ $expr, Context $context)
22
    {
23 16
        $result = false;
24 16
        $keys = [];
25
26
        /** @var Expr\ArrayItem $item */
27 16
        foreach ($expr->items as $item) {
28 8
            if ($item->key instanceof Scalar) {
29 2
                $keyRepresentation = $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...
30 2
                $key = $keyRepresentation;
31 8
            } else if ($item->key instanceof Expr\Variable) {
32 1
                $keyRepresentation = '$'.$item->key->name;
33 1
                $key = 'PHPSA__'.$keyRepresentation;
34 1
            } else {
35 6
                continue;
36
            }
37
38 2
            if (isset($keys[$key])) {
39 1
                $context->notice(
40 1
                    'array.duplicate_keys',
41 1
                    sprintf(
42 1
                        'Duplicate array key "%s" in array definition (previously declared in line %d).',
43 1
                        $keyRepresentation,
44 1
                        $keys[$key]->getLine()
45 1
                    ),
46
                    $item
47 1
                );
48
49 1
                $result = true;
50 1
            }
51
52 2
            $keys[$key] = $item;
53 16
        }
54
55 16
        return $result;
56
    }
57
58
    /**
59
     * @return array
60
     */
61 1
    public function getRegister()
62
    {
63
        return [
64
            Expr\Array_::class
65 1
        ];
66
    }
67
68
    /**
69
     * @return TreeBuilder
70
     */
71
    public function getConfiguration()
72
    {
73
        $treeBuilder = new TreeBuilder();
74
        $treeBuilder->root('array.duplicate_keys')
75
            ->canBeDisabled()
76
        ;
77
78
        return $treeBuilder;
79
    }
80
}
81