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 |
||
23 | class QueryObject extends Select implements QueryObjectInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var RepositoryInterface |
||
28 | */ |
||
29 | protected $repository; |
||
30 | |||
31 | /** |
||
32 | * QueryObject has a repository as a dependency. |
||
33 | * |
||
34 | * @param RepositoryInterface $repository |
||
35 | */ |
||
36 | 10 | public function __construct(RepositoryInterface $repository) |
|
44 | |||
45 | /** |
||
46 | * Retrieve all records matching this select query |
||
47 | * |
||
48 | * @return \Slick\Database\RecordList |
||
49 | */ |
||
50 | 6 | public function all() |
|
67 | |||
68 | /** |
||
69 | * Retrieve first record matching this select query |
||
70 | * |
||
71 | * @return EntityInterface|null |
||
72 | */ |
||
73 | 2 | public function first() |
|
95 | |||
96 | /** |
||
97 | * Gets the id for this query |
||
98 | * |
||
99 | * @param QueryObjectInterface $query |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 8 | protected function getId(QueryObjectInterface $query) |
|
110 | |||
111 | /** |
||
112 | * Registers every entity in collection to the repository identity map |
||
113 | * |
||
114 | * @param EntityCollection $collection |
||
115 | * |
||
116 | * @return self |
||
117 | */ |
||
118 | 6 | protected function updateIdentityMap(EntityCollection $collection) |
|
130 | |||
131 | /** |
||
132 | * Returns the repository that is using this query object |
||
133 | * |
||
134 | * @return RepositoryInterface |
||
135 | */ |
||
136 | 10 | public function getRepository() |
|
140 | } |
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.