Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Controller implements ControllerInterface { |
||
29 | |||
30 | /** |
||
31 | * Holds the filesystme. |
||
32 | * @var FilesystemInterface |
||
33 | */ |
||
34 | protected $filesystem; |
||
35 | |||
36 | /** |
||
37 | * Holds the session. |
||
38 | * @var SessionInterface |
||
39 | */ |
||
40 | protected $session; |
||
41 | |||
42 | /** |
||
43 | * Holds the translator. |
||
44 | * @var TranslatorInterface |
||
45 | */ |
||
46 | protected $translator; |
||
47 | |||
48 | /** |
||
49 | * Holds the service. |
||
50 | * @var Service |
||
51 | */ |
||
52 | protected $service; |
||
53 | |||
54 | /** |
||
55 | * Holds the Twig instance. |
||
56 | * @var Twig_Environment |
||
57 | */ |
||
58 | protected $twig; |
||
59 | |||
60 | /** |
||
61 | * Postprocesses the entity after modification by handling the uploaded |
||
62 | * files and setting the flash. |
||
63 | * |
||
64 | * @param Request $request |
||
65 | * the current request |
||
66 | * @param AbstractData $crudData |
||
67 | * the data instance of the entity |
||
68 | * @param Entity $instance |
||
69 | * the entity |
||
70 | * @param string $entity |
||
71 | * the name of the entity |
||
72 | * @param string $mode |
||
73 | * whether to 'edit' or to 'create' the entity |
||
74 | * |
||
75 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
76 | * the HTTP response of this modification |
||
77 | */ |
||
78 | 4 | protected function modifyFilesAndSetFlashBag(Request $request, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
92 | |||
93 | /** |
||
94 | * Sets the flashes of a failed entity modification. |
||
95 | * |
||
96 | * @param boolean $optimisticLocking |
||
97 | * whether the optimistic locking failed |
||
98 | * @param string $mode |
||
99 | * the modification mode, either 'create' or 'edit' |
||
100 | */ |
||
101 | 2 | protected function setValidationFailedFlashes($optimisticLocking, $mode) |
|
108 | |||
109 | /** |
||
110 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
111 | * response. |
||
112 | * |
||
113 | * @param Request $request |
||
114 | * the current request |
||
115 | * @param AbstractData $crudData |
||
116 | * the data instance of the entity |
||
117 | * @param Entity $instance |
||
118 | * the entity |
||
119 | * @param string $entity |
||
120 | * the name of the entity |
||
121 | * @param boolean $edit |
||
122 | * whether to edit (true) or to create (false) the entity |
||
123 | * |
||
124 | * @return Response |
||
|
|||
125 | * the HTTP response of this modification |
||
126 | */ |
||
127 | 4 | protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
160 | |||
161 | /** |
||
162 | * Gets the parameters for the redirection after deleting an entity. |
||
163 | * |
||
164 | * @param Request $request |
||
165 | * the current request |
||
166 | * @param string $entity |
||
167 | * the entity name |
||
168 | * @param string $redirectPage |
||
169 | * reference, where the page to redirect to will be stored |
||
170 | * |
||
171 | * @return array<string,string> |
||
172 | * the parameters of the redirection, entity and id |
||
173 | */ |
||
174 | 1 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
189 | |||
190 | /** |
||
191 | * Builds up the parameters of the list page filters. |
||
192 | * |
||
193 | * @param Request $request |
||
194 | * the current request |
||
195 | * @param EntityDefinition $definition |
||
196 | * the current entity definition |
||
197 | * @param array &$filter |
||
198 | * will hold a map of fields to request parameters for the filters |
||
199 | * @param boolean $filterActive |
||
200 | * reference, will be true if at least one filter is active |
||
201 | * @param array $filterToUse |
||
202 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
203 | * @param array $filterOperators |
||
204 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
205 | */ |
||
206 | 2 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
207 | { |
||
208 | 2 | foreach ($definition->getFilter() as $filterField) { |
|
209 | 2 | $type = $definition->getType($filterField); |
|
210 | 2 | $filter[$filterField] = $request->get('crudFilter'.$filterField); |
|
211 | 2 | if ($filter[$filterField]) { |
|
212 | 1 | $filterActive = true; |
|
213 | 1 | $filterToUse[$filterField] = $filter[$filterField]; |
|
214 | 1 | $filterOperators[$filterField] = '='; |
|
215 | 1 | if ($type === 'boolean') { |
|
216 | 1 | $filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
|
217 | 1 | } else if ($type === 'reference') { |
|
218 | 1 | $filter[$filterField] = ['id' => $filter[$filterField]]; |
|
219 | 1 | } else if ($type === 'many') { |
|
220 | $filter[$filterField] = array_map(function($value) { |
||
221 | 1 | return ['id' => $value]; |
|
222 | 1 | }, $filter[$filterField]); |
|
223 | 1 | $filterToUse[$filterField] = $filter[$filterField]; |
|
224 | 1 | } else if (in_array($type, ['text', 'multiline', 'fixed'])) { |
|
225 | 1 | $filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
|
226 | 2 | $filterOperators[$filterField] = 'LIKE'; |
|
227 | } |
||
228 | } |
||
229 | } |
||
230 | 2 | } |
|
231 | |||
232 | /** |
||
233 | * Generates the not found page. |
||
234 | * |
||
235 | * @param string $error |
||
236 | * the cause of the not found error |
||
237 | * |
||
238 | * @return Response |
||
239 | * the rendered not found page with the status code 404 |
||
240 | */ |
||
241 | 8 | protected function getNotFoundPage($error) |
|
250 | |||
251 | /** |
||
252 | * Controller constructor. |
||
253 | * |
||
254 | * @param Service $service |
||
255 | * the CRUDlex service |
||
256 | * @param FilesystemInterface $filesystem |
||
257 | * the used filesystem |
||
258 | * @param Twig_Environment $twig |
||
259 | * the Twig environment |
||
260 | * @param SessionInterface $session |
||
261 | * the session service |
||
262 | * @param TranslatorInterface $translator |
||
263 | * the translation service |
||
264 | */ |
||
265 | 10 | public function __construct(Service $service, FilesystemInterface $filesystem, Twig_Environment $twig, SessionInterface $session, TranslatorInterface $translator) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 2 | public function setLocaleAndCheckEntity(Request $request) |
|
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | 3 | public function create(Request $request, $entity) |
|
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | 2 | public function showList(Request $request, $entity) |
|
347 | |||
348 | /** |
||
349 | * {@inheritdoc} |
||
350 | */ |
||
351 | 1 | public function show($entity, $id) |
|
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | 1 | public function edit(Request $request, $entity, $id) |
|
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | 1 | public function delete(Request $request, $entity, $id) |
|
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | 1 | public function renderFile($entity, $id, $field) |
|
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | 1 | public function deleteFile($entity, $id, $field) |
|
470 | |||
471 | /** |
||
472 | * {@inheritdoc} |
||
473 | */ |
||
474 | 1 | public function staticFile(Request $request) |
|
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | 1 | public function setLocale(Request $request, $locale) |
|
515 | } |
||
516 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.