Test Failed
Branch lab/data (a414ec)
by Gabor
07:41
created

EntitySet   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 163
Duplicated Lines 21.47 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 35
loc 163
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 11 11 2
A offsetGet() 11 11 2
B offsetSet() 0 24 6
A offsetUnset() 13 13 3
A rewind() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
A toArray() 0 4 1
A merge() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Entity;
15
16
use ArrayAccess;
17
use InvalidArgumentException;
18
use Iterator;
19
20
/**
21
 * Class EntitySet.
22
 *
23
 * We have some strictness additions to the original ArrayAccess and Iterator interfaces.
24
 * A) The offset must be Integer.
25
 * B) The value must be an object that implements the EntityInterface.
26
 */
27
class EntitySet implements ArrayAccess, Iterator
28
{
29
    /**
30
     * @var array
31
     */
32
    private $container = [];
33
34
    /**
35
     * Checks whether an offset exists.
36
     *
37
     * @param int $offset
38
     * @throws InvalidArgumentException
39
     * @return bool
40
     */
41 View Code Duplication
    public function offsetExists($offset) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if (!is_int($offset)) {
44
            throw new InvalidArgumentException(
45
                sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)),
46
                1000
47
            );
48
        }
49
50
        return isset($this->container[$offset]);
51
    }
52
53
    /**
54
     * Returns the value at an offset.
55
     *
56
     * @param int $offset
57
     * @throws InvalidArgumentException
58
     * @return null|EntityInterface
59
     */
60 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (!is_int($offset)) {
63
            throw new InvalidArgumentException(
64
                sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)),
65
                1001
66
            );
67
        }
68
69
        return $this->container[$offset] ?? null;
70
    }
71
72
    /**
73
     * Sets a value for an offset. It appends it if offset is Null.
74
     *
75
     * @param null|int $offset
76
     * @param EntityInterface $value
77
     * @throws InvalidArgumentException
78
     */
79
    public function offsetSet($offset, $value) : void
80
    {
81
        if (is_null($offset)) {
82
            $offset = empty($this->container) ? 0 : max(array_keys($this->container)) + 1;
83
        }
84
85
        if (!is_int($offset)) {
86
            throw new InvalidArgumentException(
87
                sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)),
88
                1002
89
            );
90
        }
91
92
        if (!$value instanceof EntityInterface) {
93
            $valueType = is_object($value) ? get_class($value) : gettype($value);
94
95
            throw new InvalidArgumentException(
96
                sprintf(__METHOD__.' requires parameter 2 to be an instance of EntityInterface, %s given.', $valueType),
97
                1003
98
            );
99
        }
100
101
        $this->container[$offset] = $value;
102
    }
103
104
    /**
105
     * Deletes a record from the container at an offset. It will not reorder the container.
106
     *
107
     * @param mixed $offset
108
     * @throws InvalidArgumentException
109
     */
110 View Code Duplication
    public function offsetUnset($offset) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        if (!is_int($offset)) {
113
            throw new InvalidArgumentException(
114
                sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)),
115
                1004
116
            );
117
        }
118
119
        if ($this->offsetExists($offset)) {
120
            unset($this->container[$offset]);
121
        }
122
    }
123
124
    /**
125
     * Rewinds the Iterator to the first element.
126
     */
127
    public function rewind() : void
128
    {
129
        reset($this->container);
130
    }
131
132
    /**
133
     * Returns the current element.
134
     *
135
     * @return mixed
136
     */
137
    public function current()
138
    {
139
        return current($this->container);
140
    }
141
142
    /**
143
     * Returns the key of the current element.
144
     *
145
     * @return int|mixed|null|string
146
     */
147
    public function key()
148
    {
149
        return key($this->container);
150
    }
151
152
    /**
153
     * Moves forward to next element.
154
     */
155
    public function next() : void
156
    {
157
        next($this->container);
158
    }
159
160
    /**
161
     * Checks if current position is valid.
162
     *
163
     * @return bool
164
     */
165
    public function valid() : bool
166
    {
167
        return key($this->container) !== null;
168
    }
169
170
    /**
171
     * Return the raw container
172
     *
173
     * @return array
174
     */
175
    public function toArray() : array
176
    {
177
        return $this->container;
178
    }
179
180
    /**
181
     * Merges another EntitySet into the current container.
182
     *
183
     * @param EntitySet $entitySet
184
     */
185
    public function merge(EntitySet $entitySet) : void
186
    {
187
        $this->container = array_merge($this->container, $entitySet->toArray());
188
    }
189
}
190