Complex classes like ControllerProvider 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 ControllerProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 41 | class ControllerProvider implements ControllerProviderInterface { | 
            ||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * Generates the not found page.  | 
            ||
| 45 | *  | 
            ||
| 46 | * @param Application $app  | 
            ||
| 47 | * the Silex application  | 
            ||
| 48 | * @param string $error  | 
            ||
| 49 | * the cause of the not found error  | 
            ||
| 50 | *  | 
            ||
| 51 | * @return Response  | 
            ||
| 52 | * the rendered not found page with the status code 404  | 
            ||
| 53 | */  | 
            ||
| 54 |     protected function getNotFoundPage(Application $app, $error) { | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * Postprocesses the entity after modification by handling the uploaded  | 
            ||
| 64 | * files and setting the flash.  | 
            ||
| 65 | *  | 
            ||
| 66 | * @param Application $app  | 
            ||
| 67 | * the current application  | 
            ||
| 68 | * @param AbstractData $crudData  | 
            ||
| 69 | * the data instance of the entity  | 
            ||
| 70 | * @param Entity $instance  | 
            ||
| 71 | * the entity  | 
            ||
| 72 | * @param string $entity  | 
            ||
| 73 | * the name of the entity  | 
            ||
| 74 | * @param string $mode  | 
            ||
| 75 | * whether to 'edit' or to 'create' the entity  | 
            ||
| 76 | *  | 
            ||
| 77 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse  | 
            ||
| 78 | * the HTTP response of this modification  | 
            ||
| 79 | */  | 
            ||
| 80 |     protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) { | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Sets the flashes of a failed entity modification.  | 
            ||
| 96 | *  | 
            ||
| 97 | * @param Application $app  | 
            ||
| 98 | * the current application  | 
            ||
| 99 | * @param boolean $optimisticLocking  | 
            ||
| 100 | * whether the optimistic locking failed  | 
            ||
| 101 | * @param string $mode  | 
            ||
| 102 | * the modification mode, either 'create' or 'edit'  | 
            ||
| 103 | */  | 
            ||
| 104 |     protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) { | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Validates and saves the new or updated entity and returns the appropriate HTTP  | 
            ||
| 113 | * response.  | 
            ||
| 114 | *  | 
            ||
| 115 | * @param Application $app  | 
            ||
| 116 | * the current application  | 
            ||
| 117 | * @param AbstractData $crudData  | 
            ||
| 118 | * the data instance of the entity  | 
            ||
| 119 | * @param Entity $instance  | 
            ||
| 120 | * the entity  | 
            ||
| 121 | * @param string $entity  | 
            ||
| 122 | * the name of the entity  | 
            ||
| 123 | * @param boolean $edit  | 
            ||
| 124 | * whether to edit (true) or to create (false) the entity  | 
            ||
| 125 | *  | 
            ||
| 126 | * @return Response  | 
            ||
| 127 | * the HTTP response of this modification  | 
            ||
| 128 | */  | 
            ||
| 129 |     protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) { | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * Gets the parameters for the redirection after deleting an entity.  | 
            ||
| 164 | *  | 
            ||
| 165 | * @param Request $request  | 
            ||
| 166 | * the current request  | 
            ||
| 167 | * @param string $entity  | 
            ||
| 168 | * the entity name  | 
            ||
| 169 | * @param string $redirectPage  | 
            ||
| 170 | * reference, where the page to redirect to will be stored  | 
            ||
| 171 | *  | 
            ||
| 172 | * @return array<string,string>  | 
            ||
| 173 | * the parameters of the redirection, entity and id  | 
            ||
| 174 | */  | 
            ||
| 175 |     protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) { | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Builds up the parameters of the list page filters.  | 
            ||
| 192 | *  | 
            ||
| 193 | * @param Request $request  | 
            ||
| 194 | * the current application  | 
            ||
| 195 | * @param EntityDefinition $definition  | 
            ||
| 196 | * the current entity definition  | 
            ||
| 197 | * @param array &$filter  | 
            ||
| 198 | * will hold a map of fields to request parameters for the filters  | 
            ||
| 199 | * @param boolean $filterActive  | 
            ||
| 200 | * reference, will be true if at least one filter is active  | 
            ||
| 201 | * @param array $filterToUse  | 
            ||
| 202 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active  | 
            ||
| 203 | * @param array $filterOperators  | 
            ||
| 204 | * reference, will hold a map of fields to operators for AbstractData::listEntries()  | 
            ||
| 205 | */  | 
            ||
| 206 |     protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) { | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * Setups the templates.  | 
            ||
| 232 | *  | 
            ||
| 233 | * @param Application $app  | 
            ||
| 234 | * the Application instance of the Silex application  | 
            ||
| 235 | */  | 
            ||
| 236 |     protected function setupTemplates(Application $app) { | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * Setups the routes.  | 
            ||
| 248 | *  | 
            ||
| 249 | * @param Application $app  | 
            ||
| 250 | * the Application instance of the Silex application  | 
            ||
| 251 | *  | 
            ||
| 252 | * @return mixed  | 
            ||
| 253 | * the created controller factory  | 
            ||
| 254 | */  | 
            ||
| 255 |     protected function setupRoutes(Application $app) { | 
            ||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * Setups i18n.  | 
            ||
| 281 | *  | 
            ||
| 282 | * @param Application $app  | 
            ||
| 283 | * the Application instance of the Silex application  | 
            ||
| 284 | */  | 
            ||
| 285 |     protected function setupI18n(Application $app) { | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * Implements ControllerProviderInterface::connect() connecting this  | 
            ||
| 298 | * controller.  | 
            ||
| 299 | *  | 
            ||
| 300 | * @param Application $app  | 
            ||
| 301 | * the Application instance of the Silex application  | 
            ||
| 302 | *  | 
            ||
| 303 | * @return \SilexController\Collection  | 
            ||
| 304 | * this method is expected to return the used ControllerCollection instance  | 
            ||
| 305 | */  | 
            ||
| 306 |     public function connect(Application $app) { | 
            ||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * The controller for the "create" action.  | 
            ||
| 315 | *  | 
            ||
| 316 | * @param Application $app  | 
            ||
| 317 | * the Silex application  | 
            ||
| 318 | * @param string $entity  | 
            ||
| 319 | * the current entity  | 
            ||
| 320 | *  | 
            ||
| 321 | * @return Response  | 
            ||
| 322 | * the HTTP response of this action  | 
            ||
| 323 | */  | 
            ||
| 324 |     public function create(Application $app, $entity) { | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * The controller for the "show list" action.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @param Request $request  | 
            ||
| 338 | * the current request  | 
            ||
| 339 | * @param Application $app  | 
            ||
| 340 | * the Silex application  | 
            ||
| 341 | * @param string $entity  | 
            ||
| 342 | * the current entity  | 
            ||
| 343 | *  | 
            ||
| 344 | * @return Response  | 
            ||
| 345 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 346 | */  | 
            ||
| 347 |     public function showList(Request $request, Application $app, $entity) { | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * The controller for the "show" action.  | 
            ||
| 398 | *  | 
            ||
| 399 | * @param Application $app  | 
            ||
| 400 | * the Silex application  | 
            ||
| 401 | * @param string $entity  | 
            ||
| 402 | * the current entity  | 
            ||
| 403 | * @param string $id  | 
            ||
| 404 | * the instance id to show  | 
            ||
| 405 | *  | 
            ||
| 406 | * @return Response  | 
            ||
| 407 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 408 | */  | 
            ||
| 409 |     public function show(Application $app, $entity, $id) { | 
            ||
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * The controller for the "edit" action.  | 
            ||
| 450 | *  | 
            ||
| 451 | * @param Application $app  | 
            ||
| 452 | * the Silex application  | 
            ||
| 453 | * @param string $entity  | 
            ||
| 454 | * the current entity  | 
            ||
| 455 | * @param string $id  | 
            ||
| 456 | * the instance id to edit  | 
            ||
| 457 | *  | 
            ||
| 458 | * @return Response  | 
            ||
| 459 | * the HTTP response of this action or 404 on invalid input  | 
            ||
| 460 | */  | 
            ||
| 461 |     public function edit(Application $app, $entity, $id) { | 
            ||
| 473 | |||
| 474 | /**  | 
            ||
| 475 | * The controller for the "delete" action.  | 
            ||
| 476 | *  | 
            ||
| 477 | * @param Application $app  | 
            ||
| 478 | * the Silex application  | 
            ||
| 479 | * @param string $entity  | 
            ||
| 480 | * the current entity  | 
            ||
| 481 | * @param string $id  | 
            ||
| 482 | * the instance id to delete  | 
            ||
| 483 | *  | 
            ||
| 484 | * @return Response  | 
            ||
| 485 | * redirects to the entity list page or 404 on invalid input  | 
            ||
| 486 | */  | 
            ||
| 487 |     public function delete(Application $app, $entity, $id) { | 
            ||
| 518 | |||
| 519 | /**  | 
            ||
| 520 | * The controller for the "render file" action.  | 
            ||
| 521 | *  | 
            ||
| 522 | * @param Application $app  | 
            ||
| 523 | * the Silex application  | 
            ||
| 524 | * @param string $entity  | 
            ||
| 525 | * the current entity  | 
            ||
| 526 | * @param string $id  | 
            ||
| 527 | * the instance id  | 
            ||
| 528 | * @param string $field  | 
            ||
| 529 | * the field of the file to render of the instance  | 
            ||
| 530 | *  | 
            ||
| 531 | * @return Response  | 
            ||
| 532 | * the rendered file  | 
            ||
| 533 | */  | 
            ||
| 534 |     public function renderFile(Application $app, $entity, $id, $field) { | 
            ||
| 535 | $crudData = $app['crud']->getData($entity);  | 
            ||
| 536 |         if (!$crudData) { | 
            ||
| 537 |             return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.entityNotFound')); | 
            ||
| 538 | }  | 
            ||
| 539 | $instance = $crudData->get($id);  | 
            ||
| 540 | $definition = $crudData->getDefinition();  | 
            ||
| 541 |         if (!$instance || $definition->getType($field) != 'file' || !$instance->get($field)) { | 
            ||
| 542 |             return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); | 
            ||
| 543 | }  | 
            ||
| 544 | return $crudData->renderFile($instance, $entity, $field);  | 
            ||
| 545 | }  | 
            ||
| 546 | |||
| 547 | /**  | 
            ||
| 548 | * The controller for the "delete file" action.  | 
            ||
| 549 | *  | 
            ||
| 550 | * @param Application $app  | 
            ||
| 551 | * the Silex application  | 
            ||
| 552 | * @param string $entity  | 
            ||
| 553 | * the current entity  | 
            ||
| 554 | * @param string $id  | 
            ||
| 555 | * the instance id  | 
            ||
| 556 | * @param string $field  | 
            ||
| 557 | * the field of the file to delete of the instance  | 
            ||
| 558 | *  | 
            ||
| 559 | * @return Response  | 
            ||
| 560 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 561 | */  | 
            ||
| 562 |     public function deleteFile(Application $app, $entity, $id, $field) { | 
            ||
| 580 | |||
| 581 | /**  | 
            ||
| 582 | * The controller for serving static files.  | 
            ||
| 583 | *  | 
            ||
| 584 | * @param Request $request  | 
            ||
| 585 | * the current request  | 
            ||
| 586 | * @param Application $app  | 
            ||
| 587 | * the Silex application  | 
            ||
| 588 | *  | 
            ||
| 589 | * @return Response  | 
            ||
| 590 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 591 | */  | 
            ||
| 592 |     public function staticFile(Request $request, Application $app) { | 
            ||
| 613 | |||
| 614 | /**  | 
            ||
| 615 | * The controller for setting the locale.  | 
            ||
| 616 | *  | 
            ||
| 617 | * @param Request $request  | 
            ||
| 618 | * the current request  | 
            ||
| 619 | * @param Application $app  | 
            ||
| 620 | * the Silex application  | 
            ||
| 621 | * @param string $locale  | 
            ||
| 622 | * the new locale  | 
            ||
| 623 | *  | 
            ||
| 624 | * @return Response  | 
            ||
| 625 | * redirects to the instance details page or 404 on invalid input  | 
            ||
| 626 | */  | 
            ||
| 627 |     public function setLocale(Request $request, Application $app, $locale) { | 
            ||
| 639 | }  | 
            ||
| 640 | 
$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 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
$_POST,and$_POSTis passed to Request::createRequestFromFactory() in Request.php on line 281$_POST,and$_POSTis passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$requestis passed to Request::__construct()in vendor/Request.php on line 1929
$requestis passed to Request::initialize()in vendor/Request.php on line 222
$requestis passed to ParameterBag::__construct()in vendor/Request.php on line 240
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
$_SERVER,and$serveris assigned in Request.php on line 271$_SERVER,and$serveris assignedin vendor/Request.php on line 271
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$serveris passed to Request::__construct()in vendor/Request.php on line 1929
$serveris passed to Request::initialize()in vendor/Request.php on line 222
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 274HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 274
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$serveris passed to Request::__construct()in vendor/Request.php on line 1929
$serveris passed to Request::initialize()in vendor/Request.php on line 222
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 277HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 277
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$serveris passed to Request::__construct()in vendor/Request.php on line 1929
$serveris passed to Request::initialize()in vendor/Request.php on line 222
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 347$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 347
$serveris assignedin vendor/Request.php on line 395
$serveris assignedin vendor/Request.php on line 396
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 398
$serveris passed to Request::__construct()in vendor/Request.php on line 1929
$serveris passed to Request::initialize()in vendor/Request.php on line 222
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
$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 246
$valuesis assignedin vendor/HeaderBag.php on line 31
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $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 125
$requestUriis assignedin vendor/Request.php on line 1699
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1730
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
$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 246
$valuesis assignedin vendor/HeaderBag.php on line 31
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $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 125
$requestUriis assignedin vendor/Request.php on line 1699
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1730
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 719
$redirectis assignedin src/CRUDlex/ControllerProvider.php on line 636
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 82
in vendor/Response.php on line 406
in vendor/Response.php on line 365
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: