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\Stream; |
||
16 | class StreamRepository extends EloquentRepository implements StreamRepositoryInterface |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * The stream model. |
||
21 | * |
||
22 | * @var |
||
23 | */ |
||
24 | protected $model; |
||
25 | |||
26 | /** |
||
27 | * The schema builder. |
||
28 | * |
||
29 | * @var Builder |
||
30 | */ |
||
31 | protected $schema; |
||
32 | |||
33 | /** |
||
34 | * Create a new StreamRepository instance. |
||
35 | * |
||
36 | * @param StreamModel $model |
||
37 | */ |
||
38 | public function __construct(StreamModel $model) |
||
44 | |||
45 | /** |
||
46 | * Create a new Stream. |
||
47 | * |
||
48 | * @param array $attributes |
||
49 | * @return StreamInterface |
||
50 | */ |
||
51 | public function create(array $attributes = []) |
||
75 | |||
76 | /** |
||
77 | * Find a stream by it's namespace and slug. |
||
78 | * |
||
79 | * @param $slug |
||
80 | * @param $namespace |
||
81 | * @return null|StreamInterface |
||
82 | */ |
||
83 | public function findBySlugAndNamespace($slug, $namespace) |
||
87 | |||
88 | /** |
||
89 | * Find all streams by their searchable flag. |
||
90 | * |
||
91 | * @param $searchable |
||
92 | * @return StreamCollection |
||
93 | */ |
||
94 | public function findAllBySearchable($searchable) |
||
98 | |||
99 | /** |
||
100 | * Find all streams in a namespace. |
||
101 | * |
||
102 | * @param $namespace |
||
103 | * @return null|EloquentCollection |
||
104 | */ |
||
105 | public function findAllByNamespace($namespace) |
||
109 | |||
110 | /** |
||
111 | * Return streams that are/not hidden. |
||
112 | * |
||
113 | * @param $hidden |
||
114 | * @return StreamCollection |
||
115 | */ |
||
116 | public function hidden($hidden = true) |
||
120 | |||
121 | /** |
||
122 | * Return only visible streams. |
||
123 | * |
||
124 | * @return StreamCollection |
||
125 | */ |
||
126 | public function visible() |
||
130 | |||
131 | /** |
||
132 | * Destroy a namespace. |
||
133 | * |
||
134 | * @param $namespace |
||
135 | */ |
||
136 | public function destroy($namespace) |
||
142 | |||
143 | /** |
||
144 | * Clean up abandoned streams. |
||
145 | */ |
||
146 | public function cleanup() |
||
167 | } |
||
168 |
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.