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

ArrayDuplicateKeys::getRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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