FieldDescriptionCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A getElements() 0 4 1
A has() 0 4 1
A get() 0 8 2
A remove() 0 6 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A count() 0 4 1
A reorder() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Admin;
15
16
/**
17
 * @author Thomas Rabaix <[email protected]>
18
 */
19
final class FieldDescriptionCollection implements \ArrayAccess, \Countable
20
{
21
    /**
22
     * @var FieldDescriptionInterface[]
23
     */
24
    private $elements = [];
25
26
    public function add(FieldDescriptionInterface $fieldDescription): void
27
    {
28
        $this->elements[$fieldDescription->getName()] = $fieldDescription;
29
    }
30
31
    public function getElements(): array
32
    {
33
        return $this->elements;
34
    }
35
36
    public function has(string $name): bool
37
    {
38
        return \array_key_exists($name, $this->elements);
39
    }
40
41
    /**
42
     * @throws \InvalidArgumentException
43
     */
44
    public function get(string $name): FieldDescriptionInterface
45
    {
46
        if ($this->has($name)) {
47
            return $this->elements[$name];
48
        }
49
50
        throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
51
    }
52
53
    public function remove(string $name): void
54
    {
55
        if ($this->has($name)) {
56
            unset($this->elements[$name]);
57
        }
58
    }
59
60
    public function offsetExists($offset): bool
61
    {
62
        return $this->has($offset);
63
    }
64
65
    public function offsetGet($offset): FieldDescriptionInterface
66
    {
67
        return $this->get($offset);
68
    }
69
70
    public function offsetSet($offset, $value): void
71
    {
72
        throw new \RuntimeException('Cannot set value, use add');
73
    }
74
75
    public function offsetUnset($offset): void
76
    {
77
        $this->remove($offset);
78
    }
79
80
    public function count(): int
81
    {
82
        return \count($this->elements);
83
    }
84
85
    public function reorder(array $keys): void
86
    {
87
        if ($this->has('batch')) {
88
            array_unshift($keys, 'batch');
89
        }
90
91
        $this->elements = array_merge(array_flip($keys), $this->elements);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array_flip($keys), $this->elements) of type array is incompatible with the declared type array<integer,object<Son...dDescriptionInterface>> of property $elements.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
    }
93
}
94