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 Javascript 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 Javascript, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait Javascript |
||
8 | { |
||
9 | /** |
||
10 | * The concat() method is used to merge two or more arrays. |
||
11 | * This method does not change the existing arrays, but instead returns a new array. |
||
12 | * |
||
13 | * @return static |
||
14 | */ |
||
15 | 4 | public function concat() |
|
24 | |||
25 | /** |
||
26 | * The copyWithin() method shallow copies part of an array to another location in the same array and returns it, |
||
27 | * without modifying its size. |
||
28 | * |
||
29 | * @param int $target |
||
30 | * @param int $start |
||
31 | * @param int $end |
||
32 | * @return static |
||
33 | */ |
||
34 | public function copyWithin($target, $start, $end) |
||
37 | |||
38 | /** |
||
39 | * The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array. |
||
40 | * |
||
41 | * @return ArrayIterator |
||
42 | */ |
||
43 | 3 | public function entries() |
|
47 | |||
48 | /** |
||
49 | * The every() method tests whether all elements in the array pass the test implemented by the provided function. |
||
50 | * |
||
51 | * @param callable $callback |
||
52 | * @return bool |
||
53 | */ |
||
54 | 1 | View Code Duplication | public function every(callable $callback) |
65 | |||
66 | /** |
||
67 | * The fill() method fills all the elements of an array from a start index to an end index with a static value. |
||
68 | * |
||
69 | * @param $value |
||
70 | * @param $start |
||
71 | * @param $end |
||
72 | * @return static |
||
73 | */ |
||
74 | 1 | public function fill($value, $start = null, $end = null) |
|
105 | |||
106 | /** |
||
107 | * The filter() method creates a new array with all elements that pass the test implemented by the provided function. |
||
108 | * |
||
109 | * @param callable $callback |
||
110 | * @return static |
||
111 | */ |
||
112 | 1 | public function filter(callable $callback) |
|
116 | |||
117 | /** |
||
118 | * The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. |
||
119 | * |
||
120 | * @param callable $callback |
||
121 | * @return mix |
||
122 | */ |
||
123 | 1 | View Code Duplication | public function find(callable $callback) |
132 | |||
133 | /** |
||
134 | * The findIndex() method returns an index of the first element in the array that satisfies the provided testing function. |
||
135 | * Otherwise -1 is returned. |
||
136 | * |
||
137 | * @param callable $callback |
||
138 | * @return int|string |
||
139 | */ |
||
140 | 1 | View Code Duplication | public function findIndex(callable $callback) |
151 | |||
152 | /** |
||
153 | * The forEach() method executes a provided function once for each array element. |
||
154 | * php < 7 not allow forEach. |
||
155 | * |
||
156 | * @param callable $callback |
||
157 | */ |
||
158 | 1 | public function each(callable $callback) |
|
165 | |||
166 | /** |
||
167 | * The includes() method determines whether an array includes a certain element, returning true or false as appropriate. |
||
168 | * |
||
169 | * @param mixed $searchElement |
||
170 | * @param int $fromIndex |
||
171 | * @return bool |
||
172 | */ |
||
173 | 1 | public function includes($searchElement, $fromIndex = 0) |
|
177 | |||
178 | /** |
||
179 | * The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. |
||
180 | * |
||
181 | * @param mixed $searchElement |
||
182 | * @param int $fromIndex |
||
183 | * @return int|string |
||
184 | */ |
||
185 | 1 | View Code Duplication | public function indexOf($searchElement, $fromIndex = 0) |
198 | |||
199 | /** |
||
200 | * The join() method joins all elements of an array into a string. |
||
201 | * |
||
202 | * @param string $separator |
||
203 | * @return \Recca0120\Lodash\Str; |
||
204 | */ |
||
205 | 4 | public function join($separator = ',') |
|
209 | |||
210 | /** |
||
211 | * The keys() method returns a new Array Iterator that contains the keys for each index in the array. |
||
212 | * |
||
213 | * @return \ArrayIterator |
||
214 | */ |
||
215 | 1 | public function keys() |
|
219 | |||
220 | /** |
||
221 | * The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex. |
||
222 | * |
||
223 | * @param mixed $searchElement |
||
224 | * @param int $fromIndex |
||
225 | * @return int|string |
||
226 | */ |
||
227 | 1 | View Code Duplication | public function lastIndexOf($searchElement, $fromIndex = 0) |
236 | |||
237 | /** |
||
238 | * The map() method creates a new array with the results of calling a provided function on every element in this array. |
||
239 | * |
||
240 | * @param callable $callback |
||
241 | * @return static |
||
242 | */ |
||
243 | 2 | public function map(callable $callback) |
|
247 | |||
248 | /** |
||
249 | * The pop() method removes the last element from an array and returns that element. This method changes the length of the array. |
||
250 | * |
||
251 | * @return static |
||
252 | */ |
||
253 | 1 | public function pop() |
|
261 | |||
262 | /** |
||
263 | * The push() method adds one or more elements to the end of an array and returns the new length of the array. |
||
264 | * |
||
265 | * @return int |
||
266 | */ |
||
267 | 1 | public function push() |
|
277 | |||
278 | /** |
||
279 | * The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. |
||
280 | * |
||
281 | * @param callable $callback |
||
282 | * @param mixed $initialValue |
||
283 | * @return mixed |
||
284 | */ |
||
285 | 2 | public function reduce(callable $callback, $initialValue = null) |
|
289 | |||
290 | /** |
||
291 | * The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) has to reduce it to a single value. |
||
292 | * |
||
293 | * @param callable $callback |
||
294 | * @param mixed $initialValue |
||
295 | * @return mixed |
||
296 | */ |
||
297 | 1 | public function reduceRight(callable $callback, $initialValue = null) |
|
301 | |||
302 | /** |
||
303 | * The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first. |
||
304 | * |
||
305 | * @param bool $preservekeys |
||
306 | * @return static |
||
307 | */ |
||
308 | 2 | public function reverse($preservekeys = false) |
|
316 | |||
317 | /** |
||
318 | * The shift() method removes the first element from an array and returns that element. This method changes the length of the array. |
||
319 | * |
||
320 | * @return mix |
||
321 | */ |
||
322 | 1 | public function shift() |
|
330 | |||
331 | /** |
||
332 | * The slice() method returns a shallow copy of a portion of an array |
||
333 | * into a new array object selected from begin to end (end not included). The original array will not be modified. |
||
334 | * |
||
335 | * @param int $begin |
||
336 | * @param int $end |
||
337 | * @return static |
||
338 | */ |
||
339 | 2 | public function slice($begin, $end = null) |
|
354 | |||
355 | /** |
||
356 | * The some() method tests whether some element in the array passes the test implemented by the provided function. |
||
357 | * |
||
358 | * @param callable $callback |
||
359 | * @return bool |
||
360 | */ |
||
361 | 1 | View Code Duplication | public function some(callable $callback) |
372 | |||
373 | /** |
||
374 | * The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points. |
||
375 | * |
||
376 | * @param callable|null $compareFunction |
||
377 | * @return static |
||
378 | */ |
||
379 | 1 | public function sort(callable $compareFunction = null) |
|
392 | |||
393 | /** |
||
394 | * The splice() method changes the content of an array by removing existing elements and/or adding new elements. |
||
395 | * |
||
396 | * @return static |
||
397 | */ |
||
398 | 1 | View Code Duplication | public function splice() |
406 | |||
407 | public function toLocaleString() |
||
410 | |||
411 | /** |
||
412 | * The toString() method returns a string representing the specified array and its elements. |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | 1 | public function toString() |
|
420 | |||
421 | /** |
||
422 | * The toString() method returns a string representing the specified array and its elements. |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 1 | public function __toString() |
|
430 | |||
431 | /** |
||
432 | * The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. |
||
433 | * |
||
434 | * @return static |
||
435 | */ |
||
436 | 1 | View Code Duplication | public function unshift() |
444 | |||
445 | /** |
||
446 | * The values() method returns a new Array Iterator object that contains the values for each index in the array. |
||
447 | * |
||
448 | * @return \ArrayIterator |
||
449 | */ |
||
450 | 1 | public function values() |
|
454 | |||
455 | /** |
||
456 | * length. |
||
457 | * |
||
458 | * @return int |
||
459 | */ |
||
460 | 3 | public function length() |
|
464 | } |
||
465 |
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.