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 | * Default namespace overwritten with the configured plugin namespace. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $argumentNameSpace = 'tx_solr'; |
||
50 | |||
51 | /** |
||
52 | * Arguments that should be kept for sub requests. |
||
53 | * |
||
54 | * Default values, overwritten in the constructor with the namespaced arguments |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $persistentArgumentsPaths = ['tx_solr:q', 'tx_solr:filter', 'tx_solr:sort']; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $stateChanged = false; |
||
64 | |||
65 | /** |
||
66 | * @var ArrayAccessor |
||
67 | */ |
||
68 | protected $argumentsAccessor; |
||
69 | |||
70 | /** |
||
71 | * The sys_language_uid that was used in the context where the request was build. |
||
72 | * This could be different from the "L" parameter and and not relevant for urls, |
||
73 | * because typolink itself will handle it. |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $contextSystemLanguageUid; |
||
78 | |||
79 | /** |
||
80 | * The page_uid that was used in the context where the request was build. |
||
81 | * |
||
82 | * The pageUid is not relevant for the typolink additionalArguments and therefore |
||
83 | * a separate property. |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $contextPageUid; |
||
88 | |||
89 | /** |
||
90 | * @var TypoScriptConfiguration |
||
91 | */ |
||
92 | protected $contextTypoScriptConfiguration; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $persistedArguments = []; |
||
98 | |||
99 | /** |
||
100 | * @param array $argumentsArray |
||
101 | * @param int $pageUid |
||
102 | * @param int $sysLanguageUid |
||
103 | * @param TypoScriptConfiguration $typoScriptConfiguration |
||
104 | */ |
||
105 | 83 | public function __construct(array $argumentsArray = [], $pageUid = 0, $sysLanguageUid = 0, TypoScriptConfiguration $typoScriptConfiguration = null) |
|
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | 27 | public function getId() |
|
130 | |||
131 | /** |
||
132 | * Can be used do merge arguments into the request arguments |
||
133 | * |
||
134 | * @param array $argumentsToMerge |
||
135 | * @return SearchRequest |
||
136 | */ |
||
137 | 1 | public function mergeArguments(array $argumentsToMerge) |
|
148 | |||
149 | /** |
||
150 | * Helper method to prefix an accessor with the arguments namespace. |
||
151 | * |
||
152 | * @param string $path |
||
153 | * @return string |
||
154 | */ |
||
155 | 65 | protected function prefixWithNamespace($path) |
|
159 | |||
160 | /** |
||
161 | * @return array |
||
162 | */ |
||
163 | 24 | public function getActiveFacetNames() |
|
174 | |||
175 | /** |
||
176 | * Returns all facet values for a certain facetName |
||
177 | * @param string $facetName |
||
178 | * @return array |
||
179 | */ |
||
180 | 35 | public function getActiveFacetValuesByName($facetName) |
|
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | 42 | public function getActiveFacets() |
|
205 | |||
206 | /** |
||
207 | * @return int |
||
208 | */ |
||
209 | 2 | public function getActiveFacetCount() |
|
213 | |||
214 | /** |
||
215 | * @param $activeFacets |
||
216 | * |
||
217 | * @return SearchRequest |
||
218 | */ |
||
219 | 36 | protected function setActiveFacets($activeFacets = []) |
|
226 | |||
227 | /** |
||
228 | * Adds a facet value to the request. |
||
229 | * |
||
230 | * @param string $facetName |
||
231 | * @param mixed $facetValue |
||
232 | * |
||
233 | * @return SearchRequest |
||
234 | */ |
||
235 | 31 | public function addFacetValue($facetName, $facetValue) |
|
248 | |||
249 | /** |
||
250 | * Removes a facet value from the request. |
||
251 | * |
||
252 | * @param string $facetName |
||
253 | * @param mixed $facetValue |
||
254 | * |
||
255 | * @return SearchRequest |
||
256 | */ |
||
257 | 4 | public function removeFacetValue($facetName, $facetValue) |
|
276 | |||
277 | /** |
||
278 | * Removes all facet values from the request by a certain facet name |
||
279 | * |
||
280 | * @param string $facetName |
||
281 | * |
||
282 | * @return SearchRequest |
||
283 | */ |
||
284 | 3 | public function removeAllFacetValuesByName($facetName) |
|
296 | |||
297 | /** |
||
298 | * Removes all active facets from the request. |
||
299 | * |
||
300 | * @return SearchRequest |
||
301 | */ |
||
302 | 4 | public function removeAllFacets() |
|
309 | |||
310 | /** |
||
311 | * @param string $facetName |
||
312 | * @param mixed $facetValue |
||
313 | * @return bool |
||
314 | */ |
||
315 | 35 | public function getHasFacetValue($facetName, $facetValue) |
|
320 | |||
321 | /** |
||
322 | * @return bool |
||
323 | */ |
||
324 | 40 | public function getHasSorting() |
|
329 | |||
330 | /** |
||
331 | * Returns the sorting string in the url e.g. title asc. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | 39 | public function getSorting() |
|
340 | |||
341 | /** |
||
342 | * Helper function to get the sorting configuration name or direction. |
||
343 | * |
||
344 | * @param int $index |
||
345 | * @return string |
||
346 | */ |
||
347 | 39 | protected function getSortingPart($index) |
|
357 | |||
358 | /** |
||
359 | * Returns the sorting configuration name that is currently used. |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 39 | public function getSortingName() |
|
367 | |||
368 | /** |
||
369 | * Returns the sorting direction that is currently used. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 38 | public function getSortingDirection() |
|
377 | |||
378 | /** |
||
379 | * @return SearchRequest |
||
380 | */ |
||
381 | 27 | public function removeSorting() |
|
388 | |||
389 | /** |
||
390 | * @param string $sortingName |
||
391 | * @param string $direction (asc or desc) |
||
392 | * |
||
393 | * @return SearchRequest |
||
394 | */ |
||
395 | 28 | public function setSorting($sortingName, $direction = 'asc') |
|
403 | |||
404 | /** |
||
405 | * Method to set the paginated page of the search |
||
406 | * |
||
407 | * @param int $page |
||
408 | * @return SearchRequest |
||
409 | */ |
||
410 | 10 | public function setPage($page) |
|
417 | |||
418 | /** |
||
419 | * Returns the passed page. |
||
420 | * |
||
421 | * @return int|null |
||
422 | */ |
||
423 | 39 | public function getPage() |
|
428 | |||
429 | /** |
||
430 | * Method to overwrite the query string. |
||
431 | * |
||
432 | * @param string $rawQueryString |
||
433 | * @return SearchRequest |
||
434 | */ |
||
435 | 37 | public function setRawQueryString($rawQueryString) |
|
442 | |||
443 | /** |
||
444 | * Returns the passed rawQueryString. |
||
445 | * |
||
446 | * @return string|null |
||
447 | */ |
||
448 | 40 | public function getRawUserQuery() |
|
454 | |||
455 | /** |
||
456 | * Method to check if the query string is an empty string |
||
457 | * (also empty string or whitespaces only are handled as empty). |
||
458 | * |
||
459 | * When no query string is set (null) the method returns false. |
||
460 | * @return bool |
||
461 | */ |
||
462 | 37 | public function getRawUserQueryIsEmptyString() |
|
477 | |||
478 | /** |
||
479 | * This method returns true when no querystring is present at all. |
||
480 | * Which means no search by the user was triggered |
||
481 | * |
||
482 | * @return bool |
||
483 | */ |
||
484 | 39 | public function getRawUserQueryIsNull() |
|
490 | |||
491 | /** |
||
492 | * Sets the results per page that are used during search. |
||
493 | * |
||
494 | * @param int $resultsPerPage |
||
495 | * @return SearchRequest |
||
496 | */ |
||
497 | 1 | public function setResultsPerPage($resultsPerPage) |
|
505 | |||
506 | /** |
||
507 | * @return bool |
||
508 | */ |
||
509 | 1 | public function getStateChanged() |
|
513 | |||
514 | /** |
||
515 | * Returns the passed resultsPerPage value |
||
516 | * @return int|null |
||
517 | */ |
||
518 | 37 | public function getResultsPerPage() |
|
523 | |||
524 | /** |
||
525 | * @return int |
||
526 | */ |
||
527 | 29 | public function getContextSystemLanguageUid() |
|
531 | |||
532 | /** |
||
533 | * @return int |
||
534 | */ |
||
535 | 29 | public function getContextPageUid() |
|
539 | |||
540 | /** |
||
541 | * Get contextTypoScriptConfiguration |
||
542 | * |
||
543 | * @return TypoScriptConfiguration |
||
544 | */ |
||
545 | 47 | public function getContextTypoScriptConfiguration() |
|
549 | |||
550 | /** |
||
551 | * Assigns the last known persistedArguments and restores their state. |
||
552 | * |
||
553 | * @return SearchRequest |
||
554 | */ |
||
555 | 81 | public function reset() |
|
561 | |||
562 | /** |
||
563 | * This can be used to start a new sub request, e.g. for a faceted search. |
||
564 | * |
||
565 | * @param bool $onlyPersistentArguments |
||
566 | * @return SearchRequest |
||
567 | */ |
||
568 | 34 | public function getCopyForSubRequest($onlyPersistentArguments = true) |
|
595 | |||
596 | /** |
||
597 | * @return string |
||
598 | */ |
||
599 | 21 | public function getArgumentNameSpace() |
|
603 | |||
604 | /** |
||
605 | * @return array |
||
606 | */ |
||
607 | 43 | public function getAsArray() |
|
611 | |||
612 | /** |
||
613 | * Returns only the arguments as array. |
||
614 | * |
||
615 | * @return array |
||
616 | */ |
||
617 | 30 | public function getArguments() { |
|
620 | } |
||
621 |