Complex classes like Match 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 Match, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Match |
||
10 | { |
||
11 | /** |
||
12 | * The last compiled query. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $last_compiled; |
||
17 | |||
18 | /** |
||
19 | * List of match operations. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $tokens = array(); |
||
24 | |||
25 | /** |
||
26 | * The owning SphinxQL object; used for escaping text. |
||
27 | * |
||
28 | * @var SphinxQL |
||
29 | */ |
||
30 | protected $sphinxql; |
||
31 | |||
32 | /** |
||
33 | * @param SphinxQL $sphinxql |
||
34 | */ |
||
35 | public function __construct(SphinxQL $sphinxql) |
||
39 | |||
40 | /** |
||
41 | * Match text or sub expression. |
||
42 | * |
||
43 | * Examples: |
||
44 | * $match->match('test'); |
||
45 | * // test |
||
46 | * |
||
47 | * $match->match('test case'); |
||
48 | * // (test case) |
||
49 | * |
||
50 | * $match->match(function ($m) { |
||
51 | * $m->match('a')->orMatch('b'); |
||
52 | * }); |
||
53 | * // (a | b) |
||
54 | * |
||
55 | * $sub = new Match($sphinxql); |
||
56 | * $sub->match('a')->orMatch('b'); |
||
57 | * $match->match($sub); |
||
58 | * // (a | b) |
||
59 | * |
||
60 | * @param string|Match|\Closure $keywords The text or expression to match. |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function match($keywords = null) |
||
71 | |||
72 | /** |
||
73 | * Provide an alternation match. |
||
74 | * |
||
75 | * Examples: |
||
76 | * $match->match('test')->orMatch(); |
||
77 | * // test | |
||
78 | * |
||
79 | * $match->match('test')->orMatch('case'); |
||
80 | * // test | case |
||
81 | * |
||
82 | * @param string|Match|\Closure $keywords The text or expression to alternatively match. |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function orMatch($keywords = null) |
||
92 | |||
93 | /** |
||
94 | * Provide an optional match. |
||
95 | * |
||
96 | * Examples: |
||
97 | * $match->match('test')->maybe(); |
||
98 | * // test MAYBE |
||
99 | * |
||
100 | * $match->match('test')->maybe('case'); |
||
101 | * // test MAYBE case |
||
102 | * |
||
103 | * @param string|Match|\Closure $keywords The text or expression to optionally match. |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function maybe($keywords = null) |
||
113 | |||
114 | /** |
||
115 | * Do not match a keyword. |
||
116 | * |
||
117 | * Examples: |
||
118 | * $match->not()->match('test'); |
||
119 | * // -test |
||
120 | * |
||
121 | * $match->not('test'); |
||
122 | * // -test |
||
123 | * |
||
124 | * @param string $keyword The word not to match. |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function not($keyword = null) |
||
134 | |||
135 | /** |
||
136 | * Specify which field(s) to search. |
||
137 | * |
||
138 | * Examples: |
||
139 | * $match->field('*')->match('test'); |
||
140 | * // @* test |
||
141 | * |
||
142 | * $match->field('title')->match('test'); |
||
143 | * // @title test |
||
144 | * |
||
145 | * $match->field('body', 50)->match('test'); |
||
146 | * // @body[50] test |
||
147 | * |
||
148 | * $match->field('title', 'body')->match('test'); |
||
149 | * // @(title,body) test |
||
150 | * |
||
151 | * $match->field(['title', 'body'])->match('test'); |
||
152 | * // @(title,body) test |
||
153 | * |
||
154 | * $match->field('@relaxed')->field('nosuchfield')->match('test'); |
||
155 | * // @@relaxed @nosuchfield test |
||
156 | * |
||
157 | * @param string|array $fields Field or fields to search. |
||
158 | * @param int $limit Maximum position limit in field a match is allowed at. |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function field($fields, $limit = null) |
||
178 | |||
179 | /** |
||
180 | * Specify which field(s) not to search. |
||
181 | * |
||
182 | * Examples: |
||
183 | * $match->ignoreField('title')->match('test'); |
||
184 | * // @!title test |
||
185 | * |
||
186 | * $match->ignoreField('title', 'body')->match('test'); |
||
187 | * // @!(title,body) test |
||
188 | * |
||
189 | * $match->ignoreField(['title', 'body'])->match('test'); |
||
190 | * // @!(title,body) test |
||
191 | * |
||
192 | * @param string|array $fields Field or fields to ignore during search. |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function ignoreField($fields) |
||
208 | |||
209 | /** |
||
210 | * Match an exact phrase. |
||
211 | * |
||
212 | * Example: |
||
213 | * $match->phrase('test case'); |
||
214 | * // "test case" |
||
215 | * |
||
216 | * @param string $keywords The phrase to match. |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function phrase($keywords) |
||
225 | |||
226 | /** |
||
227 | * Provide an optional phrase. |
||
228 | * |
||
229 | * Example: |
||
230 | * $match->phrase('test case')->orPhrase('another case'); |
||
231 | * // "test case" | "another case" |
||
232 | * |
||
233 | * @param string $keywords The phrase to match. |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function orPhrase($keywords) |
||
243 | |||
244 | /** |
||
245 | * Match if keywords are close enough. |
||
246 | * |
||
247 | * Example: |
||
248 | * $match->proximity('test case', 5); |
||
249 | * // "test case"~5 |
||
250 | * |
||
251 | * @param string $keywords The words to match. |
||
252 | * @param int $distance The upper limit on separation between words. |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function proximity($keywords, $distance) |
||
264 | |||
265 | /** |
||
266 | * Match if enough keywords are present. |
||
267 | * |
||
268 | * Examples: |
||
269 | * $match->quorum('this is a test case', 3); |
||
270 | * // "this is a test case"/3 |
||
271 | * |
||
272 | * $match->quorum('this is a test case', 0.5); |
||
273 | * // "this is a test case"/0.5 |
||
274 | * |
||
275 | * @param string $keywords The words to match. |
||
276 | * @param int|float $threshold The minimum number or percent of words that must match. |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function quorum($keywords, $threshold) |
||
288 | |||
289 | /** |
||
290 | * Assert keywords or expressions must be matched in order. |
||
291 | * |
||
292 | * Examples: |
||
293 | * $match->match('test')->before(); |
||
294 | * // test << |
||
295 | * |
||
296 | * $match->match('test')->before('case'); |
||
297 | * // test << case |
||
298 | * |
||
299 | * @param string|Match|\Closure $keywords The text or expression that must come after. |
||
300 | * |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function before($keywords = null) |
||
309 | |||
310 | /** |
||
311 | * Assert a keyword must be matched exactly as written. |
||
312 | * |
||
313 | * Examples: |
||
314 | * $match->match('test')->exact('cases'); |
||
315 | * // test =cases |
||
316 | * |
||
317 | * $match->match('test')->exact()->phrase('specific cases'); |
||
318 | * // test ="specific cases" |
||
319 | * |
||
320 | * @param string $keyword The word that must be matched exactly. |
||
321 | * |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function exact($keyword = null) |
||
330 | |||
331 | /** |
||
332 | * Boost the IDF score of a keyword. |
||
333 | * |
||
334 | * Examples: |
||
335 | * $match->match('test')->boost(1.2); |
||
336 | * // test^1.2 |
||
337 | * |
||
338 | * $match->match('test')->boost('case', 1.2); |
||
339 | * // test case^1.2 |
||
340 | * |
||
341 | * @param string $keyword The word to modify the score of. |
||
342 | * @param float $amount The amount to boost the score. |
||
343 | * |
||
344 | * @return $this |
||
345 | */ |
||
346 | public function boost($keyword, $amount = null) |
||
356 | |||
357 | /** |
||
358 | * Assert keywords or expressions must be matched close to each other. |
||
359 | * |
||
360 | * Examples: |
||
361 | * $match->match('test')->near(3); |
||
362 | * // test NEAR/3 |
||
363 | * |
||
364 | * $match->match('test')->near('case', 3); |
||
365 | * // test NEAR/3 case |
||
366 | * |
||
367 | * @param string|Match|\Closure $keywords The text or expression to match nearby. |
||
368 | * @param int $distance Maximum distance to the match. |
||
369 | * |
||
370 | * @return $this |
||
371 | */ |
||
372 | public function near($keywords, $distance = null) |
||
380 | |||
381 | /** |
||
382 | * Assert matches must be in the same sentence. |
||
383 | * |
||
384 | * Examples: |
||
385 | * $match->match('test')->sentence(); |
||
386 | * // test SENTENCE |
||
387 | * |
||
388 | * $match->match('test')->sentence('case'); |
||
389 | * // test SENTENCE case |
||
390 | * |
||
391 | * @param string|Match|\Closure $keywords The text or expression that must be in the sentence. |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function sentence($keywords = null) |
||
401 | |||
402 | /** |
||
403 | * Assert matches must be in the same paragraph. |
||
404 | * |
||
405 | * Examples: |
||
406 | * $match->match('test')->paragraph(); |
||
407 | * // test PARAGRAPH |
||
408 | * |
||
409 | * $match->match('test')->paragraph('case'); |
||
410 | * // test PARAGRAPH case |
||
411 | * |
||
412 | * @param string|Match|\Closure $keywords The text or expression that must be in the paragraph. |
||
413 | * |
||
414 | * @return $this |
||
415 | */ |
||
416 | public function paragraph($keywords = null) |
||
422 | |||
423 | /** |
||
424 | * Assert matches must be in the specified zone(s). |
||
425 | * |
||
426 | * Examples: |
||
427 | * $match->zone('th'); |
||
428 | * // ZONE:(th) |
||
429 | * |
||
430 | * $match->zone(['h3', 'h4']); |
||
431 | * // ZONE:(h3,h4) |
||
432 | * |
||
433 | * $match->zone('th', 'test'); |
||
434 | * // ZONE:(th) test |
||
435 | * |
||
436 | * @param string|array $zones The zone or zones to search. |
||
437 | * @param string|Match|\Closure $keywords The text or expression that must be in these zones. |
||
438 | * |
||
439 | * @return $this |
||
440 | */ |
||
441 | public function zone($zones, $keywords = null) |
||
450 | |||
451 | |||
452 | /** |
||
453 | * Assert matches must be in the same instance of the specified zone. |
||
454 | * |
||
455 | * Examples: |
||
456 | * $match->zonespan('th'); |
||
457 | * // ZONESPAN:(th) |
||
458 | * |
||
459 | * $match->zonespan('th', 'test'); |
||
460 | * // ZONESPAN:(th) test |
||
461 | * |
||
462 | * @param string $zone The zone to search. |
||
463 | * @param string|Match|\Closure $keywords The text or expression that must be in this zone. |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | public function zonespan($zone, $keywords = null) |
||
473 | |||
474 | /** |
||
475 | * Build the match expression. |
||
476 | * |
||
477 | * @return $this |
||
478 | */ |
||
479 | public function compile() |
||
531 | |||
532 | /** |
||
533 | * Returns the latest compiled match expression. |
||
534 | * |
||
535 | * @return string The last compiled match expression. |
||
536 | */ |
||
537 | public function getCompiled() |
||
541 | } |
||
542 |