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 |
||
8 | class PublicationQuery extends BasePublicationQuery |
||
9 | { |
||
10 | /** |
||
11 | * Applies base filters |
||
12 | * |
||
13 | * @param string|null $type_key |
||
14 | * @return PublicationQuery |
||
15 | */ |
||
16 | public function applyBaseFilter($type_key = null) |
||
27 | |||
28 | /** |
||
29 | * Applies the sorting by date |
||
30 | * |
||
31 | * @param string|null $sort_order |
||
32 | * @return PublicationQuery |
||
33 | */ |
||
34 | public function applySorting($sort_order = null) |
||
42 | |||
43 | /** |
||
44 | * Applies the random sorting |
||
45 | * |
||
46 | * @return PublicationQuery |
||
47 | */ |
||
48 | public function sortRandomly() |
||
52 | |||
53 | /** |
||
54 | * Applies the limitation and returns a publication or a collection of publications |
||
55 | * |
||
56 | * @param $limit |
||
57 | * @return array|mixed|\PropelObjectCollection|Publication |
||
58 | */ |
||
59 | public function findWithLimit($limit) |
||
69 | |||
70 | /** |
||
71 | * Applies the filter by year |
||
72 | * |
||
73 | * @param string|int $value |
||
74 | * @return PublicationQuery |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | View Code Duplication | public function filterByYear($value) |
|
92 | |||
93 | /** |
||
94 | * Applies the filter by month |
||
95 | * |
||
96 | * @param string $value |
||
97 | * @return PublicationQuery |
||
98 | * @throws \Exception |
||
99 | */ |
||
100 | public function filterByMonth($value) |
||
117 | |||
118 | /** |
||
119 | * Applies the filter by day |
||
120 | * |
||
121 | * @param string $value |
||
122 | * @return PublicationQuery |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | View Code Duplication | public function filterByDay($value) |
|
140 | } |
||
141 |
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.