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 |
||
11 | class FractalUtility implements FractalAwareInterface |
||
12 | { |
||
13 | use FractalAwareTrait; |
||
14 | |||
15 | /** |
||
16 | * Creates the item array and returns it hence it came. |
||
17 | * |
||
18 | * @param array $item The data to parse |
||
19 | * @param TransformerAbstract $transformer The transformer to be used |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | View Code Duplication | public function createItem($item, TransformerAbstract $transformer) |
|
30 | |||
31 | /** |
||
32 | * Creates a collection array and sends it back to hence it came. |
||
33 | * |
||
34 | * @param array $collection |
||
35 | * @param TransformerAbstract $transformer |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | View Code Duplication | public function createCollection(array $collection, TransformerAbstract $transformer) |
|
46 | } |
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.