Complex classes like ResultList 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 ResultList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ResultList extends \ViewableData implements \SS_Limitable, \SS_List { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var \Elastica\Index |
||
| 17 | */ |
||
| 18 | private $service; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \Elastica\Query |
||
| 22 | */ |
||
| 23 | private $query; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * List of types to search for, default (blank) returns all |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | |||
| 30 | private $types = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Filters, i.e. selected aggregations, to apply to the search |
||
| 34 | */ |
||
| 35 | private $filters = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * An array list of aggregations from this search |
||
| 39 | * @var ArrayList |
||
| 40 | */ |
||
| 41 | private $aggregations; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a search and then optionally tweak it. Actual search is only performed against |
||
| 46 | * Elasticsearch when the getResults() method is called. |
||
| 47 | * |
||
| 48 | * @param ElasticaService $service object used to communicate with Elasticsearch |
||
| 49 | * @param Query $query Elastica query object, created via QueryGenerator |
||
| 50 | * @param string $queryText the text from the query |
||
| 51 | * @param array $filters Selected filters, used for aggregation purposes only |
||
| 52 | * (i.e. query already filtered prior to this) |
||
| 53 | */ |
||
| 54 | public function __construct(ElasticaService $service, Query $query, $queryText, $filters = array()) { |
||
| 55 | $this->service = $service; |
||
|
|
|||
| 56 | $this->query = $query; |
||
| 57 | $this->originalQueryText = $queryText; |
||
| 58 | $this->filters = $filters; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function __clone() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return \Elastica\Index |
||
| 67 | */ |
||
| 68 | public function getService() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set a new list of types (SilverStripe classes) to search for |
||
| 74 | * @param string $newTypes comma separated list of types to search for |
||
| 75 | */ |
||
| 76 | public function setTypes($newTypes) { |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @return \Elastica\Query |
||
| 83 | */ |
||
| 84 | public function getQuery() { |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the aggregation results for this query. Should only be called |
||
| 91 | * after $this->getResults() has been executed. |
||
| 92 | * Note this will be an empty array list if there is no aggregation |
||
| 93 | * |
||
| 94 | * @return ArrayList ArrayList of the aggregated results for this query |
||
| 95 | */ |
||
| 96 | public function getAggregations() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function getResults() { |
||
| 245 | |||
| 246 | |||
| 247 | public function getTotalItems() { |
||
| 251 | |||
| 252 | |||
| 253 | public function getTotalTime() { |
||
| 256 | |||
| 257 | public function getIterator() { |
||
| 260 | |||
| 261 | public function limit($limit, $offset = 0) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Converts results of type {@link \Elastica\Result} |
||
| 272 | * into their respective {@link DataObject} counterparts. |
||
| 273 | * |
||
| 274 | * @return array DataObject[] |
||
| 275 | */ |
||
| 276 | public function toArray() { |
||
| 362 | |||
| 363 | public function toArrayList() { |
||
| 366 | |||
| 367 | public function toNestedArray() { |
||
| 376 | |||
| 377 | public function first() { |
||
| 381 | |||
| 382 | public function last() { |
||
| 386 | |||
| 387 | public function map($key = 'ID', $title = 'Title') { |
||
| 390 | |||
| 391 | public function column($col = 'ID') { |
||
| 404 | |||
| 405 | public function each($callback) { |
||
| 408 | |||
| 409 | public function count() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @ignore |
||
| 415 | */ |
||
| 416 | public function offsetExists($offset) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @ignore |
||
| 422 | */ |
||
| 423 | public function offsetGet($offset) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @ignore |
||
| 429 | */ |
||
| 430 | public function offsetSet($offset, $value) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @ignore |
||
| 436 | */ |
||
| 437 | public function offsetUnset($offset) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @ignore |
||
| 443 | */ |
||
| 444 | public function add($item) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @ignore |
||
| 450 | */ |
||
| 451 | public function remove($item) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @ignore |
||
| 457 | */ |
||
| 458 | public function find($key, $value) { |
||
| 461 | |||
| 462 | } |
||
| 463 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..