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 |
||
16 | class ViewRepository extends NestedTreeRepository |
||
17 | { |
||
18 | private $queryBuilder; |
||
19 | |||
20 | /** |
||
21 | * Get query builder instance. |
||
22 | */ |
||
23 | public function getInstance() |
||
27 | |||
28 | /** |
||
29 | * Get the query builder for a view by url. |
||
30 | * |
||
31 | * @param string $url The url |
||
32 | * |
||
33 | * @return \Doctrine\ORM\QueryBuilder The query builder |
||
34 | */ |
||
35 | public function getOneByUrl($url) |
||
42 | |||
43 | /** |
||
44 | * Filter the query by the sitemap index (=visibility). |
||
45 | * |
||
46 | * @param bool $indexed |
||
47 | * |
||
48 | * @return ViewRepository |
||
49 | */ |
||
50 | public function filterBySitemapIndexed($indexed = true) |
||
59 | |||
60 | /** |
||
61 | * Get all rentals in the repository. |
||
62 | * |
||
63 | * @param bool $excludeUnpublished Should we get only the published Views ? |
||
64 | * |
||
65 | * @return ViewRepository |
||
66 | */ |
||
67 | public function getAll($excludeUnpublished = false) |
||
83 | |||
84 | /** |
||
85 | * Run instance. |
||
86 | * |
||
87 | * @param string $method |
||
88 | * @param string $hydrationMode |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function run($method = 'getResult', $hydrationMode = Query::HYDRATE_OBJECT) |
||
96 | |||
97 | /** |
||
98 | * Find a large amount of views by ViewReferences and optimizing queries with translation walker. |
||
99 | * |
||
100 | * @param ViewReference[] $viewReferences |
||
101 | * |
||
102 | * @return View[]|null The entity instance or NULL if the entities cannot be found. |
||
103 | */ |
||
104 | public function findByViewReferences(array $viewReferences) |
||
152 | |||
153 | /** |
||
154 | * Finds a single entity by a set of criteria. |
||
155 | * |
||
156 | * @param array $criteria |
||
157 | * @param array|null $orderBy |
||
158 | * |
||
159 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
160 | */ |
||
161 | public function findOneBy(array $criteria, array $orderBy = null) |
||
176 | |||
177 | /** |
||
178 | * Get the the view that is a homepage and a published one. |
||
179 | * |
||
180 | * @param string $locale |
||
181 | * |
||
182 | * @return Page |
||
183 | */ |
||
184 | public function findOneByHomepage($locale = 'fr') |
||
211 | |||
212 | /** |
||
213 | * Get PageSeo. |
||
214 | * |
||
215 | * @param string $method leftJoin|innerJoin |
||
216 | * |
||
217 | * @return ViewRepository |
||
218 | */ |
||
219 | public function joinSeo($method = 'leftJoin') |
||
225 | |||
226 | /** |
||
227 | * Filter the query by the sitemap index (=visibility). |
||
228 | * |
||
229 | * @param array $ids |
||
230 | * |
||
231 | * @return ViewRepository |
||
232 | */ |
||
233 | public function filterByIds($ids) |
||
239 | } |
||
240 |
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.