Complex classes like RecordQueryBuilder 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 RecordQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class RecordQueryBuilder |
||
9 | { |
||
10 | |||
11 | const RECORD_TYPE_AUDIO = 'audio'; |
||
12 | |||
13 | const RECORD_TYPE_VIDEO = 'video'; |
||
14 | |||
15 | const RECORD_TYPE_IMAGE = 'image'; |
||
16 | |||
17 | const RECORD_TYPE_DOCUMENT = 'document'; |
||
18 | |||
19 | const RECORD_TYPE_FLASH = 'flash'; |
||
20 | |||
21 | const SEARCH_RECORDS = 0; |
||
22 | |||
23 | const SEARCH_STORIES = 1; |
||
24 | |||
25 | const SEARCH_STORIES_LIGHT = 2; |
||
26 | |||
27 | const SORT_RELEVANCE = 'relevance'; |
||
28 | |||
29 | const SORT_CREATED_ON = 'created_on'; |
||
30 | |||
31 | const SORT_RANDOM = 'random'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $query = ''; |
||
37 | |||
38 | /** |
||
39 | * @var PredicateBuilder |
||
40 | */ |
||
41 | private $conditionBuilder; |
||
42 | |||
43 | /** |
||
44 | * @var int[] An array of collection ID's to search |
||
45 | */ |
||
46 | private $collections = array(); |
||
47 | |||
48 | /** |
||
49 | * @var int Offset of the first record to return |
||
50 | */ |
||
51 | private $offsetStart = 0; |
||
52 | |||
53 | /** |
||
54 | * @var int Number of records to return |
||
55 | */ |
||
56 | private $recordsPerPage = 10; |
||
57 | |||
58 | /** |
||
59 | * @var string|null One of the RECORD_TYPE_* constant values to restrict the search to a specific document type |
||
60 | */ |
||
61 | private $recordType = null; |
||
62 | |||
63 | /** |
||
64 | * @var int One of the SEARCH_TYPE_* constant values to select the type of record to fetch |
||
65 | */ |
||
66 | private $searchType = self::SEARCH_RECORDS; |
||
67 | |||
68 | /** |
||
69 | * @var string|null Name of a date field to use as a date criterion |
||
70 | */ |
||
71 | private $dateCriterionField = null; |
||
72 | |||
73 | /** |
||
74 | * @var \DateTimeInterface|null |
||
75 | */ |
||
76 | private $dateCriterionMin = null; |
||
77 | |||
78 | /** |
||
79 | * @var \DateTimeInterface|null |
||
80 | */ |
||
81 | private $dateCriterionMax = null; |
||
82 | |||
83 | /** |
||
84 | * @var int[] An array of statuses to restrict the search to documents matching the given statuses |
||
85 | */ |
||
86 | private $statuses = array(); |
||
87 | |||
88 | /** |
||
89 | * @var string[] An array of fields names to return in the search results |
||
90 | */ |
||
91 | private $fields = array(); |
||
92 | |||
93 | /** |
||
94 | * @var bool Whether to sort in descending order |
||
95 | */ |
||
96 | private $sortDescending = false; |
||
97 | |||
98 | /** |
||
99 | * @var string Type of sort to use |
||
100 | */ |
||
101 | private $sortType = null; |
||
102 | |||
103 | /** |
||
104 | * @var bool Type of truncature |
||
105 | */ |
||
106 | private $truncature = true; |
||
107 | |||
108 | /** |
||
109 | * Sets the record search query string. Format follows the same specification as the Phraseanet search |
||
110 | 1 | * engine. |
|
111 | * |
||
112 | 1 | * @param $query |
|
113 | * @return $this |
||
114 | 1 | */ |
|
115 | public function setQuery($query) |
||
121 | |||
122 | /** |
||
123 | * @return PredicateBuilder |
||
124 | */ |
||
125 | public function getConditionBuilder() |
||
133 | |||
134 | /** |
||
135 | 5 | * Add a collection to the search criteria. |
|
136 | * |
||
137 | 5 | * @param DataboxCollection|int $collection A collection or collection ID. |
|
138 | 1 | * @return $this |
|
139 | */ |
||
140 | public function addCollection($collection) |
||
150 | |||
151 | /** |
||
152 | 3 | * Adds a list of collections to the search criteria. |
|
153 | * |
||
154 | 3 | * @param array $collections An array of DataboxCollection instances or collection ID's. |
|
155 | 3 | * @return $this |
|
156 | */ |
||
157 | public function addCollections(array $collections) |
||
165 | |||
166 | /** |
||
167 | * Sets the list of collections in which to search for records. |
||
168 | * |
||
169 | 1 | * @param array $collections An array of DataboxCollection instances or collection ID's. An empty array will clear |
|
170 | * the collection restriction. |
||
171 | 1 | * |
|
172 | * @return $this |
||
173 | 1 | */ |
|
174 | public function setCollections(array $collections) |
||
182 | |||
183 | 1 | /** |
|
184 | * Intersects the current list of collections with the given collections. |
||
185 | 1 | * |
|
186 | 1 | * @param array $collections |
|
187 | */ |
||
188 | public function intersectCollections(array $collections) |
||
192 | |||
193 | /** |
||
194 | * Sets the offset of the first record to return. |
||
195 | 1 | * |
|
196 | * @param int $offset Index of the first record to return. Defaults to 0 if an invalid value is provided. |
||
197 | 1 | * |
|
198 | * @return $this |
||
199 | 1 | */ |
|
200 | public function setOffset($offset) |
||
206 | |||
207 | /** |
||
208 | * @param int $pageIndex The 0-based page index |
||
209 | */ |
||
210 | public function setPage($pageIndex) |
||
214 | |||
215 | /** |
||
216 | * Sets the sort type for the query |
||
217 | * |
||
218 | 3 | * @param string $sort One of the SORT_* constant values. |
|
219 | * @param bool $descending True to sort in descending order, false otherwise. |
||
220 | 3 | * @return $this |
|
221 | 3 | * @throws \InvalidArgumentException when the sort type is not of the SORT_* constant values.. |
|
222 | */ |
||
223 | 3 | public function sortBy($sort, $descending = false) |
|
230 | |||
231 | /** |
||
232 | * Sets the maximum number of records to return. |
||
233 | 1 | * |
|
234 | * @param int $limit Maximum number of records to return. Defaults to 1 if an invalid value is provided. |
||
235 | 1 | * |
|
236 | * @return $this |
||
237 | 1 | */ |
|
238 | public function setLimit($limit) |
||
244 | |||
245 | /** |
||
246 | * Filters the media type of records to return. |
||
247 | 6 | * |
|
248 | * @param string $recordType One of the QueryBuilder::RECORD_TYPE_* constant values. |
||
249 | * @return $this |
||
250 | 6 | * @throws \InvalidArgumentException when the record media type is not valid. |
|
251 | 6 | */ |
|
252 | 6 | public function setRecordType($recordType) |
|
272 | |||
273 | /** |
||
274 | * Sets the type of record to search for. |
||
275 | 2 | * |
|
276 | * @param int $searchType One of the QueryBuilder::SEARCH_* constant values. |
||
277 | * @return $this |
||
278 | 2 | * @throws \InvalidArgumentException when the the search type if not valid. |
|
279 | 2 | */ |
|
280 | 2 | public function setSearchType($searchType) |
|
296 | |||
297 | /** |
||
298 | * Sets a date filter on the records to return. At least one of $minDate or $maxDate arguments |
||
299 | * must be specified. |
||
300 | * |
||
301 | * @param string $fieldName The name of the field on which to filter by date. |
||
302 | * @param \DateTimeInterface $minDate The lower date boundary. |
||
303 | 10 | * @param \DateTimeInterface $maxDate The upper date boundary. |
|
304 | * @return $this |
||
305 | 10 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
|
306 | * @throws \InvalidArgumentException when both min and max date values are null. |
||
307 | 4 | */ |
|
308 | 1 | public function setDateCriterion($fieldName, \DateTimeInterface $minDate = null, \DateTimeInterface $maxDate = null) |
|
322 | |||
323 | /** |
||
324 | * Adds a status filter to the search |
||
325 | 3 | * |
|
326 | * @param mixed $status |
||
327 | 3 | * @param $value |
|
328 | * @return $this |
||
329 | 3 | */ |
|
330 | public function addStatus($status, $value) |
||
336 | |||
337 | /** |
||
338 | 2 | * Adds a list of status filters to the search. |
|
339 | * |
||
340 | 2 | * @param array $statuses |
|
341 | 2 | * @return $this |
|
342 | */ |
||
343 | public function addStatuses(array $statuses) |
||
351 | |||
352 | /** |
||
353 | 1 | * Sets the status filter to the given list. An empty list clears the filter. |
|
354 | * |
||
355 | 1 | * @param array $statuses |
|
356 | * @return $this |
||
357 | 1 | */ |
|
358 | public function setStatuses(array $statuses) |
||
366 | |||
367 | /** |
||
368 | * Set truncature search type |
||
369 | 21 | * |
|
370 | * @param bool $truncature |
||
371 | 21 | * @return $this |
|
372 | 15 | */ |
|
373 | public function setTruncature(bool $truncature) |
||
378 | |||
379 | 31 | /** |
|
380 | 24 | * Adds a field to the list of requested fields. |
|
381 | * |
||
382 | 19 | * @param string $fieldName |
|
383 | * @return $this |
||
384 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
||
385 | */ |
||
386 | public function addField($fieldName) |
||
393 | 14 | ||
394 | 14 | private function validateFieldName($fieldName, $errorMessage) |
|
400 | |||
401 | /** |
||
402 | * Adds a list of fields to the list of requested fields. |
||
403 | * |
||
404 | * @param array $fields |
||
405 | * @return $this |
||
406 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
407 | 7 | */ |
|
408 | public function addFields(array $fields) |
||
416 | |||
417 | /** |
||
418 | * Sets the list of requested fields. An empty clears the filter and all fields will be returned. |
||
419 | * |
||
420 | * @param array $fields |
||
421 | 27 | * @return $this |
|
422 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
423 | */ |
||
424 | 27 | public function setFields(array $fields) |
|
432 | 27 | ||
433 | 27 | /** |
|
434 | 27 | * Returns the built query. |
|
435 | 27 | * |
|
436 | * @return RecordQuery |
||
437 | 27 | */ |
|
438 | public function getQuery() |
||
457 | |||
458 | 27 | private function buildQueryTerm() |
|
471 | |||
472 | /** |
||
473 | 27 | * @param $query |
|
474 | * @return mixed |
||
475 | 27 | */ |
|
476 | 3 | private function appendRecordType($query) |
|
486 | |||
487 | /** |
||
488 | 1 | * @param $query |
|
489 | * @return mixed |
||
490 | */ |
||
491 | 24 | private function appendDates($query) |
|
511 | |||
512 | /** |
||
513 | 27 | * @param $query |
|
514 | * @return mixed |
||
515 | 27 | */ |
|
516 | 3 | private function appendStatuses($query) |
|
526 | |||
527 | |||
528 | 27 | /** |
|
529 | * @param $query |
||
530 | 27 | * @return mixed |
|
531 | 3 | */ |
|
532 | 3 | private function appendTruncature($query) |
|
538 | |||
539 | /** |
||
540 | * @param $query |
||
541 | * @return mixed |
||
542 | */ |
||
543 | private function appendFields($query) |
||
553 | |||
554 | /** |
||
555 | * @param $query |
||
556 | * @return mixed |
||
557 | */ |
||
558 | private function appendSort($query) |
||
569 | } |
||
570 |