Complex classes like ControllerTrait 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 ControllerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | trait ControllerTrait |
||
50 | { |
||
51 | /** |
||
52 | * Returns true if the service id is defined. |
||
53 | * |
||
54 | * @final |
||
55 | */ |
||
56 | protected function has(string $id): bool |
||
60 | |||
61 | /** |
||
62 | * Gets a container service by its id. |
||
63 | * |
||
64 | * @return object The service |
||
65 | * |
||
66 | * @final |
||
67 | */ |
||
68 | protected function get(string $id) |
||
72 | |||
73 | /** |
||
74 | * Generates a URL from the given parameters. |
||
75 | * |
||
76 | * @see UrlGeneratorInterface |
||
77 | * |
||
78 | * @final |
||
79 | */ |
||
80 | protected function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
||
84 | |||
85 | /** |
||
86 | * Forwards the request to another controller. |
||
87 | * |
||
88 | * @param string $controller The controller name (a string like Bundle\BlogBundle\Controller\PostController::indexAction) |
||
89 | * |
||
90 | * @final |
||
91 | */ |
||
92 | protected function forward(string $controller, array $path = [], array $query = []): Response |
||
100 | |||
101 | /** |
||
102 | * Returns a RedirectResponse to the given URL. |
||
103 | * |
||
104 | * @final |
||
105 | */ |
||
106 | protected function redirect(string $url, int $status = 302): RedirectResponse |
||
110 | |||
111 | /** |
||
112 | * Returns a RedirectResponse to the given route with the given parameters. |
||
113 | * |
||
114 | * @final |
||
115 | */ |
||
116 | protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse |
||
120 | |||
121 | /** |
||
122 | * Returns a JsonResponse that uses the serializer component if enabled, or json_encode. |
||
123 | * |
||
124 | * @final |
||
125 | */ |
||
126 | protected function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse |
||
138 | |||
139 | /** |
||
140 | * Returns a BinaryFileResponse object with original or customized file name and disposition header. |
||
141 | * |
||
142 | * @param \SplFileInfo|string $file File object or path to file to be sent as response |
||
143 | * |
||
144 | * @final |
||
145 | */ |
||
146 | protected function file($file, ?string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse |
||
153 | |||
154 | /** |
||
155 | * Adds a flash message to the current session for type. |
||
156 | * |
||
157 | * @throws \LogicException |
||
158 | * |
||
159 | * @final |
||
160 | */ |
||
161 | protected function addFlash(string $type, $message) |
||
169 | |||
170 | /** |
||
171 | * Checks if the attributes are granted against the current authentication token and optionally supplied subject. |
||
172 | * |
||
173 | * @throws \LogicException |
||
174 | * |
||
175 | * @final |
||
176 | */ |
||
177 | protected function isGranted($attributes, $subject = null): bool |
||
185 | |||
186 | /** |
||
187 | * Throws an exception unless the attributes are granted against the current authentication token and optionally |
||
188 | * supplied subject. |
||
189 | * |
||
190 | * @throws AccessDeniedException |
||
191 | * |
||
192 | * @final |
||
193 | */ |
||
194 | protected function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.') |
||
204 | |||
205 | /** |
||
206 | * Returns a rendered view. |
||
207 | * |
||
208 | * @final |
||
209 | */ |
||
210 | protected function renderView(string $view, array $parameters = []): string |
||
224 | |||
225 | /** |
||
226 | * Renders a view. |
||
227 | * |
||
228 | * @final |
||
229 | */ |
||
230 | protected function render(string $view, array $parameters = [], ?Response $response = null): Response |
||
250 | |||
251 | /** |
||
252 | * Streams a view. |
||
253 | * |
||
254 | * @final |
||
255 | */ |
||
256 | protected function stream(string $view, array $parameters = [], ?StreamedResponse $response = null): StreamedResponse |
||
284 | |||
285 | /** |
||
286 | * Returns a NotFoundHttpException. |
||
287 | * |
||
288 | * This will result in a 404 response code. Usage example: |
||
289 | * |
||
290 | * throw $this->createNotFoundException('Page not found!'); |
||
291 | * |
||
292 | * @final |
||
293 | */ |
||
294 | protected function createNotFoundException(string $message = 'Not Found', ?\Throwable $previous = null): NotFoundHttpException |
||
298 | |||
299 | /** |
||
300 | * Returns an AccessDeniedException. |
||
301 | * |
||
302 | * This will result in a 403 response code. Usage example: |
||
303 | * |
||
304 | * throw $this->createAccessDeniedException('Unable to access this page!'); |
||
305 | * |
||
306 | * @throws \LogicException If the Security component is not available |
||
307 | * |
||
308 | * @final |
||
309 | */ |
||
310 | protected function createAccessDeniedException(string $message = 'Access Denied.', ?\Throwable $previous = null): AccessDeniedException |
||
318 | |||
319 | /** |
||
320 | * Creates and returns a Form instance from the type of the form. |
||
321 | * |
||
322 | * @final |
||
323 | */ |
||
324 | protected function createForm(string $type, $data = null, array $options = []): FormInterface |
||
328 | |||
329 | /** |
||
330 | * Creates and returns a form builder instance. |
||
331 | * |
||
332 | * @final |
||
333 | */ |
||
334 | protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface |
||
338 | |||
339 | /** |
||
340 | * Shortcut to return the Doctrine Registry service. |
||
341 | * |
||
342 | * @throws \LogicException If DoctrineBundle is not available |
||
343 | * |
||
344 | * @return ManagerRegistry |
||
345 | * |
||
346 | * @final |
||
347 | */ |
||
348 | protected function getDoctrine() |
||
356 | |||
357 | /** |
||
358 | * Get a user from the Security Token Storage. |
||
359 | * |
||
360 | * @throws \LogicException If SecurityBundle is not available |
||
361 | * |
||
362 | * @return UserInterface|object|null |
||
363 | * |
||
364 | * @see TokenInterface::getUser() |
||
365 | * |
||
366 | * @final |
||
367 | */ |
||
368 | protected function getUser() |
||
385 | |||
386 | /** |
||
387 | * Checks the validity of a CSRF token. |
||
388 | * |
||
389 | * @param string $id The id used when generating the token |
||
390 | * @param string|null $token The actual token sent with the request that should be validated |
||
391 | * |
||
392 | * @final |
||
393 | */ |
||
394 | protected function isCsrfTokenValid(string $id, ?string $token): bool |
||
402 | |||
403 | /** |
||
404 | * Dispatches a message to the bus. |
||
405 | * |
||
406 | * @param object|Envelope $message The message or the message pre-wrapped in an envelope |
||
407 | * @param StampInterface[] $stamps |
||
408 | * |
||
409 | * @final |
||
410 | */ |
||
411 | protected function dispatchMessage($message, array $stamps = []): Envelope |
||
420 | |||
421 | /** |
||
422 | * Adds a Link HTTP header to the current response. |
||
423 | * |
||
424 | * @see https://tools.ietf.org/html/rfc5988 |
||
425 | * |
||
426 | * @final |
||
427 | */ |
||
428 | protected function addLink(Request $request, LinkInterface $link) |
||
442 | } |
||
443 |
If you suppress an error, we recommend checking for the error condition explicitly: