Complex classes like RequestConfiguration 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 RequestConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class RequestConfiguration |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var Request |
||
| 29 | */ |
||
| 30 | private $request; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var MetadataInterface |
||
| 34 | */ |
||
| 35 | private $metadata; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Parameters |
||
| 39 | */ |
||
| 40 | private $parameters; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param MetadataInterface $metadata |
||
| 44 | * @param Request $request |
||
| 45 | * @param Parameters $parameters |
||
| 46 | */ |
||
| 47 | public function __construct(MetadataInterface $metadata, Request $request, Parameters $parameters) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return Request |
||
| 56 | */ |
||
| 57 | public function getRequest() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return MetadataInterface |
||
| 64 | */ |
||
| 65 | public function getMetadata() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return Parameters |
||
| 72 | */ |
||
| 73 | public function getParameters() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return string|null |
||
| 80 | */ |
||
| 81 | public function getSection() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function isHtmlRequest() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $name |
||
| 96 | * |
||
| 97 | * @return string|null |
||
| 98 | */ |
||
| 99 | public function getDefaultTemplate($name) |
||
| 100 | { |
||
| 101 | $templatesNamespace = (string) $this->metadata->getTemplatesNamespace(); |
||
| 102 | |||
| 103 | if (false !== strpos($templatesNamespace, ':')) { |
||
| 104 | return sprintf('%s:%s.%s', $templatesNamespace ?: ':', $name, 'twig'); |
||
| 105 | } |
||
| 106 | |||
| 107 | return sprintf('%s/%s.%s', $templatesNamespace, $name, 'twig'); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $name |
||
| 112 | * |
||
| 113 | * @return mixed|null |
||
| 114 | */ |
||
| 115 | public function getTemplate($name) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string|null |
||
| 128 | */ |
||
| 129 | public function getFormType() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getFormOptions() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $name |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getRouteName($name) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param $name |
||
| 175 | * |
||
| 176 | * @return mixed|string|null |
||
| 177 | */ |
||
| 178 | public function getRedirectRoute($name) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get url hash fragment (#text) which is you configured. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getRedirectHash() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get redirect referer, This will detected by configuration |
||
| 215 | * If not exists, The `referrer` from headers will be used. |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getRedirectReferer() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param object|null $resource |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function getRedirectParameters($resource = null) |
||
| 241 | { |
||
| 242 | $redirect = $this->parameters->get('redirect'); |
||
| 243 | |||
| 244 | if ($this->areParametersIntentionallyEmptyArray($redirect)) { |
||
| 245 | return []; |
||
| 246 | } |
||
| 247 | |||
| 248 | if (!is_array($redirect)) { |
||
| 249 | $redirect = ['parameters' => []]; |
||
| 250 | } |
||
| 251 | |||
| 252 | $parameters = $redirect['parameters'] ?? []; |
||
| 253 | $parameters = $this->addExtraRedirectParameters($parameters); |
||
| 254 | |||
| 255 | if (null !== $resource) { |
||
| 256 | $parameters = $this->parseResourceValues($parameters, $resource); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $parameters; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param array $parameters |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | private function addExtraRedirectParameters($parameters) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isLimited() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return int|null |
||
| 293 | */ |
||
| 294 | public function getLimit() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function isPaginated() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return int |
||
| 317 | */ |
||
| 318 | public function getPaginationMaxPerPage() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function isFilterable() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $criteria |
||
| 333 | * |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function getCriteria(array $criteria = []) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public function isSortable() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $sorting |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getSorting(array $sorting = []) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param $parameter |
||
| 380 | * @param array $defaults |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function getRequestParameter($parameter, $defaults = []) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return string|null |
||
| 394 | */ |
||
| 395 | public function getRepositoryMethod() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function getRepositoryArguments() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string|null |
||
| 426 | */ |
||
| 427 | public function getFactoryMethod() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getFactoryArguments() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param null $message |
||
| 458 | * |
||
| 459 | * @return mixed|null |
||
| 460 | */ |
||
| 461 | public function getFlashMessage($message) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return mixed|null |
||
| 468 | */ |
||
| 469 | public function getSortablePosition() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return mixed|null |
||
| 476 | */ |
||
| 477 | public function getSerializationGroups() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @return mixed|null |
||
| 484 | */ |
||
| 485 | public function getSerializationVersion() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @return string|null |
||
| 492 | */ |
||
| 493 | public function getEvent() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | public function hasPermission() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $name |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | * |
||
| 511 | * @throws \LogicException |
||
| 512 | */ |
||
| 513 | public function getPermission($name) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return bool |
||
| 530 | */ |
||
| 531 | public function isHeaderRedirection() |
||
| 545 | |||
| 546 | public function getVars() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param array $parameters |
||
| 553 | * @param object $resource |
||
| 554 | * |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | private function parseResourceValues(array $parameters, $resource) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | public function hasGrid() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @return string |
||
| 588 | * |
||
| 589 | * @throws \LogicException |
||
| 590 | */ |
||
| 591 | public function getGrid() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | public function hasStateMachine() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | public function getStateMachineGraph() |
||
| 612 | { |
||
| 613 | $options = $this->parameters->get('state_machine'); |
||
| 614 | |||
| 615 | return $options['graph'] ?? null; |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | public function getStateMachineTransition() |
||
| 622 | { |
||
| 623 | $options = $this->parameters->get('state_machine'); |
||
| 624 | |||
| 625 | return $options['transition'] ?? null; |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @return bool |
||
| 630 | */ |
||
| 631 | public function isCsrfProtectionEnabled() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param mixed $redirect |
||
| 638 | * |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | private function areParametersIntentionallyEmptyArray($redirect) |
||
| 645 | } |
||
| 646 |