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 |
||
| 43 | class ControllerProvider implements ControllerProviderInterface |
||
| 44 | { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Generates the not found page. |
||
| 48 | * |
||
| 49 | * @param Application $app |
||
| 50 | * the Silex application |
||
| 51 | * @param string $error |
||
| 52 | * the cause of the not found error |
||
| 53 | * |
||
| 54 | * @return Response |
||
| 55 | * the rendered not found page with the status code 404 |
||
| 56 | */ |
||
| 57 | 9 | protected function getNotFoundPage(Application $app, $error) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Postprocesses the entity after modification by handling the uploaded |
||
| 68 | * files and setting the flash. |
||
| 69 | * |
||
| 70 | * @param Application $app |
||
| 71 | * the current application |
||
| 72 | * @param AbstractData $crudData |
||
| 73 | * the data instance of the entity |
||
| 74 | * @param Entity $instance |
||
| 75 | * the entity |
||
| 76 | * @param string $entity |
||
| 77 | * the name of the entity |
||
| 78 | * @param string $mode |
||
| 79 | * whether to 'edit' or to 'create' the entity |
||
| 80 | * |
||
| 81 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 82 | * the HTTP response of this modification |
||
| 83 | */ |
||
| 84 | 4 | protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Sets the flashes of a failed entity modification. |
||
| 102 | * |
||
| 103 | * @param Application $app |
||
| 104 | * the current application |
||
| 105 | * @param boolean $optimisticLocking |
||
| 106 | * whether the optimistic locking failed |
||
| 107 | * @param string $mode |
||
| 108 | * the modification mode, either 'create' or 'edit' |
||
| 109 | */ |
||
| 110 | 2 | protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
| 120 | * response. |
||
| 121 | * |
||
| 122 | * @param Application $app |
||
| 123 | * the current application |
||
| 124 | * @param AbstractData $crudData |
||
| 125 | * the data instance of the entity |
||
| 126 | * @param Entity $instance |
||
| 127 | * the entity |
||
| 128 | * @param string $entity |
||
| 129 | * the name of the entity |
||
| 130 | * @param boolean $edit |
||
| 131 | * whether to edit (true) or to create (false) the entity |
||
| 132 | * |
||
| 133 | * @return Response |
||
| 134 | * the HTTP response of this modification |
||
| 135 | */ |
||
| 136 | 5 | protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Gets the parameters for the redirection after deleting an entity. |
||
| 172 | * |
||
| 173 | * @param Request $request |
||
| 174 | * the current request |
||
| 175 | * @param string $entity |
||
| 176 | * the entity name |
||
| 177 | * @param string $redirectPage |
||
| 178 | * reference, where the page to redirect to will be stored |
||
| 179 | * |
||
| 180 | * @return array<string,string> |
||
| 181 | * the parameters of the redirection, entity and id |
||
| 182 | */ |
||
| 183 | 1 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Builds up the parameters of the list page filters. |
||
| 201 | * |
||
| 202 | * @param Request $request |
||
| 203 | * the current application |
||
| 204 | * @param EntityDefinition $definition |
||
| 205 | * the current entity definition |
||
| 206 | * @param array &$filter |
||
| 207 | * will hold a map of fields to request parameters for the filters |
||
| 208 | * @param boolean $filterActive |
||
| 209 | * reference, will be true if at least one filter is active |
||
| 210 | * @param array $filterToUse |
||
| 211 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
| 212 | * @param array $filterOperators |
||
| 213 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
| 214 | */ |
||
| 215 | 4 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Setups the templates. |
||
| 243 | * |
||
| 244 | * @param Application $app |
||
| 245 | * the Application instance of the Silex application |
||
| 246 | */ |
||
| 247 | 10 | protected function setupTemplates(Application $app) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Setups the routes. |
||
| 260 | * |
||
| 261 | * @param Application $app |
||
| 262 | * the Application instance of the Silex application |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | * the created controller factory |
||
| 266 | */ |
||
| 267 | 10 | protected function setupRoutes(Application $app) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Setups i18n. |
||
| 296 | * |
||
| 297 | * @param Application $app |
||
| 298 | * the Application instance of the Silex application |
||
| 299 | */ |
||
| 300 | protected function setupI18n(Application $app) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Implements ControllerProviderInterface::connect() connecting this |
||
| 313 | * controller. |
||
| 314 | * |
||
| 315 | * @param Application $app |
||
| 316 | * the Application instance of the Silex application |
||
| 317 | * |
||
| 318 | * @return \SilexController\Collection |
||
| 319 | * this method is expected to return the used ControllerCollection instance |
||
| 320 | */ |
||
| 321 | 10 | public function connect(Application $app) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * The controller for the "create" action. |
||
| 331 | * |
||
| 332 | * @param Application $app |
||
| 333 | * the Silex application |
||
| 334 | * @param string $entity |
||
| 335 | * the current entity |
||
| 336 | * |
||
| 337 | * @return Response |
||
| 338 | * the HTTP response of this action |
||
| 339 | */ |
||
| 340 | 4 | public function create(Application $app, $entity) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * The controller for the "show list" action. |
||
| 351 | * |
||
| 352 | * @param Request $request |
||
| 353 | * the current request |
||
| 354 | * @param Application $app |
||
| 355 | * the Silex application |
||
| 356 | * @param string $entity |
||
| 357 | * the current entity |
||
| 358 | * |
||
| 359 | * @return Response |
||
| 360 | * the HTTP response of this action or 404 on invalid input |
||
| 361 | */ |
||
| 362 | 4 | public function showList(Request $request, Application $app, $entity) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * The controller for the "show" action. |
||
| 410 | * |
||
| 411 | * @param Application $app |
||
| 412 | * the Silex application |
||
| 413 | * @param string $entity |
||
| 414 | * the current entity |
||
| 415 | * @param string $id |
||
| 416 | * the instance id to show |
||
| 417 | * |
||
| 418 | * @return Response |
||
| 419 | * the HTTP response of this action or 404 on invalid input |
||
| 420 | */ |
||
| 421 | 6 | public function show(Application $app, $entity, $id) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * The controller for the "edit" action. |
||
| 458 | * |
||
| 459 | * @param Application $app |
||
| 460 | * the Silex application |
||
| 461 | * @param string $entity |
||
| 462 | * the current entity |
||
| 463 | * @param string $id |
||
| 464 | * the instance id to edit |
||
| 465 | * |
||
| 466 | * @return Response |
||
| 467 | * the HTTP response of this action or 404 on invalid input |
||
| 468 | */ |
||
| 469 | 1 | public function edit(Application $app, $entity, $id) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * The controller for the "delete" action. |
||
| 482 | * |
||
| 483 | * @param Application $app |
||
| 484 | * the Silex application |
||
| 485 | * @param string $entity |
||
| 486 | * the current entity |
||
| 487 | * @param string $id |
||
| 488 | * the instance id to delete |
||
| 489 | * |
||
| 490 | * @return Response |
||
| 491 | * redirects to the entity list page or 404 on invalid input |
||
| 492 | */ |
||
| 493 | 1 | public function delete(Application $app, $entity, $id) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * The controller for the "render file" action. |
||
| 526 | * |
||
| 527 | * @param Application $app |
||
| 528 | * the Silex application |
||
| 529 | * @param string $entity |
||
| 530 | * the current entity |
||
| 531 | * @param string $id |
||
| 532 | * the instance id |
||
| 533 | * @param string $field |
||
| 534 | * the field of the file to render of the instance |
||
| 535 | * |
||
| 536 | * @return Response |
||
| 537 | * the rendered file |
||
| 538 | */ |
||
| 539 | 1 | public function renderFile(Application $app, $entity, $id, $field) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * The controller for the "delete file" action. |
||
| 553 | * |
||
| 554 | * @param Application $app |
||
| 555 | * the Silex application |
||
| 556 | * @param string $entity |
||
| 557 | * the current entity |
||
| 558 | * @param string $id |
||
| 559 | * the instance id |
||
| 560 | * @param string $field |
||
| 561 | * the field of the file to delete of the instance |
||
| 562 | * |
||
| 563 | * @return Response |
||
| 564 | * redirects to the instance details page or 404 on invalid input |
||
| 565 | */ |
||
| 566 | 1 | public function deleteFile(Application $app, $entity, $id, $field) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * The controller for serving static files. |
||
| 586 | * |
||
| 587 | * @param Request $request |
||
| 588 | * the current request |
||
| 589 | * @param Application $app |
||
| 590 | * the Silex application |
||
| 591 | * |
||
| 592 | * @return Response |
||
| 593 | * redirects to the instance details page or 404 on invalid input |
||
| 594 | */ |
||
| 595 | 1 | public function staticFile(Request $request, Application $app) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * The controller for setting the locale. |
||
| 622 | * |
||
| 623 | * @param Request $request |
||
| 624 | * the current request |
||
| 625 | * @param Application $app |
||
| 626 | * the Silex application |
||
| 627 | * @param string $locale |
||
| 628 | * the new locale |
||
| 629 | * |
||
| 630 | * @return Response |
||
| 631 | * redirects to the instance details page or 404 on invalid input |
||
| 632 | */ |
||
| 633 | 1 | public function setLocale(Request $request, Application $app, $locale) |
|
| 647 | } |
||
| 648 |
dirname($file)can contain request data and is used in file manipulation 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 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
$_POST,and$_POSTis passed to Request::createRequestFromFactory() in Request.php on line 317$_POST,and$_POSTis passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$requestis passed to Request::__construct()in vendor/Request.php on line 2018
$requestis passed to Request::initialize()in vendor/Request.php on line 258
$requestis passed to ParameterBag::__construct()in vendor/Request.php on line 276
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
$_SERVER,and$serveris assigned in Request.php on line 307$_SERVER,and$serveris assignedin vendor/Request.php on line 307
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 310HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 310
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 313HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 313
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 317
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 383$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 383
$serveris assignedin vendor/Request.php on line 431
$serveris assignedin vendor/Request.php on line 432
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 434
$serveris passed to Request::__construct()in vendor/Request.php on line 2018
$serveris passed to Request::initialize()in vendor/Request.php on line 258
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 281
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
$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 282
$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 143
in vendor/HeaderBag.php on line 146
in vendor/HeaderBag.php on line 67
$headersis assignedin vendor/HeaderBag.php on line 115
$requestUriis assignedin vendor/Request.php on line 1788
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1819
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
$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 282
$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 143
in vendor/HeaderBag.php on line 146
in vendor/HeaderBag.php on line 67
$headersis assignedin vendor/HeaderBag.php on line 115
$requestUriis assignedin vendor/Request.php on line 1788
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1819
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$resultis assignedin vendor/Request.php on line 798
$request->get('file')is passed through str_replace(), and$fileParamis assignedin src/CRUDlex/ControllerProvider.php on line 597
$fileis assignedin src/CRUDlex/ControllerProvider.php on line 598
$fileis passed through dirname()in src/CRUDlex/ControllerProvider.php on line 603
Used in path-write context
in vendor/src/Adapter/Local.php on line 78
in vendor/src/Adapter/Local.php on line 102
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: