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

ArrayDuplicateKeys::pass()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 36
ccs 26
cts 26
cp 1
crap 5
rs 8.439
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