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 |
||
| 37 | class SearchRequest |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $id; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $argumentNameSpace = 'tx_solr'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Arguments that should be kept for sub requests. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $persistentArgumentsPaths = array('q', 'tx_solr:filter', 'tx_solr:sort'); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $stateChanged = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ArrayAccessor |
||
| 63 | */ |
||
| 64 | protected $argumentsAccessor; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The sys_language_uid that was used in the context where the request was build. |
||
| 68 | * This could be different from the "L" parameter and and not relevant for urls, |
||
| 69 | * because typolink itself will handle it. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $contextSystemLanguageUid; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The page_uid that was used in the context where the request was build. |
||
| 77 | * |
||
| 78 | * The pageUid is not relevant for the typolink additionalArguments and therefore |
||
| 79 | * a separate property. |
||
| 80 | * |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | protected $contextPageUid; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var TypoScriptConfiguration |
||
| 87 | */ |
||
| 88 | protected $contextTypoScriptConfiguration; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $argumentsArray |
||
| 92 | * @param int $pageUid |
||
| 93 | * @param int $sysLanguageUid |
||
| 94 | * @param TypoScriptConfiguration $typoScriptConfiguration |
||
| 95 | */ |
||
| 96 | 56 | public function __construct(array $argumentsArray = array(), $pageUid = 0, $sysLanguageUid = 0, TypoScriptConfiguration $typoScriptConfiguration = null) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 1 | public function getId() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Can be used do merge arguments into the request arguments |
||
| 117 | * |
||
| 118 | * @param array $argumentsToMerge |
||
| 119 | * @return SearchRequest |
||
| 120 | */ |
||
| 121 | 24 | public function mergeArguments(array $argumentsToMerge) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Helper method to prefix an accessor with the arguments namespace. |
||
| 135 | * |
||
| 136 | * @param $path |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 47 | protected function prefixWithNamespace($path) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | 1 | public function getActiveFacetNames() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Returns all facet values for a certain facetName |
||
| 161 | * @param string $facetName |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 1 | public function getActiveFacetValuesByName($facetName) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @return array|null |
||
| 181 | */ |
||
| 182 | 11 | protected function getActiveFacets() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | 2 | public function getActiveFacetCount() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param $activeFacets |
||
| 198 | * @return array|null |
||
| 199 | * |
||
| 200 | * @return SearchRequest |
||
| 201 | */ |
||
| 202 | 8 | protected function setActiveFacets($activeFacets = array()) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Adds a facet value to the request. |
||
| 212 | * |
||
| 213 | * @param string $facetName |
||
| 214 | * @param mixed $facetValue |
||
| 215 | * |
||
| 216 | * @return SearchRequest |
||
| 217 | */ |
||
| 218 | 6 | public function addFacetValue($facetName, $facetValue) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Removes a facet value from the request. |
||
| 234 | * |
||
| 235 | * @param string $facetName |
||
| 236 | * @param mixed $facetValue |
||
| 237 | * |
||
| 238 | * @return SearchRequest |
||
| 239 | */ |
||
| 240 | 1 | public function removeFacetValue($facetName, $facetValue) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Removes all facet values from the request by a certain facet name |
||
| 262 | * |
||
| 263 | * @param string $facetName |
||
| 264 | * |
||
| 265 | * @return SearchRequest |
||
| 266 | */ |
||
| 267 | 1 | public function removeAllFacetValuesByName($facetName) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Removes all active facets from the request. |
||
| 286 | * |
||
| 287 | * @return SearchRequest |
||
| 288 | */ |
||
| 289 | 1 | public function removeAllFacets() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $facetName |
||
| 299 | * @param mixed $facetValue |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | 7 | public function getHasFacetValue($facetName, $facetValue) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 3 | public function getHasSorting() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the sorting string in the url e.g. title asc. |
||
| 325 | * |
||
| 326 | * @return string|null |
||
| 327 | */ |
||
| 328 | 2 | protected function getSorting() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Helper function to get the sorting configuration name or direction. |
||
| 336 | * |
||
| 337 | * @param $index |
||
| 338 | * @return null |
||
| 339 | */ |
||
| 340 | 2 | protected function getSortingPart($index) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the sorting configuration name that is currently used. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 2 | public function getSortingName() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Returns the sorting direction that is currently used. |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 1 | public function getSortingDirection() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @return SearchRequest |
||
| 373 | */ |
||
| 374 | 1 | public function removeSorting() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $sortingName |
||
| 384 | * @param string $direction (asc or desc) |
||
| 385 | * |
||
| 386 | * @return SearchRequest |
||
| 387 | */ |
||
| 388 | 1 | public function setSorting($sortingName, $direction = 'asc') |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Method to set the paginated page of the search |
||
| 399 | * |
||
| 400 | * @param int $page |
||
| 401 | * @return SearchRequest |
||
| 402 | */ |
||
| 403 | 1 | public function setPage($page) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Returns the passed page. |
||
| 413 | * |
||
| 414 | * @return int|null |
||
| 415 | */ |
||
| 416 | 32 | public function getPage() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Method to overwrite the query string. |
||
| 424 | * |
||
| 425 | * @param string $rawQueryString |
||
| 426 | * @return SearchRequest |
||
| 427 | */ |
||
| 428 | 9 | public function setRawQueryString($rawQueryString) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the passed rawQueryString. |
||
| 437 | * |
||
| 438 | * @return int|string |
||
| 439 | */ |
||
| 440 | 31 | public function getRawUserQuery() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Method to check if the query string is an empty string |
||
| 447 | * (also empty string or whitespaces only are handled as empty). |
||
| 448 | * |
||
| 449 | * When no query string is set (null) the method returns false. |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | 33 | public function getRawUserQueryIsEmptyString() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * This method returns true when no querystring is present at all. |
||
| 469 | * Which means no search by the user was triggered |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | 33 | public function getRawUserQueryIsNull() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Sets the results per page that are used during search. |
||
| 481 | * |
||
| 482 | * @param int $resultsPerPage |
||
| 483 | * @return SearchRequest |
||
| 484 | */ |
||
| 485 | 1 | public function setResultsPerPage($resultsPerPage) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | 1 | public function getStateChanged() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Returns the passed resultsPerPage value |
||
| 504 | * @return int|null |
||
| 505 | */ |
||
| 506 | 30 | public function getResultsPerPage() |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @return int |
||
| 514 | */ |
||
| 515 | 1 | public function getContextSystemLanguageUid() |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | 2 | public function getContextPageUid() |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Get contextTypoScriptConfiguration |
||
| 530 | * |
||
| 531 | * @return TypoScriptConfiguration |
||
| 532 | */ |
||
| 533 | 2 | public function getContextTypoScriptConfiguration() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Assigns the last known persistedArguments and restores their state. |
||
| 540 | * |
||
| 541 | * @return SearchRequest |
||
| 542 | */ |
||
| 543 | 56 | public function reset() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * This can be used to start a new sub request, e.g. for a faceted search. |
||
| 552 | * |
||
| 553 | * @param bool $onlyPersistentArguments |
||
| 554 | * @return SearchRequest |
||
| 555 | */ |
||
| 556 | 2 | public function getCopyForSubRequest($onlyPersistentArguments = true) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return array |
||
| 575 | */ |
||
| 576 | 8 | public function getAsArray() |
|
| 580 | } |
||
| 581 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: