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:
Complex classes like Sequence often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sequence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait Sequence |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $internal = []; |
||
20 | |||
21 | /** |
||
22 | * @inheritDoc |
||
23 | */ |
||
24 | public function __construct($values = null) |
||
25 | { |
||
26 | if (func_num_args()) { |
||
27 | $this->push(...$values); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | public function toArray(): array |
||
35 | { |
||
36 | return $this->internal; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | public function apply(callable $callback) |
||
43 | { |
||
44 | foreach ($this->internal as &$value) { |
||
45 | $value = $callback($value); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public function merge($values): \Ds\Sequence |
||
53 | { |
||
54 | if ( ! is_array($values)) { |
||
55 | $values = iterator_to_array($values); |
||
56 | } |
||
57 | |||
58 | return new self(array_merge($this->internal, $values)); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | public function count(): int |
||
65 | { |
||
66 | return count($this->internal); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | public function contains(...$values): bool |
||
73 | { |
||
74 | foreach ($values as $value) { |
||
75 | if ($this->find($value) === false) { |
||
76 | return false; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return true; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritDoc |
||
85 | */ |
||
86 | public function filter(callable $callback = null): \Ds\Sequence |
||
87 | { |
||
88 | return new self(array_filter($this->internal, $callback ?: 'boolval')); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | public function find($value) |
||
95 | { |
||
96 | return array_search($value, $this->internal, true); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritDoc |
||
101 | */ |
||
102 | public function first() |
||
103 | { |
||
104 | if (empty($this->internal)) { |
||
105 | throw new UnderflowException(); |
||
106 | } |
||
107 | |||
108 | return $this->internal[0]; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | public function get(int $index) |
||
115 | { |
||
116 | $this->checkRange($index); |
||
117 | |||
118 | return $this->internal[$index]; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | */ |
||
124 | public function insert(int $index, ...$values) |
||
125 | { |
||
126 | if ($index < 0 || $index > count($this->internal)) { |
||
127 | throw new OutOfRangeException(); |
||
128 | } |
||
129 | |||
130 | array_splice($this->internal, $index, 0, $values); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @inheritDoc |
||
135 | */ |
||
136 | public function join(string $glue = null): string |
||
137 | { |
||
138 | return implode($glue, $this->internal); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | public function last() |
||
145 | { |
||
146 | if ($this->isEmpty()) { |
||
|
|||
147 | throw new UnderflowException(); |
||
148 | } |
||
149 | |||
150 | return end($this->internal); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @inheritDoc |
||
155 | */ |
||
156 | public function map(callable $callback): \Ds\Sequence |
||
157 | { |
||
158 | return new self(array_map($callback, $this->internal)); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @inheritDoc |
||
163 | */ |
||
164 | View Code Duplication | public function pop() |
|
165 | { |
||
166 | if ($this->isEmpty()) { |
||
167 | throw new UnderflowException(); |
||
168 | } |
||
169 | |||
170 | $value = array_pop($this->internal); |
||
171 | $this->adjustCapacity(); |
||
172 | |||
173 | return $value; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @inheritDoc |
||
178 | */ |
||
179 | public function push(...$values) |
||
180 | { |
||
181 | if ($values) { |
||
182 | array_push($this->internal, ...$values); |
||
183 | $this->adjustCapacity(); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @inheritDoc |
||
189 | */ |
||
190 | public function reduce(callable $callback, $initial = null) |
||
191 | { |
||
192 | return array_reduce($this->internal, $callback, $initial); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @inheritDoc |
||
197 | */ |
||
198 | public function remove(int $index) |
||
199 | { |
||
200 | $this->checkRange($index); |
||
201 | |||
202 | $value = array_splice($this->internal, $index, 1, null)[0]; |
||
203 | $this->adjustCapacity(); |
||
204 | |||
205 | return $value; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @inheritDoc |
||
210 | */ |
||
211 | public function reverse() |
||
212 | { |
||
213 | $this->internal = array_reverse($this->internal); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @inheritDoc |
||
218 | */ |
||
219 | public function reversed(): \Ds\Sequence |
||
220 | { |
||
221 | return new self(array_reverse($this->internal)); |
||
222 | } |
||
223 | |||
224 | private function normalizeRotations(int $rotations, int $count) |
||
225 | { |
||
226 | if ($rotations < 0) { |
||
227 | return $count - (abs($rotations) % $count); |
||
228 | } |
||
229 | |||
230 | return $rotations % $count; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @inheritDoc |
||
235 | */ |
||
236 | public function rotate(int $rotations) |
||
237 | { |
||
238 | if (count($this) < 2) { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | $rotations = $this->normalizeRotations($rotations, count($this)); |
||
243 | |||
244 | while ($rotations--) { |
||
245 | $this->push($this->shift()); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @inheritDoc |
||
251 | */ |
||
252 | public function set(int $index, $value) |
||
253 | { |
||
254 | $this->checkRange($index); |
||
255 | $this->internal[$index] = $value; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @inheritDoc |
||
260 | */ |
||
261 | View Code Duplication | public function shift() |
|
262 | { |
||
263 | if ($this->isEmpty()) { |
||
264 | throw new UnderflowException(); |
||
265 | } |
||
266 | |||
267 | $value = array_shift($this->internal); |
||
268 | $this->adjustCapacity(); |
||
269 | |||
270 | return $value; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @inheritDoc |
||
275 | */ |
||
276 | public function slice(int $offset, int $length = null): \Ds\Sequence |
||
277 | { |
||
278 | if (func_num_args() === 1) { |
||
279 | $length = count($this); |
||
280 | } |
||
281 | |||
282 | return new self(array_slice($this->internal, $offset, $length)); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @inheritDoc |
||
287 | */ |
||
288 | public function sort(callable $comparator = null) |
||
289 | { |
||
290 | if ($comparator) { |
||
291 | usort($this->internal, $comparator); |
||
292 | } else { |
||
293 | sort($this->internal); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @inheritDoc |
||
299 | */ |
||
300 | public function sorted(callable $comparator = null): \Ds\Sequence |
||
301 | { |
||
302 | $internal = $this->internal; |
||
303 | |||
304 | if ($comparator) { |
||
305 | usort($internal, $comparator); |
||
306 | } else { |
||
307 | sort($internal); |
||
308 | } |
||
309 | |||
310 | return new self($internal); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @inheritDoc |
||
315 | */ |
||
316 | public function sum() |
||
317 | { |
||
318 | return array_sum($this->internal); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @inheritDoc |
||
323 | */ |
||
324 | public function unshift(...$values) |
||
331 | |||
332 | /** |
||
333 | * |
||
334 | * |
||
335 | * @param int $index |
||
336 | */ |
||
337 | private function checkRange(int $index) |
||
343 | |||
344 | /** |
||
345 | * |
||
346 | */ |
||
347 | public function getIterator() |
||
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | public function clear() |
||
362 | |||
363 | /** |
||
364 | * @inheritdoc |
||
365 | */ |
||
366 | public function offsetSet($offset, $value) |
||
374 | |||
375 | /** |
||
376 | * @inheritdoc |
||
377 | */ |
||
378 | public function &offsetGet($offset) |
||
383 | |||
384 | /** |
||
385 | * @inheritdoc |
||
386 | */ |
||
387 | public function offsetUnset($offset) |
||
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | public function offsetExists($offset) |
||
406 | } |
||
407 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.