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 namespace Anomaly\Streams\Platform\Assignment; |
||
14 | class AssignmentCollection extends EloquentCollection |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Find an assignment by it's field slug. |
||
19 | * |
||
20 | * @param $slug |
||
21 | * @return AssignmentInterface |
||
22 | */ |
||
23 | public function findByFieldSlug($slug) |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Find all fields using |
||
38 | * the provided field type. |
||
39 | * |
||
40 | * @param $namespace |
||
41 | * @return static |
||
42 | */ |
||
43 | public function findAllByFieldType($namespace) |
||
54 | |||
55 | /** |
||
56 | * Return assignments only included the provided fields. |
||
57 | * |
||
58 | * @param array $fields |
||
59 | * @return AssignmentCollection |
||
60 | */ |
||
61 | public function withFields(array $fields) |
||
62 | { |
||
63 | return new static( |
||
64 | array_filter( |
||
65 | array_map( |
||
66 | function (AssignmentInterface $assignment) use ($fields) { |
||
67 | return in_array($assignment->getFieldSlug(), $fields) ? $assignment : null; |
||
68 | }, |
||
69 | $this->items |
||
70 | ) |
||
71 | ) |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return assignments not included the provided fields. |
||
77 | * |
||
78 | * @param array $fields |
||
79 | * @return AssignmentCollection |
||
80 | */ |
||
81 | public function withoutFields(array $fields) |
||
82 | { |
||
83 | return new static( |
||
84 | array_filter( |
||
85 | array_map( |
||
86 | function (AssignmentInterface $assignment) use ($fields) { |
||
87 | return !in_array($assignment->getFieldSlug(), $fields) ? $assignment : null; |
||
88 | }, |
||
89 | $this->items |
||
90 | ) |
||
91 | ) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return only assignments that have relation fields. |
||
97 | * |
||
98 | * @return AssignmentCollection |
||
99 | */ |
||
100 | public function relations() |
||
116 | |||
117 | /** |
||
118 | * Return only assignments that have date fields. |
||
119 | * |
||
120 | * @return AssignmentCollection |
||
121 | */ |
||
122 | public function dates() |
||
132 | |||
133 | /** |
||
134 | * Return only assignments that are unique. |
||
135 | * |
||
136 | * @return AssignmentCollection |
||
137 | */ |
||
138 | public function indexed() |
||
146 | |||
147 | /** |
||
148 | * Return only assignments that are required. |
||
149 | * |
||
150 | * @return AssignmentCollection |
||
151 | */ |
||
152 | public function required() |
||
160 | |||
161 | /** |
||
162 | * Return only assignments that are translatable. |
||
163 | * |
||
164 | * @return AssignmentCollection |
||
165 | */ |
||
166 | public function translatable() |
||
174 | |||
175 | /** |
||
176 | * Return only assignments that are NOT translatable. |
||
177 | * |
||
178 | * @return AssignmentCollection |
||
179 | */ |
||
180 | public function notTranslatable() |
||
188 | |||
189 | /** |
||
190 | * Return an array of field slugs. |
||
191 | * |
||
192 | * @param null $prefix |
||
193 | * @return array |
||
194 | */ |
||
195 | public function fieldSlugs($prefix = null) |
||
206 | |||
207 | /** |
||
208 | * Return only assignments with locked fields. |
||
209 | * |
||
210 | * @return AssignmentCollection |
||
211 | */ |
||
212 | View Code Duplication | public function locked() |
|
226 | |||
227 | /** |
||
228 | * Return only assignments with fields |
||
229 | * that are not locked. |
||
230 | * |
||
231 | * @return AssignmentCollection |
||
232 | */ |
||
233 | View Code Duplication | public function notLocked() |
|
247 | |||
248 | /** |
||
249 | * An alias for notLocked(); |
||
250 | * |
||
251 | * @return AssignmentCollection |
||
252 | */ |
||
253 | public function unlocked() |
||
257 | |||
258 | /** |
||
259 | * Return the assignment |
||
260 | * with column type. |
||
261 | * |
||
262 | * @param $type |
||
263 | * @return AssignmentCollection |
||
264 | */ |
||
265 | public function column($type) |
||
277 | } |
||
278 |
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.