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 |
||
19 | class EnumMap implements ArrayAccess, Countable, SeekableIterator |
||
20 | { |
||
21 | /** |
||
22 | * The classname of the enumeration type |
||
23 | * @var string |
||
24 | */ |
||
25 | private $enumeration; |
||
26 | |||
27 | /** |
||
28 | * Internal map of ordinal number and value |
||
29 | * @var array |
||
30 | */ |
||
31 | private $map = []; |
||
32 | |||
33 | /** |
||
34 | * List of ordinal numbers |
||
35 | * @var int[] |
||
36 | */ |
||
37 | private $ordinals = []; |
||
38 | |||
39 | /** |
||
40 | * Current iterator position |
||
41 | * @var int |
||
42 | */ |
||
43 | private $pos = 0; |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * @param string $enumeration The classname of the enumeration type |
||
48 | * @throws InvalidArgumentException |
||
49 | */ |
||
50 | 26 | public function __construct($enumeration) |
|
51 | { |
||
52 | 26 | if (!\is_subclass_of($enumeration, Enum::class)) { |
|
53 | 2 | throw new InvalidArgumentException(\sprintf( |
|
54 | 2 | "This EnumMap can handle subclasses of '%s' only", |
|
55 | 2 | Enum::class |
|
56 | )); |
||
57 | } |
||
58 | 24 | $this->enumeration = $enumeration; |
|
59 | 24 | } |
|
60 | |||
61 | /** |
||
62 | * Get the classname of the enumeration |
||
63 | * @return string |
||
64 | */ |
||
65 | 2 | public function getEnumeration() |
|
69 | |||
70 | /** |
||
71 | * Get a list of map keys |
||
72 | * @return Enum[] |
||
73 | */ |
||
74 | 6 | public function getKeys() |
|
78 | |||
79 | /** |
||
80 | * Get a list of map values |
||
81 | * @return mixed[] |
||
82 | */ |
||
83 | 4 | public function getValues() |
|
87 | |||
88 | /** |
||
89 | * Search for the given value |
||
90 | * @param mixed $value |
||
91 | * @param bool $strict Use strict type comparison |
||
92 | * @return Enum|null The found key or NULL |
||
93 | */ |
||
94 | 4 | public function search($value, $strict = false) |
|
104 | |||
105 | /** |
||
106 | * Test if the given enumerator exists |
||
107 | * @param Enum|null|boolean|int|float|string $enumerator |
||
108 | * @return boolean |
||
109 | * @see offsetExists |
||
110 | */ |
||
111 | 8 | View Code Duplication | public function contains($enumerator) |
122 | |||
123 | /** |
||
124 | * Test if the given enumerator key exists and is not NULL |
||
125 | * @param Enum|null|boolean|int|float|string $enumerator |
||
126 | * @return boolean |
||
127 | * @see contains |
||
128 | */ |
||
129 | 10 | View Code Duplication | public function offsetExists($enumerator) |
140 | |||
141 | /** |
||
142 | * Get mapped data for the given enumerator |
||
143 | * @param Enum|null|boolean|int|float|string $enumerator |
||
144 | * @return mixed |
||
145 | * @throws InvalidArgumentException On an invalid given enumerator |
||
146 | */ |
||
147 | 8 | public function offsetGet($enumerator) |
|
148 | { |
||
149 | 8 | $enumeration = $this->enumeration; |
|
150 | 8 | $ord = $enumeration::get($enumerator)->getOrdinal(); |
|
151 | 8 | if (!isset($this->map[$ord])) { |
|
152 | throw new UnexpectedValueException(\sprintf( |
||
153 | "Enumerator '%s' could not be found", |
||
154 | \is_object($enumerator) ? $enumerator->getValue() : $enumerator |
||
155 | )); |
||
156 | } |
||
157 | |||
158 | 8 | return $this->map[$ord]; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Attach a new enumerator or overwrite an existing one |
||
163 | * @param Enum|null|boolean|int|float|string $enumerator |
||
164 | * @param mixed $data |
||
165 | * @return void |
||
166 | * @throws InvalidArgumentException On an invalid given enumerator |
||
167 | * @see attach() |
||
168 | */ |
||
169 | 22 | public function offsetSet($enumerator, $value = null) |
|
170 | { |
||
171 | 22 | $enumeration = $this->enumeration; |
|
172 | 22 | $ord = $enumeration::get($enumerator)->getOrdinal(); |
|
173 | |||
174 | 20 | if (!array_key_exists($ord, $this->map)) { |
|
175 | 20 | $this->ordinals[] = $ord; |
|
176 | } |
||
177 | 20 | $this->map[$ord] = $value; |
|
178 | 20 | } |
|
179 | |||
180 | /** |
||
181 | * Detach an existing enumerator |
||
182 | * @param Enum|null|boolean|int|float|string $enumerator |
||
183 | * @return void |
||
184 | * @throws InvalidArgumentException On an invalid given enumerator |
||
185 | * @see detach() |
||
186 | */ |
||
187 | 8 | public function offsetUnset($enumerator) |
|
188 | { |
||
189 | 8 | $enumeration = $this->enumeration; |
|
190 | 8 | $ord = $enumeration::get($enumerator)->getOrdinal(); |
|
191 | |||
192 | 8 | if (($idx = \array_search($ord, $this->ordinals, true)) !== false) { |
|
193 | 8 | unset($this->map[$ord], $this->ordinals[$idx]); |
|
194 | 8 | $this->ordinals = \array_values($this->ordinals); |
|
195 | } |
||
196 | 8 | } |
|
197 | |||
198 | /** |
||
199 | * Seeks to the given iterator position. |
||
200 | * @param int $pos |
||
201 | */ |
||
202 | 2 | public function seek($pos) |
|
211 | |||
212 | /** |
||
213 | * Get the current value |
||
214 | * @return mixed |
||
215 | */ |
||
216 | 4 | public function current() |
|
224 | |||
225 | /** |
||
226 | * Get the current key |
||
227 | * @return Enum|null |
||
228 | */ |
||
229 | 8 | public function key() |
|
238 | |||
239 | /** |
||
240 | * Reset the iterator position to zero. |
||
241 | * @return void |
||
242 | */ |
||
243 | 4 | public function rewind() |
|
247 | |||
248 | /** |
||
249 | * Increment the iterator position by one. |
||
250 | * @return void |
||
251 | */ |
||
252 | 2 | public function next() |
|
256 | |||
257 | /** |
||
258 | * Test if the iterator is in a valid state |
||
259 | * @return boolean |
||
260 | */ |
||
261 | 4 | public function valid() |
|
265 | |||
266 | /** |
||
267 | * Count the number of elements |
||
268 | * |
||
269 | * @return int |
||
270 | */ |
||
271 | 4 | public function count() |
|
275 | } |
||
276 |
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.