Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Zend_Search_Lucene_Search_Query_MultiTerm 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 Zend_Search_Lucene_Search_Query_MultiTerm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Zend_Search_Lucene_Search_Query_MultiTerm extends Zend_Search_Lucene_Search_Query |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Terms to find. |
||
| 40 | * Array of Zend_Search_Lucene_Index_Term |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $_terms = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Term signs. |
||
| 48 | * If true then term is required. |
||
| 49 | * If false then term is prohibited. |
||
| 50 | * If null then term is neither prohibited, nor required |
||
| 51 | * |
||
| 52 | * If array is null then all terms are required |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | |||
| 57 | private $_signs = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Result vector. |
||
| 61 | * Bitset or array of document IDs |
||
| 62 | * (depending from Bitset extension availability). |
||
| 63 | * |
||
| 64 | * @var mixed |
||
| 65 | */ |
||
| 66 | private $_resVector = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Terms positions vectors. |
||
| 70 | * Array of Arrays: |
||
| 71 | * term1Id => (docId => array( pos1, pos2, ... ), ...) |
||
| 72 | * term2Id => (docId => array( pos1, pos2, ... ), ...) |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $_termsPositions = array(); |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * A score factor based on the fraction of all query terms |
||
| 81 | * that a document contains. |
||
| 82 | * float for conjunction queries |
||
| 83 | * array of float for non conjunction queries |
||
| 84 | * |
||
| 85 | * @var mixed |
||
| 86 | */ |
||
| 87 | private $_coord = null; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Terms weights |
||
| 92 | * array of Zend_Search_Lucene_Search_Weight |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $_weights = array(); |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Class constructor. Create a new multi-term query object. |
||
| 101 | * |
||
| 102 | * @param array $terms Array of Zend_Search_Lucene_Index_Term objects |
||
| 103 | * @param array $signs Array of signs. Sign is boolean|null. |
||
| 104 | * @return void |
||
|
|
|||
| 105 | */ |
||
| 106 | public function __construct($terms = null, $signs = null) |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Add a $term (Zend_Search_Lucene_Index_Term) to this query. |
||
| 130 | * |
||
| 131 | * The sign is specified as: |
||
| 132 | * TRUE - term is required |
||
| 133 | * FALSE - term is prohibited |
||
| 134 | * NULL - term is neither prohibited, nor required |
||
| 135 | * |
||
| 136 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 137 | * @param boolean|null $sign |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function addTerm(Zend_Search_Lucene_Index_Term $term, $sign=null) { |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns query term |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function getTerms() |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Return terms signs |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getSigns() |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Set weight for specified term |
||
| 186 | * |
||
| 187 | * @param integer $num |
||
| 188 | * @param Zend_Search_Lucene_Search_Weight_Term $weight |
||
| 189 | */ |
||
| 190 | public function setWeight($num, $weight) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Constructs an appropriate Weight implementation for this query. |
||
| 198 | * |
||
| 199 | * @param Zend_Search_Lucene $reader |
||
| 200 | * @return Zend_Search_Lucene_Search_Weight |
||
| 201 | */ |
||
| 202 | protected function _createWeight($reader) |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Calculate result vector for Conjunction query |
||
| 210 | * (like '+something +another') |
||
| 211 | * |
||
| 212 | * @param Zend_Search_Lucene $reader |
||
| 213 | */ |
||
| 214 | View Code Duplication | private function _calculateConjunctionResult($reader) |
|
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Calculate result vector for non Conjunction query |
||
| 249 | * (like '+something -another') |
||
| 250 | * |
||
| 251 | * @param Zend_Search_Lucene $reader |
||
| 252 | */ |
||
| 253 | private function _calculateNonConjunctionResult($reader) |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Score calculator for conjunction queries (all terms are required) |
||
| 339 | * |
||
| 340 | * @param integer $docId |
||
| 341 | * @param Zend_Search_Lucene $reader |
||
| 342 | * @return float |
||
| 343 | */ |
||
| 344 | public function _conjunctionScore($docId, $reader) |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Score calculator for non conjunction queries (not all terms are required) |
||
| 365 | * |
||
| 366 | * @param integer $docId |
||
| 367 | * @param Zend_Search_Lucene $reader |
||
| 368 | * @return float |
||
| 369 | */ |
||
| 370 | public function _nonConjunctionScore($docId, $reader) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Score specified document |
||
| 407 | * |
||
| 408 | * @param integer $docId |
||
| 409 | * @param Zend_Search_Lucene $reader |
||
| 410 | * @return float |
||
| 411 | */ |
||
| 412 | public function score($docId, $reader) |
||
| 436 | } |
||
| 437 | |||
| 438 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.