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 |
||
29 | class Repository |
||
30 | { |
||
31 | use Filters; |
||
32 | use IncludesRelations; |
||
33 | use Sorts; |
||
34 | use WrapsInResource; |
||
35 | |||
36 | const WITH_ALLOW_ALL = ['*']; |
||
37 | const WITH_ALLOW_NONE = []; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $model; |
||
41 | |||
42 | /** @var ResourceContext $resource_context */ |
||
43 | protected $resource_context; |
||
44 | |||
45 | /** @var array $allowed_with */ |
||
46 | protected $allowed_with = self::WITH_ALLOW_NONE; |
||
47 | |||
48 | /** @var array $default_with */ |
||
49 | protected $default_with = []; |
||
50 | |||
51 | /** @var string $default_sort_by */ |
||
52 | protected $default_sort_by; |
||
53 | |||
54 | /** @var string $default_sort_order */ |
||
55 | protected $default_sort_order = 'asc'; |
||
56 | |||
57 | /** @var string|function $list_column */ |
||
58 | protected $default_list_column; |
||
59 | |||
60 | /** @var bool $only_query */ |
||
61 | protected $only_query = false; |
||
62 | |||
63 | public function __construct(ResourceContext $resource_context) |
||
68 | |||
69 | protected function register() |
||
73 | |||
74 | /** |
||
75 | * Creates a new repository for a model. |
||
76 | * |
||
77 | * @param string $model model name |
||
78 | * @param array|ResourceContext $context |
||
79 | */ |
||
80 | public static function for(string $model, $context = null): self |
||
93 | |||
94 | public function __call($name, $arguments) |
||
113 | |||
114 | private function canReturnAsQuery($name) |
||
122 | |||
123 | private function canWrapInResource($name) |
||
132 | |||
133 | /** |
||
134 | * Get the currenct resource context |
||
135 | */ |
||
136 | public function getContext(): ResourceContext |
||
140 | |||
141 | /** |
||
142 | * Set or replace the resource context |
||
143 | */ |
||
144 | public function setContext(ResourceContext $resource_context) |
||
150 | |||
151 | /** |
||
152 | * Set the model |
||
153 | */ |
||
154 | public function setModel(string $model) |
||
160 | |||
161 | /** |
||
162 | * Return all models based on resource context and query |
||
163 | * |
||
164 | * The ResourceContext determines if the result is a Collection or a |
||
165 | * LengthAwarePaginator. |
||
166 | * |
||
167 | * @return LengthAwarePaginator|Collection|Builder |
||
168 | */ |
||
169 | View Code Duplication | public function all($query = null) |
|
185 | |||
186 | /** |
||
187 | * Execute query |
||
188 | * |
||
189 | * @param Builder $query |
||
190 | * @return LengthAwarePaginator|Collection |
||
191 | */ |
||
192 | private function execute($query) |
||
202 | |||
203 | /** |
||
204 | * Produces a result suitable for selects, lists, and autocomplete. All |
||
205 | * entries that has a 'value' and a 'label' key. |
||
206 | * |
||
207 | * Note: if a callable is used the mapping is performed in memory, while a |
||
208 | * string is done in the database layer. |
||
209 | * |
||
210 | * @param callable|string $column |
||
211 | * @param Builder $query |
||
212 | * @return Collection|Builder |
||
213 | */ |
||
214 | public function list($column = null, $query = null) |
||
257 | |||
258 | /** |
||
259 | * @return Model|Builder |
||
260 | */ |
||
261 | View Code Duplication | public function find($id, $query = null) |
|
277 | |||
278 | public function create(array $data): Model |
||
282 | |||
283 | public function update(Model $model, array $data): Model |
||
289 | |||
290 | public function destroy(Model $model) |
||
294 | |||
295 | protected function modelQuery($query = null) |
||
299 | |||
300 | protected function validateQurey(Builder $query) |
||
311 | } |
||
312 |
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.