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 | */ |
||
106 | 91 | public function __construct(array $argumentsArray = [], $pageUid = 0, $sysLanguageUid = 0, TypoScriptConfiguration $typoScriptConfiguration = null) |
|
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | 32 | 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 | */ |
||
138 | 1 | 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 | */ |
||
156 | 73 | protected function prefixWithNamespace($path) |
|
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | 28 | public function getActiveFacetNames() |
|
175 | |||
176 | /** |
||
177 | * Returns all facet values for a certain facetName |
||
178 | * @param string $facetName |
||
179 | * @return array |
||
180 | */ |
||
181 | 40 | public function getActiveFacetValuesByName($facetName) |
|
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | 47 | public function getActiveFacets() |
|
206 | |||
207 | /** |
||
208 | * @return int |
||
209 | */ |
||
210 | 2 | public function getActiveFacetCount() |
|
214 | |||
215 | /** |
||
216 | * @param $activeFacets |
||
217 | * |
||
218 | * @return SearchRequest |
||
219 | */ |
||
220 | 41 | 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 | */ |
||
236 | 36 | 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 | */ |
||
258 | 6 | 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 | */ |
||
285 | 3 | public function removeAllFacetValuesByName($facetName) |
|
297 | |||
298 | /** |
||
299 | * Removes all active facets from the request. |
||
300 | * |
||
301 | * @return SearchRequest |
||
302 | */ |
||
303 | 6 | public function removeAllFacets() |
|
310 | |||
311 | /** |
||
312 | * @param string $facetName |
||
313 | * @param mixed $facetValue |
||
314 | * @return bool |
||
315 | */ |
||
316 | 40 | public function getHasFacetValue($facetName, $facetValue) |
|
321 | |||
322 | /** |
||
323 | * @return bool |
||
324 | */ |
||
325 | 44 | public function getHasSorting() |
|
330 | |||
331 | /** |
||
332 | * Returns the sorting string in the url e.g. title asc. |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | 43 | 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 | */ |
||
348 | 43 | protected function getSortingPart($index) |
|
358 | |||
359 | /** |
||
360 | * Returns the sorting configuration name that is currently used. |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | 43 | public function getSortingName() |
|
368 | |||
369 | /** |
||
370 | * Returns the sorting direction that is currently used. |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | 42 | public function getSortingDirection() |
|
378 | |||
379 | /** |
||
380 | * @return SearchRequest |
||
381 | */ |
||
382 | 30 | public function removeSorting() |
|
389 | |||
390 | /** |
||
391 | * @param string $sortingName |
||
392 | * @param string $direction (asc or desc) |
||
393 | * |
||
394 | * @return SearchRequest |
||
395 | */ |
||
396 | 31 | 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 | */ |
||
411 | 14 | public function setPage($page) |
|
418 | |||
419 | /** |
||
420 | * Returns the passed page. |
||
421 | * |
||
422 | * @return int|null |
||
423 | */ |
||
424 | 45 | public function getPage() |
|
429 | |||
430 | /** |
||
431 | * Can be used to reset all groupPages. |
||
432 | * |
||
433 | * @return SearchRequest |
||
434 | */ |
||
435 | public function removeAllGroupItemPages(): SearchRequest |
||
442 | 1 | ||
443 | 1 | /** |
|
444 | * Can be used to paginate within a groupItem. |
||
445 | * |
||
446 | * @param string $groupName e.g. type |
||
447 | * @param string $groupItemValue e.g. pages |
||
448 | * @param int $page |
||
449 | * @return SearchRequest |
||
450 | */ |
||
451 | public function setGroupItemPage(string $groupName, string $groupItemValue, int $page): SearchRequest |
||
458 | |||
459 | /** |
||
460 | * Retrieves the current page for this group item. |
||
461 | * |
||
462 | * @param string $groupName |
||
463 | * @param string $groupItemValue |
||
464 | * @return int |
||
465 | */ |
||
466 | public function getGroupItemPage(string $groupName, string $groupItemValue): int |
||
471 | |||
472 | /** |
||
473 | * Retrieves the highest page of the groups. |
||
474 | * |
||
475 | * @return int |
||
476 | */ |
||
477 | public function getHighestGroupPage() |
||
493 | |||
494 | /** |
||
495 | * Method to overwrite the query string. |
||
496 | * |
||
497 | * @param string $rawQueryString |
||
498 | * @return SearchRequest |
||
499 | */ |
||
500 | 45 | public function setRawQueryString($rawQueryString) |
|
507 | |||
508 | /** |
||
509 | * Returns the passed rawQueryString. |
||
510 | * |
||
511 | * @return string|null |
||
512 | */ |
||
513 | public function getRawUserQuery() |
||
519 | 42 | ||
520 | 4 | /** |
|
521 | * Method to check if the query string is an empty string |
||
522 | * (also empty string or whitespaces only are handled as empty). |
||
523 | 38 | * |
|
524 | * When no query string is set (null) the method returns false. |
||
525 | * @return bool |
||
526 | */ |
||
527 | 38 | public function getRawUserQueryIsEmptyString() |
|
542 | |||
543 | /** |
||
544 | * This method returns true when no querystring is present at all. |
||
545 | * Which means no search by the user was triggered |
||
546 | * |
||
547 | * @return bool |
||
548 | */ |
||
549 | 47 | public function getRawUserQueryIsNull() |
|
555 | 47 | ||
556 | /** |
||
557 | * Sets the results per page that are used during search. |
||
558 | * |
||
559 | * @param int $resultsPerPage |
||
560 | * @return SearchRequest |
||
561 | 1 | */ |
|
562 | public function setResultsPerPage($resultsPerPage) |
||
570 | 46 | ||
571 | /** |
||
572 | 46 | * @return bool |
|
573 | 46 | */ |
|
574 | public function getStateChanged() |
||
578 | |||
579 | 33 | /** |
|
580 | * Returns the passed resultsPerPage value |
||
581 | 33 | * @return int|null |
|
582 | */ |
||
583 | public function getResultsPerPage() |
||
588 | |||
589 | 33 | /** |
|
590 | * @return int |
||
591 | */ |
||
592 | public function getContextSystemLanguageUid() |
||
596 | |||
597 | 52 | /** |
|
598 | * @return int |
||
599 | 52 | */ |
|
600 | public function getContextPageUid() |
||
604 | |||
605 | /** |
||
606 | * Get contextTypoScriptConfiguration |
||
607 | 89 | * |
|
608 | * @return TypoScriptConfiguration |
||
609 | 89 | */ |
|
610 | 89 | public function getContextTypoScriptConfiguration() |
|
614 | |||
615 | /** |
||
616 | * Assigns the last known persistedArguments and restores their state. |
||
617 | * |
||
618 | * @return SearchRequest |
||
619 | */ |
||
620 | 38 | public function reset() |
|
626 | |||
627 | /** |
||
628 | * This can be used to start a new sub request, e.g. for a faceted search. |
||
629 | * |
||
630 | * @param bool $onlyPersistentArguments |
||
631 | * @return SearchRequest |
||
632 | */ |
||
633 | 38 | public function getCopyForSubRequest($onlyPersistentArguments = true) |
|
660 | |||
661 | 47 | /** |
|
662 | * @return string |
||
663 | */ |
||
664 | public function getArgumentNameSpace() |
||
668 | |||
669 | 36 | /** |
|
670 | 36 | * @return array |
|
671 | */ |
||
672 | public function getAsArray() |
||
676 | |||
677 | /** |
||
678 | * Returns only the arguments as array. |
||
679 | * |
||
680 | * @return array |
||
681 | */ |
||
682 | public function getArguments() { |
||
685 | } |
||
686 |
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.