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 |
||
30 | abstract class RestController |
||
31 | { |
||
32 | /** |
||
33 | * @var RestPaginatorFactory |
||
34 | */ |
||
35 | private $paginatorFactory; |
||
36 | |||
37 | /** |
||
38 | * @var EventDispatcherInterface |
||
39 | */ |
||
40 | private $eventDispatcher; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $sortConfiguration = []; |
||
46 | |||
47 | /** |
||
48 | * @var ViewHandler |
||
49 | */ |
||
50 | private $viewHandler; |
||
51 | |||
52 | /** |
||
53 | * @var FilterQueryBuilder |
||
54 | */ |
||
55 | private $filterQueryBuilder; |
||
56 | |||
57 | /** |
||
58 | * @param RestManager $restManager |
||
59 | * @param FilterQueryBuilder $filterQueryBuilder |
||
60 | * @param RestPaginatorFactory $paginatorFactory |
||
61 | * @param EventDispatcherInterface $eventDispatcher |
||
62 | * @param ViewHandler $viewHandler |
||
63 | */ |
||
64 | 12 | public function __construct( |
|
77 | |||
78 | /** |
||
79 | * @param array $sortConfiguration |
||
80 | */ |
||
81 | 1 | public function setSortConfiguration(array $sortConfiguration) |
|
85 | |||
86 | /** |
||
87 | * List action for crud module. |
||
88 | * |
||
89 | * @param Request $request |
||
90 | * @param ParamFetcherInterface $paramFetcher |
||
91 | * |
||
92 | * @return Response |
||
93 | */ |
||
94 | 1 | protected function getList(Request $request, ParamFetcherInterface $paramFetcher) |
|
120 | |||
121 | /** |
||
122 | * Get entity from given id. |
||
123 | * |
||
124 | * @param Request $request |
||
125 | * @param int $id |
||
126 | * |
||
127 | * @return Response |
||
128 | */ |
||
129 | 1 | protected function getOne(Request $request, $id) |
|
140 | |||
141 | /** |
||
142 | * Create entity from form. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * |
||
146 | * @return FormInterface|Response |
||
147 | */ |
||
148 | 2 | protected function post(Request $request) |
|
171 | |||
172 | /** |
||
173 | * Update entity from form. |
||
174 | * |
||
175 | * @param Request $request |
||
176 | * @param int $id |
||
177 | * |
||
178 | * @return Response |
||
179 | */ |
||
180 | 2 | protected function put(Request $request, $id) |
|
204 | |||
205 | /** |
||
206 | * Delete entity from given id. |
||
207 | * |
||
208 | * @param Request $request |
||
209 | * @param int $id |
||
210 | * |
||
211 | * @return Response |
||
212 | */ |
||
213 | 1 | protected function delete(Request $request, $id) |
|
228 | |||
229 | /** |
||
230 | * Show new form schema for crud entity. |
||
231 | * |
||
232 | * @param Request $request |
||
233 | * |
||
234 | * @return Response |
||
235 | */ |
||
236 | 1 | protected function newForm(Request $request) |
|
245 | |||
246 | /** |
||
247 | * Show edit form for crud entity. |
||
248 | * |
||
249 | * @param Request $request |
||
250 | * @param int $id |
||
251 | * |
||
252 | * @return FormInterface |
||
253 | */ |
||
254 | 1 | protected function editForm(Request $request, $id) |
|
264 | |||
265 | /** |
||
266 | * This method is responsible for patching field for resource |
||
267 | * it should be decorating by child controller and defined as patch[field]Action. |
||
268 | * |
||
269 | * @param int $id |
||
270 | * @param string $fieldName |
||
271 | * @param mixed $value |
||
272 | * |
||
273 | * @return Response |
||
274 | */ |
||
275 | 2 | protected function patch($id, $fieldName, $value) |
|
293 | |||
294 | /** |
||
295 | * Apply serialization rules on response object. |
||
296 | * |
||
297 | * @param object $result |
||
298 | * @param array $groups |
||
299 | * @param int $statusCode |
||
300 | * |
||
301 | * @return Response |
||
302 | */ |
||
303 | 10 | protected function handleWithGroups($result, array $groups, $statusCode = Response::HTTP_OK) |
|
311 | |||
312 | /** |
||
313 | * Fetch entity from given PK. |
||
314 | * |
||
315 | * @param int $id |
||
316 | * |
||
317 | * @return object |
||
318 | * |
||
319 | * @throws NotFoundHttpException |
||
320 | */ |
||
321 | 8 | protected function findOr404($id) |
|
331 | |||
332 | /** |
||
333 | * @param string $actionName |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | 10 | protected function getSerializationGroup($actionName) |
|
341 | } |
||
342 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: