Complex classes like SearchRequest 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 SearchRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class SearchRequest |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Default namespace overwritten with the configured plugin namespace. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $argumentNameSpace = 'tx_solr'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Arguments that should be kept for sub requests. |
||
| 54 | * |
||
| 55 | * Default values, overwritten in the constructor with the namespaced arguments |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $persistentArgumentsPaths = ['tx_solr:q', 'tx_solr:filter', 'tx_solr:sort']; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $stateChanged = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ArrayAccessor |
||
| 68 | */ |
||
| 69 | protected $argumentsAccessor; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The sys_language_uid that was used in the context where the request was build. |
||
| 73 | * This could be different from the "L" parameter and and not relevant for urls, |
||
| 74 | * because typolink itself will handle it. |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $contextSystemLanguageUid; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The page_uid that was used in the context where the request was build. |
||
| 82 | * |
||
| 83 | * The pageUid is not relevant for the typolink additionalArguments and therefore |
||
| 84 | * a separate property. |
||
| 85 | * |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected $contextPageUid; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var TypoScriptConfiguration |
||
| 92 | */ |
||
| 93 | protected $contextTypoScriptConfiguration; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $persistedArguments = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param array $argumentsArray |
||
| 102 | * @param int $pageUid |
||
| 103 | * @param int $sysLanguageUid |
||
| 104 | * @param TypoScriptConfiguration $typoScriptConfiguration |
||
| 105 | 86 | */ |
|
| 106 | public function __construct(array $argumentsArray = [], $pageUid = 0, $sysLanguageUid = 0, TypoScriptConfiguration $typoScriptConfiguration = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | 28 | */ |
|
| 127 | public function getId() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Can be used do merge arguments into the request arguments |
||
| 134 | * |
||
| 135 | * @param array $argumentsToMerge |
||
| 136 | * @return SearchRequest |
||
| 137 | 1 | */ |
|
| 138 | public function mergeArguments(array $argumentsToMerge) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Helper method to prefix an accessor with the arguments namespace. |
||
| 152 | * |
||
| 153 | * @param string $path |
||
| 154 | * @return string |
||
| 155 | 68 | */ |
|
| 156 | protected function prefixWithNamespace($path) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | 25 | */ |
|
| 164 | public function getActiveFacetNames() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns all facet values for a certain facetName |
||
| 178 | * @param string $facetName |
||
| 179 | * @return array |
||
| 180 | 36 | */ |
|
| 181 | public function getActiveFacetValuesByName($facetName) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | 43 | */ |
|
| 199 | public function getActiveFacets() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return int |
||
| 209 | 2 | */ |
|
| 210 | public function getActiveFacetCount() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $activeFacets |
||
| 217 | * |
||
| 218 | * @return SearchRequest |
||
| 219 | 37 | */ |
|
| 220 | protected function setActiveFacets($activeFacets = []) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Adds a facet value to the request. |
||
| 230 | * |
||
| 231 | * @param string $facetName |
||
| 232 | * @param mixed $facetValue |
||
| 233 | * |
||
| 234 | * @return SearchRequest |
||
| 235 | 32 | */ |
|
| 236 | public function addFacetValue($facetName, $facetValue) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Removes a facet value from the request. |
||
| 252 | * |
||
| 253 | * @param string $facetName |
||
| 254 | * @param mixed $facetValue |
||
| 255 | * |
||
| 256 | * @return SearchRequest |
||
| 257 | 4 | */ |
|
| 258 | public function removeFacetValue($facetName, $facetValue) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Removes all facet values from the request by a certain facet name |
||
| 280 | * |
||
| 281 | * @param string $facetName |
||
| 282 | * |
||
| 283 | * @return SearchRequest |
||
| 284 | 3 | */ |
|
| 285 | public function removeAllFacetValuesByName($facetName) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Removes all active facets from the request. |
||
| 300 | * |
||
| 301 | * @return SearchRequest |
||
| 302 | 4 | */ |
|
| 303 | public function removeAllFacets() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $facetName |
||
| 313 | * @param mixed $facetValue |
||
| 314 | * @return bool |
||
| 315 | 36 | */ |
|
| 316 | public function getHasFacetValue($facetName, $facetValue) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return bool |
||
| 324 | 40 | */ |
|
| 325 | public function getHasSorting() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns the sorting string in the url e.g. title asc. |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | 39 | */ |
|
| 336 | public function getSorting() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Helper function to get the sorting configuration name or direction. |
||
| 344 | * |
||
| 345 | * @param int $index |
||
| 346 | * @return string |
||
| 347 | 39 | */ |
|
| 348 | protected function getSortingPart($index) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the sorting configuration name that is currently used. |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | 39 | */ |
|
| 364 | public function getSortingName() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the sorting direction that is currently used. |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | 38 | */ |
|
| 374 | public function getSortingDirection() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return SearchRequest |
||
| 381 | 28 | */ |
|
| 382 | public function removeSorting() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $sortingName |
||
| 392 | * @param string $direction (asc or desc) |
||
| 393 | * |
||
| 394 | * @return SearchRequest |
||
| 395 | 29 | */ |
|
| 396 | public function setSorting($sortingName, $direction = 'asc') |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Method to set the paginated page of the search |
||
| 407 | * |
||
| 408 | * @param int $page |
||
| 409 | * @return SearchRequest |
||
| 410 | 13 | */ |
|
| 411 | public function setPage($page) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Returns the passed page. |
||
| 421 | * |
||
| 422 | * @return int|null |
||
| 423 | 41 | */ |
|
| 424 | public function getPage() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Can be used to paginate within a groupItem. |
||
| 432 | * |
||
| 433 | * @param string $groupName e.g. type |
||
| 434 | * @param string $groupItemValue e.g. pages |
||
| 435 | 38 | * @param int $page |
|
| 436 | * @return SearchRequest |
||
| 437 | 38 | */ |
|
| 438 | 38 | public function setGroupItemPage(string $groupName, string $groupItemValue, int $page): SearchRequest |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Retrieves the current page for this group item. |
||
| 448 | 41 | * |
|
| 449 | * @param string $groupName |
||
| 450 | 41 | * @param string $groupItemValue |
|
| 451 | 41 | * @return int |
|
| 452 | 41 | */ |
|
| 453 | public function getGroupItemPage(string $groupName, string $groupItemValue): int |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Retrieves the highest page of the groups. |
||
| 461 | * |
||
| 462 | 38 | * @return int |
|
| 463 | */ |
||
| 464 | 38 | public function getHighestGroupPage() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Method to overwrite the query string. |
||
| 483 | * |
||
| 484 | 40 | * @param string $rawQueryString |
|
| 485 | * @return SearchRequest |
||
| 486 | 40 | */ |
|
| 487 | 40 | public function setRawQueryString($rawQueryString) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Returns the passed rawQueryString. |
||
| 497 | 43 | * |
|
| 498 | * @return string|null |
||
| 499 | 43 | */ |
|
| 500 | 43 | public function getRawUserQuery() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Method to check if the query string is an empty string |
||
| 509 | 1 | * (also empty string or whitespaces only are handled as empty). |
|
| 510 | * |
||
| 511 | 1 | * When no query string is set (null) the method returns false. |
|
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | public function getRawUserQueryIsEmptyString() |
||
| 529 | 30 | ||
| 530 | /** |
||
| 531 | * This method returns true when no querystring is present at all. |
||
| 532 | * Which means no search by the user was triggered |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | 30 | */ |
|
| 536 | public function getRawUserQueryIsNull() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Sets the results per page that are used during search. |
||
| 545 | 48 | * |
|
| 546 | * @param int $resultsPerPage |
||
| 547 | 48 | * @return SearchRequest |
|
| 548 | */ |
||
| 549 | public function setResultsPerPage($resultsPerPage) |
||
| 557 | 84 | ||
| 558 | 84 | /** |
|
| 559 | 84 | * @return bool |
|
| 560 | */ |
||
| 561 | public function getStateChanged() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Returns the passed resultsPerPage value |
||
| 568 | 35 | * @return int|null |
|
| 569 | */ |
||
| 570 | 35 | public function getResultsPerPage() |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @return int |
||
| 578 | */ |
||
| 579 | public function getContextSystemLanguageUid() |
||
| 583 | 35 | ||
| 584 | 35 | /** |
|
| 585 | * @return int |
||
| 586 | */ |
||
| 587 | public function getContextPageUid() |
||
| 591 | 35 | ||
| 592 | 35 | /** |
|
| 593 | * Get contextTypoScriptConfiguration |
||
| 594 | * |
||
| 595 | * @return TypoScriptConfiguration |
||
| 596 | */ |
||
| 597 | public function getContextTypoScriptConfiguration() |
||
| 601 | 22 | ||
| 602 | /** |
||
| 603 | * Assigns the last known persistedArguments and restores their state. |
||
| 604 | * |
||
| 605 | * @return SearchRequest |
||
| 606 | */ |
||
| 607 | 44 | public function reset() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * This can be used to start a new sub request, e.g. for a faceted search. |
||
| 616 | * |
||
| 617 | 32 | * @param bool $onlyPersistentArguments |
|
| 618 | 32 | * @return SearchRequest |
|
| 619 | */ |
||
| 620 | public function getCopyForSubRequest($onlyPersistentArguments = true) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | public function getArgumentNameSpace() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return array |
||
| 658 | */ |
||
| 659 | public function getAsArray() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Returns only the arguments as array. |
||
| 666 | * |
||
| 667 | * @return array |
||
| 668 | */ |
||
| 669 | public function getArguments() { |
||
| 672 | } |
||
| 673 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.