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  | 
            ||
| 42 | class Controller { | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * Postprocesses the entity after modification by handling the uploaded  | 
            ||
| 46 | * files and setting the flash.  | 
            ||
| 47 | *  | 
            ||
| 48 | * @param Application $app  | 
            ||
| 49 | * the current application  | 
            ||
| 50 | * @param AbstractData $crudData  | 
            ||
| 51 | * the data instance of the entity  | 
            ||
| 52 | * @param Entity $instance  | 
            ||
| 53 | * the entity  | 
            ||
| 54 | * @param string $entity  | 
            ||
| 55 | * the name of the entity  | 
            ||
| 56 | * @param string $mode  | 
            ||
| 57 | * whether to 'edit' or to 'create' the entity  | 
            ||
| 58 | *  | 
            ||
| 59 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse  | 
            ||
| 60 | * the HTTP response of this modification  | 
            ||
| 61 | */  | 
            ||
| 62 | 4 | protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode)  | 
            |
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * Sets the flashes of a failed entity modification.  | 
            ||
| 80 | *  | 
            ||
| 81 | * @param Application $app  | 
            ||
| 82 | * the current application  | 
            ||
| 83 | * @param boolean $optimisticLocking  | 
            ||
| 84 | * whether the optimistic locking failed  | 
            ||
| 85 | * @param string $mode  | 
            ||
| 86 | * the modification mode, either 'create' or 'edit'  | 
            ||
| 87 | */  | 
            ||
| 88 | 2 | protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode)  | 
            |
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * Validates and saves the new or updated entity and returns the appropriate HTTP  | 
            ||
| 98 | * response.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @param Application $app  | 
            ||
| 101 | * the current application  | 
            ||
| 102 | * @param AbstractData $crudData  | 
            ||
| 103 | * the data instance of the entity  | 
            ||
| 104 | * @param Entity $instance  | 
            ||
| 105 | * the entity  | 
            ||
| 106 | * @param string $entity  | 
            ||
| 107 | * the name of the entity  | 
            ||
| 108 | * @param boolean $edit  | 
            ||
| 109 | * whether to edit (true) or to create (false) the entity  | 
            ||
| 110 | *  | 
            ||
| 111 | * @return Response  | 
            ||
| 112 | * the HTTP response of this modification  | 
            ||
| 113 | */  | 
            ||
| 114 | 5 | protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit)  | 
            |
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * Gets the parameters for the redirection after deleting an entity.  | 
            ||
| 151 | *  | 
            ||
| 152 | * @param Request $request  | 
            ||
| 153 | * the current request  | 
            ||
| 154 | * @param string $entity  | 
            ||
| 155 | * the entity name  | 
            ||
| 156 | * @param string $redirectPage  | 
            ||
| 157 | * reference, where the page to redirect to will be stored  | 
            ||
| 158 | *  | 
            ||
| 159 | * @return array<string,string>  | 
            ||
| 160 | * the parameters of the redirection, entity and id  | 
            ||
| 161 | */  | 
            ||
| 162 | 1 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage)  | 
            |
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * Builds up the parameters of the list page filters.  | 
            ||
| 180 | *  | 
            ||
| 181 | * @param Request $request  | 
            ||
| 182 | * the current application  | 
            ||
| 183 | * @param EntityDefinition $definition  | 
            ||
| 184 | * the current entity definition  | 
            ||
| 185 | * @param array &$filter  | 
            ||
| 186 | * will hold a map of fields to request parameters for the filters  | 
            ||
| 187 | * @param boolean $filterActive  | 
            ||
| 188 | * reference, will be true if at least one filter is active  | 
            ||
| 189 | * @param array $filterToUse  | 
            ||
| 190 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active  | 
            ||
| 191 | * @param array $filterOperators  | 
            ||
| 192 | * reference, will hold a map of fields to operators for AbstractData::listEntries()  | 
            ||
| 193 | */  | 
            ||
| 194 | 4 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators)  | 
            |
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * Generates the not found page.  | 
            ||
| 222 | *  | 
            ||
| 223 | * @param Application $app  | 
            ||
| 224 | * the Silex application  | 
            ||
| 225 | * @param string $error  | 
            ||
| 226 | * the cause of the not found error  | 
            ||
| 227 | *  | 
            ||
| 228 | * @return Response  | 
            ||
| 229 | * the rendered not found page with the status code 404  | 
            ||
| 230 | */  | 
            ||
| 231 | 9 | public function getNotFoundPage(Application $app, $error)  | 
            |
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * The controller for the "create" action.  | 
            ||
| 243 | *  | 
            ||
| 244 | * @param Application $app  | 
            ||
| 245 | * the Silex application  | 
            ||
| 246 | * @param string $entity  | 
            ||
| 247 | * the current entity  | 
            ||
| 248 | *  | 
            ||
| 249 | * @return Response  | 
            ||
| 250 | * the HTTP response of this action  | 
            ||
| 251 | */  | 
            ||
| 252 | 4 | public function create(Application $app, $entity)  | 
            |
| 260 | |||
| 261 | /**  | 
            ||
| 262 | * The controller for the "show list" action.  | 
            ||
| 263 | *  | 
            ||
| 264 | * @param Request $request  | 
            ||
| 265 | * the current request  | 
            ||
| 266 | * @param Application $app  | 
            ||
| 267 | * the Silex application  | 
            ||
| 268 | * @param string $entity  | 
            ||
| 269 | * the current entity  | 
            ||
| 270 | *  | 
            ||
| 271 | * @return Response  | 
            ||
| 272 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 273 | */  | 
            ||
| 274 | 4 | public function showList(Request $request, Application $app, $entity)  | 
            |
| 320 | |||
| 321 | /**  | 
            ||
| 322 | * The controller for the "show" action.  | 
            ||
| 323 | *  | 
            ||
| 324 | * @param Application $app  | 
            ||
| 325 | * the Silex application  | 
            ||
| 326 | * @param string $entity  | 
            ||
| 327 | * the current entity  | 
            ||
| 328 | * @param string $id  | 
            ||
| 329 | * the instance id to show  | 
            ||
| 330 | *  | 
            ||
| 331 | * @return Response  | 
            ||
| 332 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 333 | */  | 
            ||
| 334 | 6 | public function show(Application $app, $entity, $id)  | 
            |
| 369 | |||
| 370 | /**  | 
            ||
| 371 | * The controller for the "edit" action.  | 
            ||
| 372 | *  | 
            ||
| 373 | * @param Application $app  | 
            ||
| 374 | * the Silex application  | 
            ||
| 375 | * @param string $entity  | 
            ||
| 376 | * the current entity  | 
            ||
| 377 | * @param string $id  | 
            ||
| 378 | * the instance id to edit  | 
            ||
| 379 | *  | 
            ||
| 380 | * @return Response  | 
            ||
| 381 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 382 | */  | 
            ||
| 383 | 1 | public function edit(Application $app, $entity, $id)  | 
            |
| 393 | |||
| 394 | /**  | 
            ||
| 395 | * The controller for the "delete" action.  | 
            ||
| 396 | *  | 
            ||
| 397 | * @param Application $app  | 
            ||
| 398 | * the Silex application  | 
            ||
| 399 | * @param string $entity  | 
            ||
| 400 | * the current entity  | 
            ||
| 401 | * @param string $id  | 
            ||
| 402 | * the instance id to delete  | 
            ||
| 403 | *  | 
            ||
| 404 | * @return Response  | 
            ||
| 405 | * redirects to the entity list page or 404 on invalid input  | 
            ||
| 406 | */  | 
            ||
| 407 | 1 | public function delete(Application $app, $entity, $id)  | 
            |
| 437 | |||
| 438 | /**  | 
            ||
| 439 | * The controller for the "render file" action.  | 
            ||
| 440 | *  | 
            ||
| 441 | * @param Application $app  | 
            ||
| 442 | * the Silex application  | 
            ||
| 443 | * @param string $entity  | 
            ||
| 444 | * the current entity  | 
            ||
| 445 | * @param string $id  | 
            ||
| 446 | * the instance id  | 
            ||
| 447 | * @param string $field  | 
            ||
| 448 | * the field of the file to render of the instance  | 
            ||
| 449 | *  | 
            ||
| 450 | * @return Response  | 
            ||
| 451 | * the rendered file  | 
            ||
| 452 | */  | 
            ||
| 453 | 1 | public function renderFile(Application $app, $entity, $id, $field)  | 
            |
| 464 | |||
| 465 | /**  | 
            ||
| 466 | * The controller for the "delete file" action.  | 
            ||
| 467 | *  | 
            ||
| 468 | * @param Application $app  | 
            ||
| 469 | * the Silex application  | 
            ||
| 470 | * @param string $entity  | 
            ||
| 471 | * the current entity  | 
            ||
| 472 | * @param string $id  | 
            ||
| 473 | * the instance id  | 
            ||
| 474 | * @param string $field  | 
            ||
| 475 | * the field of the file to delete of the instance  | 
            ||
| 476 | *  | 
            ||
| 477 | * @return Response  | 
            ||
| 478 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 479 | */  | 
            ||
| 480 | 1 | public function deleteFile(Application $app, $entity, $id, $field)  | 
            |
| 497 | |||
| 498 | /**  | 
            ||
| 499 | * The controller for serving static files.  | 
            ||
| 500 | *  | 
            ||
| 501 | * @param Request $request  | 
            ||
| 502 | * the current request  | 
            ||
| 503 | * @param Application $app  | 
            ||
| 504 | * the Silex application  | 
            ||
| 505 | *  | 
            ||
| 506 | * @return Response  | 
            ||
| 507 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 508 | */  | 
            ||
| 509 | 1 | public function staticFile(Request $request, Application $app)  | 
            |
| 532 | |||
| 533 | /**  | 
            ||
| 534 | * The controller for setting the locale.  | 
            ||
| 535 | *  | 
            ||
| 536 | * @param Request $request  | 
            ||
| 537 | * the current request  | 
            ||
| 538 | * @param Application $app  | 
            ||
| 539 | * the Silex application  | 
            ||
| 540 | * @param string $locale  | 
            ||
| 541 | * the new locale  | 
            ||
| 542 | *  | 
            ||
| 543 | * @return Response  | 
            ||
| 544 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 545 | */  | 
            ||
| 546 | 1 | public function setLocale(Request $request, Application $app, $locale)  | 
            |
| 560 | }  | 
            
$redirectcan 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['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$_POST,and$_POSTis passed to Request::createRequestFromFactory() in Request.php on line 314$_POST,and$_POSTis passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$requestis passed to Request::__construct()in vendor/Request.php on line 2068
$requestis passed to Request::initialize()in vendor/Request.php on line 255
$requestis 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
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$_SERVER,and$serveris assigned in Request.php on line 304$_SERVER,and$serveris assignedin vendor/Request.php on line 304
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris 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
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 307HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 307
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris 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
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 310HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 310
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris 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
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 380$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 380
$serveris assignedin vendor/Request.php on line 428
$serveris assignedin vendor/Request.php on line 429
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 431
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris 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
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 43
$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$requestUriis assignedin vendor/Request.php on line 1831
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1862
in vendor/ParameterBag.php on line 95
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$requestUriis assignedin vendor/Request.php on line 1831
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1862
in vendor/ParameterBag.php on line 95
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
Used in output context
in vendor/src/Silex/Application.php on line 376
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:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: