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 EloquentReadRepository extends BaseEloquentRepository implements ReadRepository |
||
12 | { |
||
13 | /** |
||
14 | * Retrieves an entity by its id. |
||
15 | * |
||
16 | * @param Identity $id |
||
17 | * @param Fields|null $fields |
||
18 | * |
||
19 | * @return array |
||
20 | */ |
||
21 | public function find(Identity $id, Fields $fields = null) |
||
28 | |||
29 | /** |
||
30 | * Returns all instances of the type. |
||
31 | * |
||
32 | * @param Filter|null $filter |
||
33 | * @param Sort|null $sort |
||
34 | * @param Fields|null $fields |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | View Code Duplication | public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
|
54 | |||
55 | /** |
||
56 | * Returns all instances of the type meeting $distinctFields values. |
||
57 | * |
||
58 | * @param Fields $distinctFields |
||
59 | * @param Filter|null $filter |
||
60 | * @param Sort|null $sort |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | View Code Duplication | public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) |
|
81 | |||
82 | /** |
||
83 | * Returns the total amount of elements in the repository given the restrictions provided by the Filter object. |
||
84 | * |
||
85 | * @param Filter|null $filter |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | public function count(Filter $filter = null) |
||
93 | |||
94 | /** |
||
95 | * Returns whether an entity with the given id exists. |
||
96 | * |
||
97 | * @param $id |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function exists(Identity $id) |
||
105 | } |
||
106 |
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.