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 |
||
7 | View Code Duplication | class HasOne extends HasOneOrMany |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * Get the results of the relationship. |
||
11 | * |
||
12 | * @param $relation |
||
13 | * @return mixed |
||
14 | */ |
||
15 | public function getResults($relation) |
||
23 | |||
24 | /** |
||
25 | * Get the results of the relationship. |
||
26 | * |
||
27 | * @return mixed |
||
28 | */ |
||
29 | public function fetch() |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Initialize the relation on a set of entities. |
||
37 | * |
||
38 | * @param \Analogue\ORM\Entity[] $entities |
||
39 | * @param string $relation |
||
40 | * @return array |
||
41 | */ |
||
42 | public function initRelation(array $entities, $relation) |
||
50 | |||
51 | /** |
||
52 | * Match the eagerly loaded results to their parents. |
||
53 | * |
||
54 | * @param \Analogue\ORM\Entity[] $entities |
||
55 | * @param EntityCollection $results |
||
56 | * @param string $relation |
||
57 | * @return array |
||
58 | */ |
||
59 | public function match(array $entities, EntityCollection $results, $relation) |
||
63 | } |
||
64 |
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.