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 (is_array($values) || $values instanceof Traversable) { |
||
27 | $this->pushAll($values); |
||
28 | } elseif (is_integer($values)) { |
||
29 | $this->allocate($values); |
||
|
|||
30 | } |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | public function toArray(): array |
||
37 | { |
||
38 | return $this->internal; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | public function merge($values): \Ds\Sequence |
||
45 | { |
||
46 | $merged = $this->copy(); |
||
47 | $merged->pushAll($values); |
||
48 | |||
49 | return $merged; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function count(): int |
||
56 | { |
||
57 | return count($this->internal); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | View Code Duplication | public function contains(...$values): bool |
|
64 | { |
||
65 | if ( ! $values) { |
||
66 | return false; |
||
67 | } |
||
68 | |||
69 | foreach ($values as $value) { |
||
70 | if ($this->find($value) === false) { |
||
71 | return false; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return true; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | public function filter(callable $callback = null): \Ds\Sequence |
||
82 | { |
||
83 | if ($callback) { |
||
84 | return new self(array_filter($this->internal, $callback)); |
||
85 | } |
||
86 | |||
87 | return new self(array_filter($this->internal)); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function find($value) |
||
94 | { |
||
95 | return array_search($value, $this->internal, true); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function first() |
||
102 | { |
||
103 | if (empty($this->internal)) { |
||
104 | throw new UnderflowException(); |
||
105 | } |
||
106 | |||
107 | return $this->internal[0]; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function get(int $index) |
||
114 | { |
||
115 | $this->checkRange($index); |
||
116 | |||
117 | return $this->internal[$index]; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | public function insert(int $index, ...$values) |
||
124 | { |
||
125 | if ($index < 0 || $index > count($this->internal)) { |
||
126 | throw new OutOfRangeException(); |
||
127 | } |
||
128 | |||
129 | array_splice($this->internal, $index, 0, $values); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | public function join(string $glue = null): string |
||
136 | { |
||
137 | return implode($glue, $this->internal); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function last() |
||
144 | { |
||
145 | if ($this->isEmpty()) { |
||
146 | throw new UnderflowException(); |
||
147 | } |
||
148 | |||
149 | return end($this->internal); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | public function map(callable $callback): \Ds\Sequence |
||
156 | { |
||
157 | return new self(array_map($callback, $this->internal)); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | View Code Duplication | public function pop() |
|
164 | { |
||
165 | if ($this->isEmpty()) { |
||
166 | throw new UnderflowException(); |
||
167 | } |
||
168 | |||
169 | $value = array_pop($this->internal); |
||
170 | $this->adjustCapacity(); |
||
171 | |||
172 | return $value; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function push(...$values) |
||
179 | { |
||
180 | if ($values) { |
||
181 | array_push($this->internal, ...$values); |
||
182 | $this->adjustCapacity(); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | public function pushAll($values) |
||
190 | { |
||
191 | if ( ! is_array($values) && ! $values instanceof Traversable) { |
||
192 | throw new Error(); |
||
193 | } |
||
194 | |||
195 | $this->push(...$values); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | public function reduce(callable $callback, $initial = null) |
||
202 | { |
||
203 | return array_reduce($this->internal, $callback, $initial); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @inheritDoc |
||
208 | */ |
||
209 | public function remove(int $index) |
||
210 | { |
||
211 | $this->checkRange($index); |
||
212 | |||
213 | $value = array_splice($this->internal, $index, 1, null)[0]; |
||
214 | $this->adjustCapacity(); |
||
215 | |||
216 | return $value; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @inheritDoc |
||
221 | */ |
||
222 | public function reverse(): \Ds\Sequence |
||
223 | { |
||
224 | return new self(array_reverse($this->internal)); |
||
225 | |||
226 | } |
||
227 | |||
228 | private function reverseRange(int $a, int $b) |
||
229 | { |
||
230 | $swap = function(&$a, &$b) { |
||
231 | $t = $a; |
||
232 | $a = $b; |
||
233 | $b = $t; |
||
234 | }; |
||
235 | |||
236 | while (--$b > $a) { |
||
237 | $swap($this->internal[$a++], $this->internal[$b--]); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | private function normalizeRotations(int $rotations, int $count) |
||
242 | { |
||
243 | if ($rotations < 0) { |
||
244 | return $count - (abs($rotations) % $count); |
||
245 | } |
||
246 | |||
247 | return $rotations % $count; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | public function rotate(int $rotations) |
||
254 | { |
||
255 | if ($this->isEmpty()) { |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | $n = count($this); |
||
260 | $r = $this->normalizeRotations($rotations, $n); |
||
261 | |||
262 | if ($r > 0) { |
||
263 | $this->reverseRange(0, $r); |
||
264 | $this->reverseRange($r, $n); |
||
265 | $this->reverseRange(0, $n); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @inheritDoc |
||
271 | */ |
||
272 | public function set(int $index, $value) |
||
277 | |||
278 | /** |
||
279 | * @inheritDoc |
||
280 | */ |
||
281 | View Code Duplication | public function shift() |
|
292 | |||
293 | /** |
||
294 | * @inheritDoc |
||
295 | */ |
||
296 | public function slice(int $offset, int $length = null): \Ds\Sequence |
||
304 | |||
305 | /** |
||
306 | * @inheritDoc |
||
307 | */ |
||
308 | public function sort(callable $comparator = null): \Ds\Sequence |
||
320 | |||
321 | /** |
||
322 | * @inheritDoc |
||
323 | */ |
||
324 | public function unshift(...$values) |
||
331 | |||
332 | /** |
||
333 | * Check Range |
||
334 | * |
||
335 | * @param int $index |
||
336 | */ |
||
337 | private function checkRange(int $index) |
||
343 | |||
344 | /** |
||
345 | * Get Iterator |
||
346 | */ |
||
347 | public function getIterator() |
||
353 | |||
354 | /** |
||
355 | * |
||
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.