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 SORT_RELEVANCE = 'relevance'; |
||
26 | |||
27 | const SORT_CREATED_ON = 'created_on'; |
||
28 | |||
29 | const SORT_RANDOM = 'random'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $query = ''; |
||
35 | |||
36 | /** |
||
37 | * @var PredicateBuilder |
||
38 | */ |
||
39 | private $conditionBuilder; |
||
40 | |||
41 | /** |
||
42 | * @var int[] An array of collection ID's to search |
||
43 | */ |
||
44 | private $collections = array(); |
||
45 | |||
46 | /** |
||
47 | * @var int Offset of the first record to return |
||
48 | */ |
||
49 | private $offsetStart = 0; |
||
50 | |||
51 | /** |
||
52 | * @var int Number of records to return |
||
53 | */ |
||
54 | private $recordsPerPage = 10; |
||
55 | |||
56 | /** |
||
57 | * @var string|null One of the RECORD_TYPE_* constant values to restrict the search to a specific document type |
||
58 | */ |
||
59 | private $recordType = null; |
||
60 | |||
61 | /** |
||
62 | * @var int One of the SEARCH_TYPE_* constant values to select the type of record to fetch |
||
63 | */ |
||
64 | private $searchType = self::SEARCH_RECORDS; |
||
65 | |||
66 | /** |
||
67 | * @var string|null Name of a date field to use as a date criterion |
||
68 | */ |
||
69 | private $dateCriterionField = null; |
||
70 | |||
71 | /** |
||
72 | * @var \DateTimeInterface|null |
||
73 | */ |
||
74 | private $dateCriterionMin = null; |
||
75 | |||
76 | /** |
||
77 | * @var \DateTimeInterface|null |
||
78 | */ |
||
79 | private $dateCriterionMax = null; |
||
80 | |||
81 | /** |
||
82 | * @var int[] An array of statuses to restrict the search to documents matching the given statuses |
||
83 | */ |
||
84 | private $statuses = array(); |
||
85 | |||
86 | /** |
||
87 | * @var string[] An array of fields names to return in the search results |
||
88 | */ |
||
89 | private $fields = array(); |
||
90 | |||
91 | /** |
||
92 | * @var bool Whether to sort in descending order |
||
93 | */ |
||
94 | private $sortDescending = false; |
||
95 | |||
96 | /** |
||
97 | * @var string Type of sort to use |
||
98 | */ |
||
99 | private $sortType = null; |
||
100 | |||
101 | /** |
||
102 | * Sets the record search query string. Format follows the same specification as the Phraseanet search |
||
103 | * engine. |
||
104 | * |
||
105 | * @param $query |
||
106 | * @return $this |
||
107 | */ |
||
108 | 1 | public function setQuery($query) |
|
114 | |||
115 | /** |
||
116 | * @return PredicateBuilder |
||
117 | */ |
||
118 | public function getConditionBuilder() |
||
119 | { |
||
120 | if ($this->conditionBuilder === null) { |
||
121 | $this->conditionBuilder = new PredicateBuilder(); |
||
122 | } |
||
123 | |||
124 | return $this->conditionBuilder; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Add a collection to the search criteria. |
||
129 | * |
||
130 | * @param DataboxCollection|int $collection A collection or collection ID. |
||
131 | * @return $this |
||
132 | */ |
||
133 | 5 | public function addCollection($collection) |
|
134 | { |
||
135 | 5 | if ($collection instanceof DataboxCollection) { |
|
136 | 1 | $collection = $collection->getBaseId(); |
|
137 | } |
||
138 | |||
139 | 5 | $this->collections[] = $collection; |
|
140 | |||
141 | 5 | return $this; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Adds a list of collections to the search criteria. |
||
146 | * |
||
147 | * @param array $collections An array of DataboxCollection instances or collection ID's. |
||
148 | * @return $this |
||
149 | */ |
||
150 | 3 | public function addCollections(array $collections) |
|
151 | { |
||
152 | 3 | foreach ($collections as $collection) { |
|
153 | 3 | $this->addCollection($collection); |
|
154 | } |
||
155 | |||
156 | 3 | return $this; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Sets the list of collections in which to search for records. |
||
161 | * |
||
162 | * @param array $collections An array of DataboxCollection instances or collection ID's. An empty array will clear |
||
163 | * the collection restriction. |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | 1 | public function setCollections(array $collections) |
|
175 | |||
176 | /** |
||
177 | * Intersects the current list of collections with the given collections. |
||
178 | * |
||
179 | * @param array $collections |
||
180 | */ |
||
181 | 1 | public function intersectCollections(array $collections) |
|
185 | |||
186 | /** |
||
187 | * Sets the offset of the first record to return. |
||
188 | * |
||
189 | * @param int $offset Index of the first record to return. Defaults to 0 if an invalid value is provided. |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | 1 | public function setOffset($offset) |
|
199 | |||
200 | /** |
||
201 | * @param int $pageIndex The 0-based page index |
||
202 | */ |
||
203 | public function setPage($pageIndex) |
||
207 | |||
208 | /** |
||
209 | * Sets the sort type for the query |
||
210 | * |
||
211 | * @param string $sort One of the SORT_* constant values. |
||
212 | * @param bool $descending True to sort in descending order, false otherwise. |
||
213 | * @return $this |
||
214 | * @throws \InvalidArgumentException when the sort type is not of the SORT_* constant values.. |
||
215 | */ |
||
216 | 3 | public function sortBy($sort, $descending = false) |
|
223 | |||
224 | /** |
||
225 | * Sets the maximum number of records to return. |
||
226 | * |
||
227 | * @param int $limit Maximum number of records to return. Defaults to 1 if an invalid value is provided. |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | 1 | public function setLimit($limit) |
|
237 | |||
238 | /** |
||
239 | * Filters the media type of records to return. |
||
240 | * |
||
241 | * @param string $recordType One of the QueryBuilder::RECORD_TYPE_* constant values. |
||
242 | * @return $this |
||
243 | * @throws \InvalidArgumentException when the record media type is not valid. |
||
244 | */ |
||
245 | 6 | public function setRecordType($recordType) |
|
265 | |||
266 | /** |
||
267 | * Sets the type of record to search for. |
||
268 | * |
||
269 | * @param int $searchType One of the QueryBuilder::SEARCH_* constant values. |
||
270 | * @return $this |
||
271 | * @throws \InvalidArgumentException when the the search type if not valid. |
||
272 | */ |
||
273 | 2 | public function setSearchType($searchType) |
|
288 | |||
289 | /** |
||
290 | * Sets a date filter on the records to return. At least one of $minDate or $maxDate arguments |
||
291 | * must be specified. |
||
292 | * |
||
293 | * @param string $fieldName The name of the field on which to filter by date. |
||
294 | * @param \DateTimeInterface $minDate The lower date boundary. |
||
295 | * @param \DateTimeInterface $maxDate The upper date boundary. |
||
296 | * @return $this |
||
297 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
||
298 | * @throws \InvalidArgumentException when both min and max date values are null. |
||
299 | */ |
||
300 | 10 | public function setDateCriterion($fieldName, \DateTimeInterface $minDate = null, \DateTimeInterface $maxDate = null) |
|
314 | |||
315 | /** |
||
316 | * Adds a status filter to the search |
||
317 | * |
||
318 | * @param mixed $status |
||
319 | * @param $value |
||
320 | * @return $this |
||
321 | */ |
||
322 | 3 | public function addStatus($status, $value) |
|
328 | |||
329 | /** |
||
330 | * Adds a list of status filters to the search. |
||
331 | * |
||
332 | * @param array $statuses |
||
333 | * @return $this |
||
334 | */ |
||
335 | 2 | public function addStatuses(array $statuses) |
|
343 | |||
344 | /** |
||
345 | * Sets the status filter to the given list. An empty list clears the filter. |
||
346 | * |
||
347 | * @param array $statuses |
||
348 | * @return $this |
||
349 | */ |
||
350 | 1 | public function setStatuses(array $statuses) |
|
358 | |||
359 | /** |
||
360 | * Adds a field to the list of requested fields. |
||
361 | * |
||
362 | * @param string $fieldName |
||
363 | * @return $this |
||
364 | * @throws \InvalidArgumentException when the field name is an invalid or empty string |
||
365 | */ |
||
366 | 21 | public function addField($fieldName) |
|
373 | |||
374 | 31 | private function validateFieldName($fieldName, $errorMessage) |
|
380 | |||
381 | /** |
||
382 | * Adds a list of fields to the list of requested fields. |
||
383 | * |
||
384 | * @param array $fields |
||
385 | * @return $this |
||
386 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
387 | */ |
||
388 | 14 | public function addFields(array $fields) |
|
396 | |||
397 | /** |
||
398 | * Sets the list of requested fields. An empty clears the filter and all fields will be returned. |
||
399 | * |
||
400 | * @param array $fields |
||
401 | * @return $this |
||
402 | * @throws \InvalidArgumentException when one of the field names is an invalid or empty string |
||
403 | */ |
||
404 | 7 | public function setFields(array $fields) |
|
412 | |||
413 | /** |
||
414 | * Returns the built query. |
||
415 | * |
||
416 | * @return RecordQuery |
||
417 | */ |
||
418 | 27 | public function getQuery() |
|
436 | |||
437 | 27 | private function buildQueryTerm() |
|
450 | |||
451 | /** |
||
452 | * @param $query |
||
453 | * @return mixed |
||
454 | */ |
||
455 | 27 | private function appendRecordType($query) |
|
465 | |||
466 | /** |
||
467 | * @param $query |
||
468 | * @return mixed |
||
469 | */ |
||
470 | 27 | private function appendDates($query) |
|
490 | |||
491 | /** |
||
492 | * @param $query |
||
493 | * @return mixed |
||
494 | */ |
||
495 | 27 | private function appendStatuses($query) |
|
505 | |||
506 | /** |
||
507 | * @param $query |
||
508 | * @return mixed |
||
509 | */ |
||
510 | 27 | private function appendFields($query) |
|
520 | |||
521 | /** |
||
522 | * @param $query |
||
523 | * @return mixed |
||
524 | */ |
||
525 | 27 | private function appendSort($query) |
|
536 | } |
||
537 |