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 |
||
44 | class Controller extends FOSRestController implements ControllerInterface |
||
45 | { |
||
46 | /** |
||
47 | * @var ResourceInterface |
||
48 | */ |
||
49 | protected $resource; |
||
50 | |||
51 | /** |
||
52 | * @param ResourceInterface $resource |
||
53 | */ |
||
54 | public function __construct(ResourceInterface $resource) |
||
58 | |||
59 | /** |
||
60 | * @param Request $request |
||
61 | * |
||
62 | * @return Response |
||
63 | */ |
||
64 | public function indexAction(Request $request) |
||
68 | |||
69 | /** |
||
70 | * @param Request $request |
||
71 | * |
||
72 | * @return Response |
||
73 | */ |
||
74 | public function gridAction(Request $request) |
||
81 | |||
82 | /** |
||
83 | * @param Request $request |
||
84 | * |
||
85 | * @return Response |
||
86 | */ |
||
87 | public function batchAction(Request $request) |
||
118 | |||
119 | /** |
||
120 | * @param Request $request |
||
121 | * |
||
122 | * @return Response |
||
123 | */ |
||
124 | public function showAction(Request $request) |
||
128 | |||
129 | /** |
||
130 | * @param Request $request |
||
131 | * |
||
132 | * @return Response |
||
133 | */ |
||
134 | public function createAction(Request $request) |
||
148 | |||
149 | /** |
||
150 | * @param Request $request |
||
151 | * |
||
152 | * @return Response |
||
153 | */ |
||
154 | public function updateAction(Request $request) |
||
169 | |||
170 | /** |
||
171 | * @param Request $request |
||
172 | * |
||
173 | * @return Response |
||
174 | */ |
||
175 | public function deleteAction(Request $request) |
||
187 | |||
188 | /** |
||
189 | * @param string $action |
||
190 | * @param bool $mandatory |
||
191 | * |
||
192 | * @return object|object[] |
||
193 | */ |
||
194 | protected function find($action, $mandatory = true) |
||
218 | |||
219 | /** |
||
220 | * @param string|object|null $form |
||
221 | * @param object|null $object |
||
222 | * @param mixed[] $options |
||
223 | * |
||
224 | * @return FormInterface |
||
225 | */ |
||
226 | protected function buildForm($form = null, $object = null, array $options = []) |
||
230 | |||
231 | /** |
||
232 | * @param FormInterface $form |
||
233 | * @param Request $request |
||
234 | * |
||
235 | * @return FormInterface |
||
236 | */ |
||
237 | protected function submitForm(FormInterface $form, Request $request) |
||
257 | |||
258 | /** |
||
259 | * @return GridInterface |
||
260 | */ |
||
261 | protected function buildGrid() |
||
265 | |||
266 | /** |
||
267 | * @param GridInterface $grid |
||
268 | * @param Request $request |
||
269 | * |
||
270 | * @return FormInterface |
||
271 | */ |
||
272 | protected function submitGrid(GridInterface $grid, Request $request) |
||
276 | |||
277 | /** |
||
278 | * @param string $action |
||
279 | * @param FormInterface|null $form |
||
280 | * @param int $statusCode |
||
281 | * |
||
282 | * @return Response |
||
283 | */ |
||
284 | protected function processAction($action, FormInterface $form = null, $statusCode = Response::HTTP_NO_CONTENT) |
||
300 | |||
301 | /** |
||
302 | * @param string $action |
||
303 | * @param View $view |
||
304 | * |
||
305 | * @return Response |
||
306 | */ |
||
307 | protected function processView($action, View $view) |
||
316 | |||
317 | /** |
||
318 | * @param DomainException $domainException |
||
319 | */ |
||
320 | protected function processException(DomainException $domainException) |
||
330 | |||
331 | /** |
||
332 | * @return FormFactoryInterface |
||
333 | */ |
||
334 | protected function getFormFactory() |
||
338 | |||
339 | /** |
||
340 | * @return FactoryInterface |
||
341 | */ |
||
342 | protected function getFactory() |
||
346 | |||
347 | /** |
||
348 | * @return DomainManagerInterface |
||
349 | */ |
||
350 | protected function getDomainManager() |
||
354 | |||
355 | /** |
||
356 | * @return SecurityCheckerInterface |
||
357 | */ |
||
358 | protected function getSecurityChecker() |
||
362 | |||
363 | /** |
||
364 | * @return ParameterResolverInterface |
||
365 | */ |
||
366 | protected function getParameterResolver() |
||
370 | |||
371 | /** |
||
372 | * @return GridBuilderInterface |
||
373 | */ |
||
374 | protected function getGridBuilder() |
||
378 | |||
379 | /** |
||
380 | * @return GridHandlerInterface |
||
381 | */ |
||
382 | protected function getGridHandler() |
||
386 | |||
387 | /** |
||
388 | * @return BatcherInterface |
||
389 | */ |
||
390 | protected function getGridBatcher() |
||
394 | |||
395 | /** |
||
396 | * @return EventDispatcherInterface |
||
397 | */ |
||
398 | protected function getRestEventDispatcher() |
||
402 | } |
||
403 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.