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; |
||
| 15 | class AssignmentCollection extends EloquentCollection |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Find an assignment by it's field slug. |
||
| 20 | * |
||
| 21 | * @param $slug |
||
| 22 | * @return AssignmentInterface |
||
| 23 | */ |
||
| 24 | public function findByFieldSlug($slug) |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Find all fields using |
||
| 39 | * the provided field type. |
||
| 40 | * |
||
| 41 | * @param $namespace |
||
| 42 | * @return static |
||
| 43 | */ |
||
| 44 | public function findAllByFieldType($namespace) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return assignments only included the provided fields. |
||
| 58 | * |
||
| 59 | * @param array $fields |
||
| 60 | * @return AssignmentCollection |
||
| 61 | */ |
||
| 62 | public function withFields(array $fields) |
||
| 63 | { |
||
| 64 | return new static( |
||
| 65 | array_filter( |
||
| 66 | array_map( |
||
| 67 | function (AssignmentInterface $assignment) use ($fields) { |
||
| 68 | return in_array($assignment->getFieldSlug(), $fields) ? $assignment : null; |
||
| 69 | }, |
||
| 70 | $this->items |
||
| 71 | ) |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return assignments not included the provided fields. |
||
| 78 | * |
||
| 79 | * @param array $fields |
||
| 80 | * @return AssignmentCollection |
||
| 81 | */ |
||
| 82 | public function withoutFields(array $fields) |
||
| 83 | { |
||
| 84 | return new static( |
||
| 85 | array_filter( |
||
| 86 | array_map( |
||
| 87 | function (AssignmentInterface $assignment) use ($fields) { |
||
| 88 | return !in_array($assignment->getFieldSlug(), $fields) ? $assignment : null; |
||
| 89 | }, |
||
| 90 | $this->items |
||
| 91 | ) |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Return only assignments that have relation fields. |
||
| 98 | * |
||
| 99 | * @return AssignmentCollection |
||
| 100 | */ |
||
| 101 | public function relations() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Return only assignments that have date fields. |
||
| 121 | * |
||
| 122 | * @return AssignmentCollection |
||
| 123 | */ |
||
| 124 | public function dates() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return only assignments that are unique. |
||
| 138 | * |
||
| 139 | * @return AssignmentCollection |
||
| 140 | */ |
||
| 141 | public function indexed() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return only assignments that are required. |
||
| 152 | * |
||
| 153 | * @return AssignmentCollection |
||
| 154 | */ |
||
| 155 | public function required() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return only assignments that are translatable. |
||
| 166 | * |
||
| 167 | * @return AssignmentCollection |
||
| 168 | */ |
||
| 169 | public function translatable() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return only assignments that are NOT translatable. |
||
| 180 | * |
||
| 181 | * @return AssignmentCollection |
||
| 182 | */ |
||
| 183 | public function notTranslatable() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Return an array of field slugs. |
||
| 194 | * |
||
| 195 | * @param null $prefix |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function fieldSlugs($prefix = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return only assignments with locked fields. |
||
| 212 | * |
||
| 213 | * @return AssignmentCollection |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function locked() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Return only assignments with fields |
||
| 232 | * that are not locked. |
||
| 233 | * |
||
| 234 | * @return AssignmentCollection |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function notLocked() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * An alias for notLocked(); |
||
| 253 | * |
||
| 254 | * @return AssignmentCollection |
||
| 255 | */ |
||
| 256 | public function unlocked() |
||
| 260 | } |
||
| 261 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.