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 |
||
46 | class Controller { |
||
47 | |||
48 | /** |
||
49 | * Holds the filesystme. |
||
50 | * @var FilesystemInterface |
||
51 | */ |
||
52 | protected $filesystem; |
||
53 | |||
54 | /** |
||
55 | * Holds the session. |
||
56 | * @var Session |
||
57 | */ |
||
58 | protected $session; |
||
59 | |||
60 | /** |
||
61 | * Holds the translator. |
||
62 | * @var Translator |
||
63 | */ |
||
64 | protected $translator; |
||
65 | |||
66 | /** |
||
67 | * Holds the service. |
||
68 | * @var Service |
||
69 | */ |
||
70 | protected $service; |
||
71 | |||
72 | /** |
||
73 | * Holds the Twig instance. |
||
74 | * @var Twig_Environment |
||
75 | */ |
||
76 | protected $twig; |
||
77 | |||
78 | /** |
||
79 | * Postprocesses the entity after modification by handling the uploaded |
||
80 | * files and setting the flash. |
||
81 | * |
||
82 | * @param Request $request |
||
83 | * the current request |
||
84 | * @param AbstractData $crudData |
||
85 | * the data instance of the entity |
||
86 | * @param Entity $instance |
||
87 | * the entity |
||
88 | * @param string $entity |
||
89 | * the name of the entity |
||
90 | * @param string $mode |
||
91 | * whether to 'edit' or to 'create' the entity |
||
92 | * |
||
93 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
94 | * the HTTP response of this modification |
||
95 | */ |
||
96 | 4 | protected function modifyFilesAndSetFlashBag(Request $request, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
110 | |||
111 | /** |
||
112 | * Sets the flashes of a failed entity modification. |
||
113 | * |
||
114 | * @param boolean $optimisticLocking |
||
115 | * whether the optimistic locking failed |
||
116 | * @param string $mode |
||
117 | * the modification mode, either 'create' or 'edit' |
||
118 | */ |
||
119 | 2 | protected function setValidationFailedFlashes($optimisticLocking, $mode) |
|
126 | |||
127 | /** |
||
128 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
129 | * response. |
||
130 | * |
||
131 | * @param Request $request |
||
132 | * the current request |
||
133 | * @param AbstractData $crudData |
||
134 | * the data instance of the entity |
||
135 | * @param Entity $instance |
||
136 | * the entity |
||
137 | * @param string $entity |
||
138 | * the name of the entity |
||
139 | * @param boolean $edit |
||
140 | * whether to edit (true) or to create (false) the entity |
||
141 | * |
||
142 | * @return Response |
||
143 | * the HTTP response of this modification |
||
144 | */ |
||
145 | 5 | protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
178 | |||
179 | /** |
||
180 | * Gets the parameters for the redirection after deleting an entity. |
||
181 | * |
||
182 | * @param Request $request |
||
183 | * the current request |
||
184 | * @param string $entity |
||
185 | * the entity name |
||
186 | * @param string $redirectPage |
||
187 | * reference, where the page to redirect to will be stored |
||
188 | * |
||
189 | * @return array<string,string> |
||
190 | * the parameters of the redirection, entity and id |
||
191 | */ |
||
192 | 1 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
207 | |||
208 | /** |
||
209 | * Builds up the parameters of the list page filters. |
||
210 | * |
||
211 | * @param Request $request |
||
212 | * the current request |
||
213 | * @param EntityDefinition $definition |
||
214 | * the current entity definition |
||
215 | * @param array &$filter |
||
216 | * will hold a map of fields to request parameters for the filters |
||
217 | * @param boolean $filterActive |
||
218 | * reference, will be true if at least one filter is active |
||
219 | * @param array $filterToUse |
||
220 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
221 | * @param array $filterOperators |
||
222 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
223 | */ |
||
224 | 4 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
249 | |||
250 | /** |
||
251 | * Controller constructor. |
||
252 | * |
||
253 | * @param Service $service |
||
254 | * the CRUDlex service |
||
255 | * @param FilesystemInterface $filesystem |
||
256 | * the used filesystem |
||
257 | * @param Twig_Environment $twig |
||
258 | * the Twig environment |
||
259 | * @param Session $session |
||
260 | * the session service |
||
261 | * @param Translator $translator |
||
262 | * the translation service |
||
263 | */ |
||
264 | 10 | public function __construct(Service $service, FilesystemInterface $filesystem, Twig_Environment $twig, Session $session, Translator $translator) |
|
272 | |||
273 | /** |
||
274 | * Generates the not found page. |
||
275 | * |
||
276 | * @param string $error |
||
277 | * the cause of the not found error |
||
278 | * |
||
279 | * @return Response |
||
280 | * the rendered not found page with the status code 404 |
||
281 | */ |
||
282 | 9 | public function getNotFoundPage($error) |
|
291 | |||
292 | /** |
||
293 | * Transfers the locale from the translator to CRUDlex and |
||
294 | * |
||
295 | * @param Request $request |
||
296 | * the current request |
||
297 | * @return Response|null |
||
298 | * null if everything is ok, a 404 response else |
||
299 | */ |
||
300 | 9 | public function setLocaleAndCheckEntity(Request $request) |
|
309 | |||
310 | /** |
||
311 | * The controller for the "create" action. |
||
312 | * |
||
313 | * @param Request $request |
||
314 | * the current request |
||
315 | * @param string $entity |
||
316 | * the current entity |
||
317 | * |
||
318 | * @return Response |
||
319 | * the HTTP response of this action |
||
320 | */ |
||
321 | 4 | public function create(Request $request, $entity) |
|
328 | |||
329 | /** |
||
330 | * The controller for the "show list" action. |
||
331 | * |
||
332 | * @param Request $request |
||
333 | * the current request |
||
334 | * @param string $entity |
||
335 | * the current entity |
||
336 | * |
||
337 | * @return Response |
||
338 | * the HTTP response of this action or 404 on invalid input |
||
339 | */ |
||
340 | 4 | public function showList(Request $request, $entity) |
|
386 | |||
387 | /** |
||
388 | * The controller for the "show" action. |
||
389 | * |
||
390 | * @param string $entity |
||
391 | * the current entity |
||
392 | * @param string $id |
||
393 | * the instance id to show |
||
394 | * |
||
395 | * @return Response |
||
396 | * the HTTP response of this action or 404 on invalid input |
||
397 | */ |
||
398 | 6 | public function show($entity, $id) |
|
433 | |||
434 | /** |
||
435 | * The controller for the "edit" action. |
||
436 | * |
||
437 | * @param Request $request |
||
438 | * the current request |
||
439 | * @param string $entity |
||
440 | * the current entity |
||
441 | * @param string $id |
||
442 | * the instance id to edit |
||
443 | * |
||
444 | * @return Response |
||
445 | * the HTTP response of this action or 404 on invalid input |
||
446 | */ |
||
447 | 1 | public function edit(Request $request, $entity, $id) |
|
457 | |||
458 | /** |
||
459 | * The controller for the "delete" action. |
||
460 | * |
||
461 | * @param Request $request |
||
462 | * the current request |
||
463 | * @param string $entity |
||
464 | * the current entity |
||
465 | * @param string $id |
||
466 | * the instance id to delete |
||
467 | * |
||
468 | * @return Response |
||
469 | * redirects to the entity list page or 404 on invalid input |
||
470 | */ |
||
471 | 1 | public function delete(Request $request, $entity, $id) |
|
501 | |||
502 | /** |
||
503 | * The controller for the "render file" action. |
||
504 | * |
||
505 | * @param string $entity |
||
506 | * the current entity |
||
507 | * @param string $id |
||
508 | * the instance id |
||
509 | * @param string $field |
||
510 | * the field of the file to render of the instance |
||
511 | * |
||
512 | * @return Response |
||
513 | * the rendered file |
||
514 | */ |
||
515 | 1 | public function renderFile($entity, $id, $field) |
|
526 | |||
527 | /** |
||
528 | * The controller for the "delete file" action. |
||
529 | * |
||
530 | * @param string $entity |
||
531 | * the current entity |
||
532 | * @param string $id |
||
533 | * the instance id |
||
534 | * @param string $field |
||
535 | * the field of the file to delete of the instance |
||
536 | * |
||
537 | * @return Response |
||
538 | * redirects to the instance details page or 404 on invalid input |
||
539 | */ |
||
540 | 1 | public function deleteFile($entity, $id, $field) |
|
557 | |||
558 | /** |
||
559 | * The controller for serving static files. |
||
560 | * |
||
561 | * @param Request $request |
||
562 | * the current request |
||
563 | * |
||
564 | * @return Response |
||
565 | * redirects to the instance details page or 404 on invalid input |
||
566 | */ |
||
567 | 1 | public function staticFile(Request $request) |
|
590 | |||
591 | /** |
||
592 | * The controller for setting the locale. |
||
593 | * |
||
594 | * @param Request $request |
||
595 | * the current request |
||
596 | * @param string $locale |
||
597 | * the new locale |
||
598 | * |
||
599 | * @return Response |
||
600 | * redirects to the instance details page or 404 on invalid input |
||
601 | */ |
||
602 | 1 | public function setLocale(Request $request, $locale) |
|
616 | } |
||
617 |
$this->service->generate... $entity, 'id' => $id))
can contain request data and is used in output context(s) leading to a potential security vulnerability.8 paths for user data to reach this point
$this->parameters['PHP_AUTH_USER']
seems to return tainted data, and$headers
is assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']
seems to return tainted data, and$headers
is assignedin vendor/ServerBag.php on line 43
$headers
is assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()
is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$values
is assignedin vendor/HeaderBag.php on line 29
$values
is passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$values
is passed through array_values(), and$values
is assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headers
is assignedin vendor/HeaderBag.php on line 113
$host
is assignedin vendor/Request.php on line 1273
$host
is passed through trim(), andtrim($host)
is passed through preg_replace(), andpreg_replace('/:\\d+$/', '', trim($host))
is passed through strtolower(), and$host
is assignedin vendor/Request.php on line 1281
$request->getHost()
is passed to RequestContext::setHost()in vendor/RequestContext.php on line 68
$host
is passed through strtolower(), and RequestContext::$host is assignedin vendor/RequestContext.php on line 172
in vendor/RequestContext.php on line 160
$host
is assignedin vendor/Generator/UrlGenerator.php on line 184
$schemeAuthority
is assignedin vendor/Generator/UrlGenerator.php on line 233
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$this->parameters['PHP_AUTH_PW']
seems to return tainted data, and$headers
is assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']
seems to return tainted data, and$headers
is assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()
is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$values
is assignedin vendor/HeaderBag.php on line 29
$values
is passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$values
is passed through array_values(), and$values
is assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headers
is assignedin vendor/HeaderBag.php on line 113
$host
is assignedin vendor/Request.php on line 1273
$host
is passed through trim(), andtrim($host)
is passed through preg_replace(), andpreg_replace('/:\\d+$/', '', trim($host))
is passed through strtolower(), and$host
is assignedin vendor/Request.php on line 1281
$request->getHost()
is passed to RequestContext::setHost()in vendor/RequestContext.php on line 68
$host
is passed through strtolower(), and RequestContext::$host is assignedin vendor/RequestContext.php on line 172
in vendor/RequestContext.php on line 160
$host
is assignedin vendor/Generator/UrlGenerator.php on line 184
$schemeAuthority
is assignedin vendor/Generator/UrlGenerator.php on line 233
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$this->parameters['HTTP_AUTHORIZATION']
seems to return tainted data, and$authorizationHeader
is assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']
seems to return tainted data, and$authorizationHeader
is assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$_POST,
and$_POST
is passed to Request::createRequestFromFactory() in Request.php on line 314$_POST,
and$_POST
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$request
is passed to Request::__construct()in vendor/Request.php on line 2068
$request
is passed to Request::initialize()in vendor/Request.php on line 255
$request
is passed to ParameterBag::__construct()in vendor/Request.php on line 273
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$_SERVER,
and$server
is assigned in Request.php on line 304$_SERVER,
and$server
is assignedin vendor/Request.php on line 304
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$server
is passed to Request::__construct()in vendor/Request.php on line 2068
$server
is passed to Request::initialize()in vendor/Request.php on line 255
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
HTTP_CONTENT_LENGTH
from$_SERVER,
and$server
is assigned in Request.php on line 307HTTP_CONTENT_LENGTH
from$_SERVER,
and$server
is assignedin vendor/Request.php on line 307
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$server
is passed to Request::__construct()in vendor/Request.php on line 2068
$server
is passed to Request::initialize()in vendor/Request.php on line 255
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
HTTP_CONTENT_TYPE
from$_SERVER,
and$server
is assigned in Request.php on line 310HTTP_CONTENT_TYPE
from$_SERVER,
and$server
is assignedin vendor/Request.php on line 310
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$server
is passed to Request::__construct()in vendor/Request.php on line 2068
$server
is passed to Request::initialize()in vendor/Request.php on line 255
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
$server['HTTP_HOST']
seems to return tainted data, and$server
is assigned in Request.php on line 380$server['HTTP_HOST']
seems to return tainted data, and$server
is assignedin vendor/Request.php on line 380
$server
is assignedin vendor/Request.php on line 428
$server
is assignedin vendor/Request.php on line 429
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 431
$server
is passed to Request::__construct()in vendor/Request.php on line 2068
$server
is passed to Request::initialize()in vendor/Request.php on line 255
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$baseUrl
is assignedin vendor/Request.php on line 1877
$baseUrl
is passed through rtrim()in vendor/Request.php on line 1933
in vendor/Request.php on line 994
in vendor/Request.php on line 997
$request->getBaseUrl()
is passed to RequestContext::setBaseUrl()in vendor/RequestContext.php on line 65
in vendor/RequestContext.php on line 96
in vendor/RequestContext.php on line 84
$url
is assignedin vendor/Generator/UrlGenerator.php on line 239
in vendor/Generator/UrlGenerator.php on line 119
in src/CRUDlex/Service.php on line 394
in src/CRUDlex/Controller.php on line 108
Used in output context
in vendor/RedirectResponse.php on line 39
in vendor/RedirectResponse.php on line 92
in vendor/Response.php on line 391
in vendor/Response.php on line 350
Preventing Cross-Site-Scripting Attacks
Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.
In order to prevent this, make sure to escape all user-provided data:
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
For numeric data, we recommend to explicitly cast the data: