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 = null; |
||
| 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 = null; |
||
| 31 | |||
| 32 | public function __construct(SphinxQL $sphinxql) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param SphinxQL $sphinxql |
||
| 39 | * |
||
| 40 | * @return Match |
||
| 41 | */ |
||
| 42 | public static function create(SphinxQL $sphinxql) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Match text or sub expression. |
||
| 49 | * |
||
| 50 | * Examples: |
||
| 51 | * $match->match('test'); |
||
| 52 | * // test |
||
| 53 | * |
||
| 54 | * $match->match('test case'); |
||
| 55 | * // (test case) |
||
| 56 | * |
||
| 57 | * $match->match(function ($m) { |
||
| 58 | * $m->match('a')->orMatch('b'); |
||
| 59 | * }); |
||
| 60 | * // (a | b) |
||
| 61 | * |
||
| 62 | * $sub = new Match($sphinxql); |
||
| 63 | * $sub->match('a')->orMatch('b'); |
||
| 64 | * $match->match($sub); |
||
| 65 | * // (a | b) |
||
| 66 | * |
||
| 67 | * @param string|Match|Closure $keywords The text or expression to match. |
||
| 68 | */ |
||
| 69 | public function match($keywords = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Provide an alternation match. |
||
| 79 | * |
||
| 80 | * Examples: |
||
| 81 | * $match->match('test')->orMatch(); |
||
| 82 | * // test | |
||
| 83 | * |
||
| 84 | * $match->match('test')->orMatch('case'); |
||
| 85 | * // test | case |
||
| 86 | * |
||
| 87 | * @param string|Match|Closure $keywords The text or expression to alternatively match. |
||
| 88 | */ |
||
| 89 | public function orMatch($keywords = null) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Provide an optional match. |
||
| 98 | * |
||
| 99 | * Examples: |
||
| 100 | * $match->match('test')->maybe(); |
||
| 101 | * // test MAYBE |
||
| 102 | * |
||
| 103 | * $match->match('test')->maybe('case'); |
||
| 104 | * // test MAYBE case |
||
| 105 | * |
||
| 106 | * @param string|Match|Closure $keywords The text or expression to optionally match. |
||
| 107 | */ |
||
| 108 | public function maybe($keywords = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Do not match a keyword. |
||
| 117 | * |
||
| 118 | * Examples: |
||
| 119 | * $match->not()->match('test'); |
||
| 120 | * // -test |
||
| 121 | * |
||
| 122 | * $match->not('test'); |
||
| 123 | * // -test |
||
| 124 | * |
||
| 125 | * @param string $keyword The word not to match. |
||
| 126 | */ |
||
| 127 | public function not($keyword = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Specify which field(s) to search. |
||
| 136 | * |
||
| 137 | * Examples: |
||
| 138 | * $match->field('*')->match('test'); |
||
| 139 | * // @* test |
||
| 140 | * |
||
| 141 | * $match->field('title')->match('test'); |
||
| 142 | * // @title test |
||
| 143 | * |
||
| 144 | * $match->field('body', 50)->match('test'); |
||
| 145 | * // @body[50] test |
||
| 146 | * |
||
| 147 | * $match->field('title', 'body')->match('test'); |
||
| 148 | * // @(title,body) test |
||
| 149 | * |
||
| 150 | * $match->field(['title', 'body'])->match('test'); |
||
| 151 | * // @(title,body) test |
||
| 152 | * |
||
| 153 | * $match->field('@relaxed')->field('nosuchfield')->match('test'); |
||
| 154 | * // @@relaxed @nosuchfield test |
||
| 155 | * |
||
| 156 | * @param string|array $fields Field or fields to search. |
||
| 157 | * @param int $limit Maximum position limit in field a match is allowed at. |
||
| 158 | */ |
||
| 159 | public function field($fields, $limit = null) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Specify which field(s) not to search. |
||
| 178 | * |
||
| 179 | * Examples: |
||
| 180 | * $match->ignoreField('title')->match('test'); |
||
| 181 | * // @!title test |
||
| 182 | * |
||
| 183 | * $match->ignoreField('title', 'body')->match('test'); |
||
| 184 | * // @!(title,body) test |
||
| 185 | * |
||
| 186 | * $match->ignoreField(['title', 'body'])->match('test'); |
||
| 187 | * // @!(title,body) test |
||
| 188 | * |
||
| 189 | * @param string|array $fields Field or fields to ignore during search. |
||
| 190 | */ |
||
| 191 | public function ignoreField($fields) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Match an exact phrase. |
||
| 206 | * |
||
| 207 | * Example: |
||
| 208 | * $match->phrase('test case'); |
||
| 209 | * // "test case" |
||
| 210 | * |
||
| 211 | * @param string $keywords The phrase to match. |
||
| 212 | */ |
||
| 213 | public function phrase($keywords) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Provide an optional phrase. |
||
| 221 | * |
||
| 222 | * Example: |
||
| 223 | * $match->phrase('test case')->orPhrase('another case'); |
||
| 224 | * // "test case" | "another case" |
||
| 225 | * |
||
| 226 | * @param string $keywords The phrase to match. |
||
| 227 | */ |
||
| 228 | public function orPhrase($keywords) |
||
| 229 | { |
||
| 230 | $this->tokens[] = array('OPERATOR' => '| '); |
||
| 231 | $this->phrase($keywords); |
||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Match if keywords are close enough. |
||
| 237 | * |
||
| 238 | * Example: |
||
| 239 | * $match->proximity('test case', 5); |
||
| 240 | * // "test case"~5 |
||
| 241 | * |
||
| 242 | * @param string $keywords The words to match. |
||
| 243 | * @param int $distance The upper limit on separation between words. |
||
| 244 | */ |
||
| 245 | public function proximity($keywords, $distance) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Match if enough keywords are present. |
||
| 256 | * |
||
| 257 | * Examples: |
||
| 258 | * $match->quorum('this is a test case', 3); |
||
| 259 | * // "this is a test case"/3 |
||
| 260 | * |
||
| 261 | * $match->quorum('this is a test case', 0.5); |
||
| 262 | * // "this is a test case"/0.5 |
||
| 263 | * |
||
| 264 | * @param string $keywords The words to match. |
||
| 265 | * @param int|float $threshold The minimum number or percent of words that must match. |
||
| 266 | */ |
||
| 267 | public function quorum($keywords, $threshold) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Assert keywords or expressions must be matched in order. |
||
| 278 | * |
||
| 279 | * Examples: |
||
| 280 | * $match->match('test')->before(); |
||
| 281 | * // test << |
||
| 282 | * |
||
| 283 | * $match->match('test')->before('case'); |
||
| 284 | * // test << case |
||
| 285 | * |
||
| 286 | * @param string|Match|Closure $keywords The text or expression that must come after. |
||
| 287 | */ |
||
| 288 | public function before($keywords = null) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Assert a keyword must be matched exactly as written. |
||
| 297 | * |
||
| 298 | * Examples: |
||
| 299 | * $match->match('test')->exact('cases'); |
||
| 300 | * // test =cases |
||
| 301 | * |
||
| 302 | * $match->match('test')->exact()->phrase('specific cases'); |
||
| 303 | * // test ="specific cases" |
||
| 304 | * |
||
| 305 | * @param string $keyword The word that must be matched exactly. |
||
| 306 | */ |
||
| 307 | public function exact($keyword = null) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Boost the IDF score of a keyword. |
||
| 316 | * |
||
| 317 | * Examples: |
||
| 318 | * $match->match('test')->boost(1.2); |
||
| 319 | * // test^1.2 |
||
| 320 | * |
||
| 321 | * $match->match('test')->boost('case', 1.2); |
||
| 322 | * // test case^1.2 |
||
| 323 | * |
||
| 324 | * @param string $keyword The word to modify the score of. |
||
| 325 | * @param float $amount The amount to boost the score. |
||
| 326 | */ |
||
| 327 | public function boost($keyword, $amount = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Assert keywords or expressions must be matched close to each other. |
||
| 340 | * |
||
| 341 | * Examples: |
||
| 342 | * $match->match('test')->near(3); |
||
| 343 | * // test NEAR/3 |
||
| 344 | * |
||
| 345 | * $match->match('test')->near('case', 3); |
||
| 346 | * // test NEAR/3 case |
||
| 347 | * |
||
| 348 | * @param string|Match|Closure $keywords The text or expression to match nearby. |
||
| 349 | * @param int $distance Maximum distance to the match. |
||
| 350 | */ |
||
| 351 | public function near($keywords, $distance = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Assert matches must be in the same sentence. |
||
| 362 | * |
||
| 363 | * Examples: |
||
| 364 | * $match->match('test')->sentence(); |
||
| 365 | * // test SENTENCE |
||
| 366 | * |
||
| 367 | * $match->match('test')->sentence('case'); |
||
| 368 | * // test SENTENCE case |
||
| 369 | * |
||
| 370 | * @param string|Match|Closure $keywords The text or expression that must be in the sentence. |
||
| 371 | */ |
||
| 372 | public function sentence($keywords = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Assert matches must be in the same paragraph. |
||
| 381 | * |
||
| 382 | * Examples: |
||
| 383 | * $match->match('test')->paragraph(); |
||
| 384 | * // test PARAGRAPH |
||
| 385 | * |
||
| 386 | * $match->match('test')->paragraph('case'); |
||
| 387 | * // test PARAGRAPH case |
||
| 388 | * |
||
| 389 | * @param string|Match|Closure $keywords The text or expression that must be in the paragraph. |
||
| 390 | */ |
||
| 391 | public function paragraph($keywords = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Assert matches must be in the specified zone(s). |
||
| 400 | * |
||
| 401 | * Examples: |
||
| 402 | * $match->zone('th'); |
||
| 403 | * // ZONE:(th) |
||
| 404 | * |
||
| 405 | * $match->zone(['h3', 'h4']); |
||
| 406 | * // ZONE:(h3,h4) |
||
| 407 | * |
||
| 408 | * $match->zone('th', 'test'); |
||
| 409 | * // ZONE:(th) test |
||
| 410 | * |
||
| 411 | * @param string|array $zones The zone or zones to search. |
||
| 412 | * @param string|Match|Closure $keywords The text or expression that must be in these zones. |
||
| 413 | */ |
||
| 414 | public function zone($zones, $keywords = null) |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Assert matches must be in the same instance of the specified zone. |
||
| 427 | * |
||
| 428 | * Examples: |
||
| 429 | * $match->zonespan('th'); |
||
| 430 | * // ZONESPAN:(th) |
||
| 431 | * |
||
| 432 | * $match->zonespan('th', 'test'); |
||
| 433 | * // ZONESPAN:(th) test |
||
| 434 | * |
||
| 435 | * @param string $zone The zone to search. |
||
| 436 | * @param string|Match|Closure $keywords The text or expression that must be in this zone. |
||
| 437 | */ |
||
| 438 | public function zonespan($zone, $keywords = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Build the match expression. |
||
| 447 | */ |
||
| 448 | public function compile() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the latest compiled match expression. |
||
| 503 | * |
||
| 504 | * @return string The last compiled match expression. |
||
| 505 | */ |
||
| 506 | public function getCompiled() |
||
| 510 | } |
||
| 511 |