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 | private $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 | private 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 | private function buildForm($form = null, $object = null, array $options = []) |
||
227 | { |
||
228 | return $this->getFormFactory()->create($this->resource, $form, $object, $options); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param FormInterface $form |
||
233 | * @param Request $request |
||
234 | * |
||
235 | * @return FormInterface |
||
236 | */ |
||
237 | private function submitForm(FormInterface $form, Request $request) |
||
257 | |||
258 | /** |
||
259 | * @return GridInterface |
||
260 | */ |
||
261 | private function buildGrid() |
||
265 | |||
266 | /** |
||
267 | * @param GridInterface $grid |
||
268 | * @param Request $request |
||
269 | * |
||
270 | * @return FormInterface |
||
271 | */ |
||
272 | private function submitGrid(GridInterface $grid, Request $request) |
||
276 | |||
277 | /** |
||
278 | * @param FormInterface|null $form |
||
279 | * @param int $statusCode |
||
280 | * |
||
281 | * @return Response |
||
282 | */ |
||
283 | private function processAction(FormInterface $form = null, $statusCode = Response::HTTP_NO_CONTENT) |
||
297 | |||
298 | /** |
||
299 | * @param View $view |
||
300 | * |
||
301 | * @return Response |
||
302 | */ |
||
303 | private function processView(View $view) |
||
309 | |||
310 | /** |
||
311 | * @param DomainException $domainException |
||
312 | */ |
||
313 | private function processException(DomainException $domainException) |
||
323 | |||
324 | /** |
||
325 | * @return FormFactoryInterface |
||
326 | */ |
||
327 | private function getFormFactory() |
||
331 | |||
332 | /** |
||
333 | * @return FactoryInterface |
||
334 | */ |
||
335 | private function getFactory() |
||
339 | |||
340 | /** |
||
341 | * @return DomainManagerInterface |
||
342 | */ |
||
343 | private function getDomainManager() |
||
347 | |||
348 | /** |
||
349 | * @return SecurityCheckerInterface |
||
350 | */ |
||
351 | private function getSecurityChecker() |
||
355 | |||
356 | /** |
||
357 | * @return ParameterResolverInterface |
||
358 | */ |
||
359 | private function getParameterResolver() |
||
363 | |||
364 | /** |
||
365 | * @return GridBuilderInterface |
||
366 | */ |
||
367 | private function getGridBuilder() |
||
371 | |||
372 | /** |
||
373 | * @return GridHandlerInterface |
||
374 | */ |
||
375 | private function getGridHandler() |
||
379 | |||
380 | /** |
||
381 | * @return BatcherInterface |
||
382 | */ |
||
383 | private function getGridBatcher() |
||
387 | |||
388 | /** |
||
389 | * @return EventDispatcherInterface |
||
390 | */ |
||
391 | private function getRestEventDispatcher() |
||
395 | } |
||
396 |
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.