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 | * Sets the record search query string. Format follows the same specification as the Phraseanet search |
||
105 | * engine. |
||
106 | * |
||
107 | * @param $query |
||
108 | * @return $this |
||
109 | */ |
||
110 | 1 | public function setQuery($query) |
|
116 | |||
117 | /** |
||
118 | * @return PredicateBuilder |
||
119 | */ |
||
120 | public function getConditionBuilder() |
||
128 | |||
129 | /** |
||
130 | * Add a collection to the search criteria. |
||
131 | * |
||
132 | * @param DataboxCollection|int $collection A collection or collection ID. |
||
133 | * @return $this |
||
134 | */ |
||
135 | 5 | public function addCollection($collection) |
|
145 | |||
146 | /** |
||
147 | * Adds a list of collections to the search criteria. |
||
148 | * |
||
149 | * @param array $collections An array of DataboxCollection instances or collection ID's. |
||
150 | * @return $this |
||
151 | */ |
||
152 | 3 | public function addCollections(array $collections) |
|
160 | |||
161 | /** |
||
162 | * Sets the list of collections in which to search for records. |
||
163 | * |
||
164 | * @param array $collections An array of DataboxCollection instances or collection ID's. An empty array will clear |
||
165 | * the collection restriction. |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | 1 | public function setCollections(array $collections) |
|
177 | |||
178 | /** |
||
179 | * Intersects the current list of collections with the given collections. |
||
180 | * |
||
181 | * @param array $collections |
||
182 | */ |
||
183 | 1 | public function intersectCollections(array $collections) |
|
187 | |||
188 | /** |
||
189 | * Sets the offset of the first record to return. |
||
190 | * |
||
191 | * @param int $offset Index of the first record to return. Defaults to 0 if an invalid value is provided. |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | 1 | public function setOffset($offset) |
|
201 | |||
202 | /** |
||
203 | * @param int $pageIndex The 0-based page index |
||
204 | */ |
||
205 | public function setPage($pageIndex) |
||
209 | |||
210 | /** |
||
211 | * Sets the sort type for the query |
||
212 | * |
||
213 | * @param string $sort One of the SORT_* constant values. |
||
214 | * @param bool $descending True to sort in descending order, false otherwise. |
||
215 | * @return $this |
||
216 | * @throws \InvalidArgumentException when the sort type is not of the SORT_* constant values.. |
||
217 | */ |
||
218 | 3 | public function sortBy($sort, $descending = false) |
|
225 | |||
226 | /** |
||
227 | * Sets the maximum number of records to return. |
||
228 | * |
||
229 | * @param int $limit Maximum number of records to return. Defaults to 1 if an invalid value is provided. |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | 1 | public function setLimit($limit) |
|
239 | |||
240 | /** |
||
241 | * Filters the media type of records to return. |
||
242 | * |
||
243 | * @param string $recordType One of the QueryBuilder::RECORD_TYPE_* constant values. |
||
244 | * @return $this |
||
245 | * @throws \InvalidArgumentException when the record media type is not valid. |
||
246 | */ |
||
247 | 6 | public function setRecordType($recordType) |
|
267 | |||
268 | /** |
||
269 | * Sets the type of record to search for. |
||
270 | * |
||
271 | * @param int $searchType One of the QueryBuilder::SEARCH_* constant values. |
||
272 | * @return $this |
||
273 | * @throws \InvalidArgumentException when the the search type if not valid. |
||
274 | */ |
||
275 | 2 | public function setSearchType($searchType) |
|
291 | |||
292 | /** |
||
293 | * Sets a date filter on the records to return. At least one of $minDate or $maxDate arguments |
||
294 | * must be specified. |
||
295 | * |
||
296 | * @param string $fieldName The name of the field on which to filter by date. |
||
297 | * @param \DateTimeInterface $minDate The lower date boundary. |
||
298 | * @param \DateTimeInterface $maxDate The upper date boundary. |
||
299 | * @return $this |
||
300 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
||
301 | * @throws \InvalidArgumentException when both min and max date values are null. |
||
302 | */ |
||
303 | 10 | public function setDateCriterion($fieldName, \DateTimeInterface $minDate = null, \DateTimeInterface $maxDate = null) |
|
317 | |||
318 | /** |
||
319 | * Adds a status filter to the search |
||
320 | * |
||
321 | * @param mixed $status |
||
322 | * @param $value |
||
323 | * @return $this |
||
324 | */ |
||
325 | 3 | public function addStatus($status, $value) |
|
331 | |||
332 | /** |
||
333 | * Adds a list of status filters to the search. |
||
334 | * |
||
335 | * @param array $statuses |
||
336 | * @return $this |
||
337 | */ |
||
338 | 2 | public function addStatuses(array $statuses) |
|
346 | |||
347 | /** |
||
348 | * Sets the status filter to the given list. An empty list clears the filter. |
||
349 | * |
||
350 | * @param array $statuses |
||
351 | * @return $this |
||
352 | */ |
||
353 | 1 | public function setStatuses(array $statuses) |
|
361 | |||
362 | /** |
||
363 | * Adds a field to the list of requested fields. |
||
364 | * |
||
365 | * @param string $fieldName |
||
366 | * @return $this |
||
367 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
||
368 | */ |
||
369 | 21 | public function addField($fieldName) |
|
376 | |||
377 | 31 | private function validateFieldName($fieldName, $errorMessage) |
|
383 | |||
384 | /** |
||
385 | * Adds a list of fields to the list of requested fields. |
||
386 | * |
||
387 | * @param array $fields |
||
388 | * @return $this |
||
389 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
390 | */ |
||
391 | 14 | public function addFields(array $fields) |
|
399 | |||
400 | /** |
||
401 | * Sets the list of requested fields. An empty clears the filter and all fields will be returned. |
||
402 | * |
||
403 | * @param array $fields |
||
404 | * @return $this |
||
405 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
406 | */ |
||
407 | 7 | public function setFields(array $fields) |
|
415 | |||
416 | /** |
||
417 | * Returns the built query. |
||
418 | * |
||
419 | * @return RecordQuery |
||
420 | */ |
||
421 | 27 | public function getQuery() |
|
439 | |||
440 | 27 | private function buildQueryTerm() |
|
453 | |||
454 | /** |
||
455 | * @param $query |
||
456 | * @return mixed |
||
457 | */ |
||
458 | 27 | private function appendRecordType($query) |
|
468 | |||
469 | /** |
||
470 | * @param $query |
||
471 | * @return mixed |
||
472 | */ |
||
473 | 27 | private function appendDates($query) |
|
493 | |||
494 | /** |
||
495 | * @param $query |
||
496 | * @return mixed |
||
497 | */ |
||
498 | 27 | private function appendStatuses($query) |
|
508 | |||
509 | /** |
||
510 | * @param $query |
||
511 | * @return mixed |
||
512 | */ |
||
513 | 27 | private function appendFields($query) |
|
523 | |||
524 | /** |
||
525 | * @param $query |
||
526 | * @return mixed |
||
527 | */ |
||
528 | 27 | private function appendSort($query) |
|
539 | } |
||
540 |