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 |
||
12 | View Code Duplication | trait ChainTrait |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * Returns an iterator that returns elements from the first iterable |
||
16 | * until it is exhausted, then proceeds to the next iterable, until |
||
17 | * all the iterables are exhausted. |
||
18 | * |
||
19 | * Used for creating consecutive sequences as a single sequence. |
||
20 | * |
||
21 | * Note that the keys of the returned ChainIterator follow 0, 1, etc, |
||
22 | * regardless of the keys given in the iterables. |
||
23 | * |
||
24 | * > iter\iterable([1, 2, 3], [4, 5, 6])->chain() |
||
25 | * 1 2 3 4 5 6 |
||
26 | * |
||
27 | * > iter\iterable('ABC', 'DEF')->chain() |
||
28 | * A B C D E F |
||
29 | * |
||
30 | * @param array|string|\Iterator $iterable |
||
31 | * @param array|string|\Iterator $iterable2 |
||
32 | * @return null|ChainIterator |
||
33 | */ |
||
34 | 12 | public function chain(/* $iterable, $iterable2, ... */) |
|
47 | } |
||
48 |
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.