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\Field; |
||
14 | class FieldCollection extends EloquentCollection |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Create a new FieldCollection instance. |
||
19 | * |
||
20 | * @param array $items |
||
21 | */ |
||
22 | public function __construct($items = []) |
||
33 | |||
34 | /** |
||
35 | * Return only unassigned fields. |
||
36 | * |
||
37 | * @return FieldCollection |
||
38 | */ |
||
39 | public function unassigned() |
||
52 | |||
53 | /** |
||
54 | * Return fields only assigned |
||
55 | * to the provided stream. |
||
56 | * |
||
57 | * @param StreamInterface $stream |
||
58 | * @return FieldCollection |
||
59 | */ |
||
60 | View Code Duplication | public function assignedTo(StreamInterface $stream) |
|
73 | |||
74 | /** |
||
75 | * Return fields only NOT assigned |
||
76 | * to the provided stream. |
||
77 | * |
||
78 | * @param StreamInterface $stream |
||
79 | * @return FieldCollection |
||
80 | */ |
||
81 | View Code Duplication | public function notAssignedTo(StreamInterface $stream) |
|
94 | |||
95 | /** |
||
96 | * Return only unlocked fields. |
||
97 | * |
||
98 | * @return FieldCollection |
||
99 | */ |
||
100 | public function unlocked() |
||
113 | } |
||
114 |
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.