Conditions | 50 |
Paths | > 20000 |
Total Lines | 287 |
Code Lines | 144 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 50 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
156 | 1 | public function optimize(Zend_Search_Lucene_Interface $index) |
|
157 | { |
||
158 | 1 | $subqueries = array(); |
|
159 | 1 | $signs = array(); |
|
160 | |||
161 | // Optimize all subqueries |
||
162 | 1 | foreach ($this->_subqueries as $id => $subquery) { |
|
163 | 1 | $subqueries[] = $subquery->optimize($index); |
|
164 | 1 | $signs[] = ($this->_signs === null)? true : $this->_signs[$id]; |
|
165 | } |
||
166 | |||
167 | // Remove insignificant subqueries |
||
168 | 1 | foreach ($subqueries as $id => $subquery) { |
|
169 | 1 | if ($subquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) { |
|
170 | // Insignificant subquery has to be removed anyway |
||
171 | unset($subqueries[$id]); |
||
172 | 1 | unset($signs[$id]); |
|
173 | } |
||
174 | } |
||
175 | 1 | if (count($subqueries) == 0) { |
|
176 | // Boolean query doesn't has non-insignificant subqueries |
||
177 | require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php'; |
||
178 | return new Zend_Search_Lucene_Search_Query_Insignificant(); |
||
179 | } |
||
180 | // Check if all non-insignificant subqueries are prohibited |
||
181 | 1 | $allProhibited = true; |
|
182 | 1 | foreach ($signs as $sign) { |
|
183 | 1 | if ($sign !== false) { |
|
184 | 1 | $allProhibited = false; |
|
185 | 1 | break; |
|
186 | } |
||
187 | } |
||
188 | 1 | if ($allProhibited) { |
|
189 | require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php'; |
||
190 | return new Zend_Search_Lucene_Search_Query_Insignificant(); |
||
191 | } |
||
192 | |||
193 | |||
194 | // Check for empty subqueries |
||
195 | 1 | foreach ($subqueries as $id => $subquery) { |
|
196 | 1 | if ($subquery instanceof Zend_Search_Lucene_Search_Query_Empty) { |
|
197 | 1 | if ($signs[$id] === true) { |
|
198 | // Matching is required, but is actually empty |
||
199 | require_once 'Zend/Search/Lucene/Search/Query/Empty.php'; |
||
200 | return new Zend_Search_Lucene_Search_Query_Empty(); |
||
201 | } else { |
||
202 | // Matching is optional or prohibited, but is empty |
||
203 | // Remove it from subqueries and signs list |
||
204 | 1 | unset($subqueries[$id]); |
|
205 | 1 | unset($signs[$id]); |
|
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | // Check, if reduced subqueries list is empty |
||
211 | 1 | if (count($subqueries) == 0) { |
|
212 | 1 | require_once 'Zend/Search/Lucene/Search/Query/Empty.php'; |
|
213 | 1 | return new Zend_Search_Lucene_Search_Query_Empty(); |
|
214 | } |
||
215 | |||
216 | // Check if all non-empty subqueries are prohibited |
||
217 | $allProhibited = true; |
||
218 | foreach ($signs as $sign) { |
||
219 | if ($sign !== false) { |
||
220 | $allProhibited = false; |
||
221 | break; |
||
222 | } |
||
223 | } |
||
224 | if ($allProhibited) { |
||
225 | require_once 'Zend/Search/Lucene/Search/Query/Empty.php'; |
||
226 | return new Zend_Search_Lucene_Search_Query_Empty(); |
||
227 | } |
||
228 | |||
229 | |||
230 | // Check, if reduced subqueries list has only one entry |
||
231 | if (count($subqueries) == 1) { |
||
232 | // It's a query with only one required or optional clause |
||
233 | // (it's already checked, that it's not a prohibited clause) |
||
234 | |||
235 | if ($this->getBoost() == 1) { |
||
236 | return reset($subqueries); |
||
237 | } |
||
238 | |||
239 | $optimizedQuery = clone reset($subqueries); |
||
240 | $optimizedQuery->setBoost($optimizedQuery->getBoost()*$this->getBoost()); |
||
241 | |||
242 | return $optimizedQuery; |
||
243 | } |
||
244 | |||
245 | |||
246 | // Prepare first candidate for optimized query |
||
247 | $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs); |
||
248 | $optimizedQuery->setBoost($this->getBoost()); |
||
249 | |||
250 | |||
251 | $terms = array(); |
||
252 | $tsigns = array(); |
||
253 | $boostFactors = array(); |
||
254 | |||
255 | // Try to decompose term and multi-term subqueries |
||
256 | foreach ($subqueries as $id => $subquery) { |
||
257 | if ($subquery instanceof Zend_Search_Lucene_Search_Query_Term) { |
||
258 | $terms[] = $subquery->getTerm(); |
||
259 | $tsigns[] = $signs[$id]; |
||
260 | $boostFactors[] = $subquery->getBoost(); |
||
261 | |||
262 | // remove subquery from a subqueries list |
||
263 | unset($subqueries[$id]); |
||
264 | unset($signs[$id]); |
||
265 | } else if ($subquery instanceof Zend_Search_Lucene_Search_Query_MultiTerm) { |
||
266 | $subTerms = $subquery->getTerms(); |
||
267 | $subSigns = $subquery->getSigns(); |
||
268 | |||
269 | if ($signs[$id] === true) { |
||
270 | // It's a required multi-term subquery. |
||
271 | // Something like '... +(+term1 -term2 term3 ...) ...' |
||
272 | |||
273 | // Multi-term required subquery can be decomposed only if it contains |
||
274 | // required terms and doesn't contain prohibited terms: |
||
275 | // ... +(+term1 term2 ...) ... => ... +term1 term2 ... |
||
276 | // |
||
277 | // Check this |
||
278 | $hasRequired = false; |
||
279 | $hasProhibited = false; |
||
280 | if ($subSigns === null) { |
||
281 | // All subterms are required |
||
282 | $hasRequired = true; |
||
283 | } else { |
||
284 | foreach ($subSigns as $sign) { |
||
285 | if ($sign === true) { |
||
286 | $hasRequired = true; |
||
287 | } else if ($sign === false) { |
||
288 | $hasProhibited = true; |
||
289 | break; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | // Continue if subquery has prohibited terms or doesn't have required terms |
||
294 | if ($hasProhibited || !$hasRequired) { |
||
295 | continue; |
||
296 | } |
||
297 | |||
298 | foreach ($subTerms as $termId => $term) { |
||
299 | $terms[] = $term; |
||
300 | $tsigns[] = ($subSigns === null)? true : $subSigns[$termId]; |
||
301 | $boostFactors[] = $subquery->getBoost(); |
||
302 | } |
||
303 | |||
304 | // remove subquery from a subqueries list |
||
305 | unset($subqueries[$id]); |
||
306 | unset($signs[$id]); |
||
307 | |||
308 | } else { // $signs[$id] === null || $signs[$id] === false |
||
309 | // It's an optional or prohibited multi-term subquery. |
||
310 | // Something like '... (+term1 -term2 term3 ...) ...' |
||
311 | // or |
||
312 | // something like '... -(+term1 -term2 term3 ...) ...' |
||
313 | |||
314 | // Multi-term optional and required subqueries can be decomposed |
||
315 | // only if all terms are optional. |
||
316 | // |
||
317 | // Check if all terms are optional. |
||
318 | $onlyOptional = true; |
||
319 | if ($subSigns === null) { |
||
320 | // All subterms are required |
||
321 | $onlyOptional = false; |
||
322 | } else { |
||
323 | foreach ($subSigns as $sign) { |
||
324 | if ($sign !== null) { |
||
325 | $onlyOptional = false; |
||
326 | break; |
||
327 | } |
||
328 | } |
||
329 | } |
||
330 | |||
331 | // Continue if non-optional terms are presented in this multi-term subquery |
||
332 | if (!$onlyOptional) { |
||
333 | continue; |
||
334 | } |
||
335 | |||
336 | foreach ($subTerms as $termId => $term) { |
||
337 | $terms[] = $term; |
||
338 | $tsigns[] = ($signs[$id] === null)? null /* optional */ : |
||
339 | false /* prohibited */; |
||
340 | $boostFactors[] = $subquery->getBoost(); |
||
341 | } |
||
342 | |||
343 | // remove subquery from a subqueries list |
||
344 | unset($subqueries[$id]); |
||
345 | unset($signs[$id]); |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | |||
350 | |||
351 | // Check, if there are no decomposed subqueries |
||
352 | if (count($terms) == 0 ) { |
||
353 | // return prepared candidate |
||
354 | return $optimizedQuery; |
||
355 | } |
||
356 | |||
357 | |||
358 | // Check, if all subqueries have been decomposed and all terms has the same boost factor |
||
359 | if (count($subqueries) == 0 && count(array_unique($boostFactors)) == 1) { |
||
360 | require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php'; |
||
361 | $optimizedQuery = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns); |
||
362 | $optimizedQuery->setBoost(reset($boostFactors)*$this->getBoost()); |
||
363 | |||
364 | return $optimizedQuery; |
||
365 | } |
||
366 | |||
367 | |||
368 | // This boolean query can't be transformed to Term/MultiTerm query and still contains |
||
369 | // several subqueries |
||
370 | |||
371 | // Separate prohibited terms |
||
372 | $prohibitedTerms = array(); |
||
373 | foreach ($terms as $id => $term) { |
||
374 | if ($tsigns[$id] === false) { |
||
375 | $prohibitedTerms[] = $term; |
||
376 | |||
377 | unset($terms[$id]); |
||
378 | unset($tsigns[$id]); |
||
379 | unset($boostFactors[$id]); |
||
380 | } |
||
381 | } |
||
382 | |||
383 | if (count($terms) == 1) { |
||
384 | require_once 'Zend/Search/Lucene/Search/Query/Term.php'; |
||
385 | $clause = new Zend_Search_Lucene_Search_Query_Term(reset($terms)); |
||
386 | $clause->setBoost(reset($boostFactors)); |
||
387 | |||
388 | $subqueries[] = $clause; |
||
389 | $signs[] = reset($tsigns); |
||
390 | |||
391 | // Clear terms list |
||
392 | $terms = array(); |
||
393 | } else if (count($terms) > 1 && count(array_unique($boostFactors)) == 1) { |
||
394 | require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php'; |
||
395 | $clause = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns); |
||
396 | $clause->setBoost(reset($boostFactors)); |
||
397 | |||
398 | $subqueries[] = $clause; |
||
399 | // Clause sign is 'required' if clause contains required terms. 'Optional' otherwise. |
||
400 | $signs[] = (in_array(true, $tsigns))? true : null; |
||
401 | |||
402 | // Clear terms list |
||
403 | $terms = array(); |
||
404 | } |
||
405 | |||
406 | if (count($prohibitedTerms) == 1) { |
||
407 | // (boost factors are not significant for prohibited clauses) |
||
408 | require_once 'Zend/Search/Lucene/Search/Query/Term.php'; |
||
409 | $subqueries[] = new Zend_Search_Lucene_Search_Query_Term(reset($prohibitedTerms)); |
||
410 | $signs[] = false; |
||
411 | |||
412 | // Clear prohibited terms list |
||
413 | $prohibitedTerms = array(); |
||
414 | } else if (count($prohibitedTerms) > 1) { |
||
415 | // prepare signs array |
||
416 | $prohibitedSigns = array(); |
||
417 | foreach ($prohibitedTerms as $id => $term) { |
||
418 | // all prohibited term are grouped as optional into multi-term query |
||
419 | $prohibitedSigns[$id] = null; |
||
420 | } |
||
421 | |||
422 | // (boost factors are not significant for prohibited clauses) |
||
423 | require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php'; |
||
424 | $subqueries[] = new Zend_Search_Lucene_Search_Query_MultiTerm($prohibitedTerms, $prohibitedSigns); |
||
425 | // Clause sign is 'prohibited' |
||
426 | $signs[] = false; |
||
427 | |||
428 | // Clear terms list |
||
429 | $prohibitedTerms = array(); |
||
430 | } |
||
431 | |||
432 | /** @todo Group terms with the same boost factors together */ |
||
433 | |||
434 | // Check, that all terms are processed |
||
435 | // Replace candidate for optimized query |
||
436 | if (count($terms) == 0 && count($prohibitedTerms) == 0) { |
||
437 | $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs); |
||
438 | $optimizedQuery->setBoost($this->getBoost()); |
||
439 | } |
||
440 | |||
441 | return $optimizedQuery; |
||
442 | } |
||
443 | |||
816 |
Adding a
@return
annotation 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.