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_Phrase 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_Phrase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Zend_Search_Lucene_Search_Query_Phrase extends Zend_Search_Lucene_Search_Query |
||
42 | { |
||
43 | /** |
||
44 | * Terms to find. |
||
45 | * Array of Zend_Search_Lucene_Index_Term objects. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private $_terms; |
||
50 | |||
51 | /** |
||
52 | * Term positions (relative positions of terms within the phrase). |
||
53 | * Array of integers |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $_offsets; |
||
58 | |||
59 | /** |
||
60 | * Sets the number of other words permitted between words in query phrase. |
||
61 | * If zero, then this is an exact phrase search. For larger values this works |
||
62 | * like a WITHIN or NEAR operator. |
||
63 | * |
||
64 | * The slop is in fact an edit-distance, where the units correspond to |
||
65 | * moves of terms in the query phrase out of position. For example, to switch |
||
66 | * the order of two words requires two moves (the first move places the words |
||
67 | * atop one another), so to permit re-orderings of phrases, the slop must be |
||
68 | * at least two. |
||
69 | * More exact matches are scored higher than sloppier matches, thus search |
||
70 | * results are sorted by exactness. |
||
71 | * |
||
72 | * The slop is zero by default, requiring exact matches. |
||
73 | * |
||
74 | * @var unknown_type |
||
75 | */ |
||
76 | private $_slop; |
||
77 | |||
78 | /** |
||
79 | * Result vector. |
||
80 | * Bitset or array of document IDs |
||
81 | * (depending from Bitset extension availability). |
||
82 | * |
||
83 | * @var mixed |
||
84 | */ |
||
85 | private $_resVector = null; |
||
86 | |||
87 | /** |
||
88 | * Terms positions vectors. |
||
89 | * Array of Arrays: |
||
90 | * term1Id => (docId => array( pos1, pos2, ... ), ...) |
||
91 | * term2Id => (docId => array( pos1, pos2, ... ), ...) |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | private $_termsPositions = array(); |
||
96 | |||
97 | /** |
||
98 | * Class constructor. Create a new prase query. |
||
99 | * |
||
100 | * @param string $field Field to search. |
||
101 | * @param array $terms Terms to search Array of strings. |
||
102 | * @param array $offsets Relative term positions. Array of integers. |
||
103 | * @throws Zend_Search_Lucene_Exception |
||
104 | */ |
||
105 | public function __construct($terms = null, $offsets = null, $field = null) |
||
136 | |||
137 | /** |
||
138 | * Set slop |
||
139 | * |
||
140 | * @param integer $slop |
||
141 | */ |
||
142 | public function setSlop($slop) |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Get slop |
||
150 | * |
||
151 | * @return integer |
||
152 | */ |
||
153 | public function getSlop() |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Adds a term to the end of the query phrase. |
||
161 | * The relative position of the term is specified explicitly or the one immediately |
||
162 | * after the last term added. |
||
163 | * |
||
164 | * @param Zend_Search_Lucene_Index_Term $term |
||
165 | * @param integer $position |
||
166 | */ |
||
167 | public function addTerm(Zend_Search_Lucene_Index_Term $term, $position = null) { |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Returns query term |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getTerms() |
||
193 | |||
194 | |||
195 | /** |
||
196 | * Set weight for specified term |
||
197 | * |
||
198 | * @param integer $num |
||
199 | * @param Zend_Search_Lucene_Search_Weight_Term $weight |
||
200 | */ |
||
201 | public function setWeight($num, $weight) |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Constructs an appropriate Weight implementation for this query. |
||
209 | * |
||
210 | * @param Zend_Search_Lucene $reader |
||
211 | * @return Zend_Search_Lucene_Search_Weight |
||
212 | */ |
||
213 | protected function _createWeight($reader) |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Calculate result vector |
||
221 | * |
||
222 | * @param Zend_Search_Lucene $reader |
||
223 | */ |
||
224 | View Code Duplication | private function _calculateResult($reader) |
|
255 | |||
256 | |||
257 | /** |
||
258 | * Score calculator for exact phrase queries (terms sequence is fixed) |
||
259 | * |
||
260 | * @param integer $docId |
||
261 | * @return float |
||
262 | */ |
||
263 | public function _exactPhraseFreq($docId) |
||
301 | |||
302 | /** |
||
303 | * Score calculator for sloppy phrase queries (terms sequence is fixed) |
||
304 | * |
||
305 | * @param integer $docId |
||
306 | * @param Zend_Search_Lucene $reader |
||
307 | * @return float |
||
308 | */ |
||
309 | public function _sloppyPhraseFreq($docId, Zend_Search_Lucene $reader) |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Score specified document |
||
381 | * |
||
382 | * @param integer $docId |
||
383 | * @param Zend_Search_Lucene $reader |
||
384 | * @return float |
||
385 | */ |
||
386 | public function score($docId, $reader) |
||
423 | } |
||
424 | |||
425 |
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..