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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | class Zend_Search_Lucene |
||
61 | { |
||
62 | /** |
||
63 | * File system adapter. |
||
64 | * |
||
65 | * @var Zend_Search_Lucene_Storage_Directory |
||
66 | */ |
||
67 | private $_directory = null; |
||
68 | |||
69 | /** |
||
70 | * File system adapter closing option |
||
71 | * |
||
72 | * @var boolean |
||
73 | */ |
||
74 | private $_closeDirOnExit = true; |
||
75 | |||
76 | /** |
||
77 | * Writer for this index, not instantiated unless required. |
||
78 | * |
||
79 | * @var Zend_Search_Lucene_Index_Writer |
||
80 | */ |
||
81 | private $_writer = null; |
||
82 | |||
83 | /** |
||
84 | * Array of Zend_Search_Lucene_Index_SegmentInfo objects for this index. |
||
85 | * |
||
86 | * @var array Zend_Search_Lucene_Index_SegmentInfo |
||
87 | */ |
||
88 | private $_segmentInfos = array(); |
||
89 | |||
90 | /** |
||
91 | * Number of documents in this index. |
||
92 | * |
||
93 | * @var integer |
||
94 | */ |
||
95 | private $_docCount = 0; |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Opens the index. |
||
100 | * |
||
101 | * IndexReader constructor needs Directory as a parameter. It should be |
||
102 | * a string with a path to the index folder or a Directory object. |
||
103 | * |
||
104 | * @param mixed $directory |
||
105 | * @throws Zend_Search_Lucene_Exception |
||
106 | */ |
||
107 | public function __construct($directory = null, $create = false) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Object destructor |
||
163 | */ |
||
164 | public function __destruct() |
||
172 | |||
173 | /** |
||
174 | * Returns an instance of Zend_Search_Lucene_Index_Writer for the index |
||
175 | * |
||
176 | * @return Zend_Search_Lucene_Index_Writer |
||
177 | */ |
||
178 | public function getIndexWriter() |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Returns the Zend_Search_Lucene_Storage_Directory instance for this index. |
||
190 | * |
||
191 | * @return Zend_Search_Lucene_Storage_Directory |
||
192 | */ |
||
193 | public function getDirectory() |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Returns the total number of documents in this index. |
||
201 | * |
||
202 | * @return integer |
||
203 | */ |
||
204 | public function count() |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Performs a query against the index and returns an array |
||
212 | * of Zend_Search_Lucene_Search_QueryHit objects. |
||
213 | * Input is a string or Zend_Search_Lucene_Search_Query. |
||
214 | * |
||
215 | * @param mixed $query |
||
216 | * @return array ZSearchHit |
||
217 | */ |
||
218 | public function find($query) |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Returns a list of all unique field names that exist in this index. |
||
253 | * |
||
254 | * @param boolean $indexed |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getFieldNames($indexed = false) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Returns a Zend_Search_Lucene_Document object for the document |
||
269 | * number $id in this index. |
||
270 | * |
||
271 | * @param integer|Zend_Search_Lucene_Search_QueryHit $id |
||
272 | * @return Zend_Search_Lucene_Document |
||
273 | */ |
||
274 | public function getDocument($id) |
||
330 | |||
331 | |||
332 | /** |
||
333 | * Returns an array of all the documents which contain term. |
||
334 | * |
||
335 | * @param Zend_Search_Lucene_Index_Term $term |
||
336 | * @return array |
||
337 | */ |
||
338 | public function termDocs(Zend_Search_Lucene_Index_Term $term) |
||
371 | |||
372 | |||
373 | /** |
||
374 | * Returns an array of all term positions in the documents. |
||
375 | * Return array structure: array( docId => array( pos1, pos2, ...), ...) |
||
376 | * |
||
377 | * @param Zend_Search_Lucene_Index_Term $term |
||
378 | * @return array |
||
379 | */ |
||
380 | public function termPositions(Zend_Search_Lucene_Index_Term $term) |
||
426 | |||
427 | |||
428 | /** |
||
429 | * Returns the number of documents in this index containing the $term. |
||
430 | * |
||
431 | * @param Zend_Search_Lucene_Index_Term $term |
||
432 | * @return integer |
||
433 | */ |
||
434 | public function docFreq(Zend_Search_Lucene_Index_Term $term) |
||
446 | |||
447 | |||
448 | /** |
||
449 | * Retrive similarity used by index reader |
||
450 | * |
||
451 | * @return Zend_Search_Lucene_Search_Similarity |
||
452 | */ |
||
453 | public function getSimilarity() |
||
457 | |||
458 | |||
459 | /** |
||
460 | * Returns a normalization factor for "field, document" pair. |
||
461 | * |
||
462 | * @param integer $id |
||
463 | * @param string $fieldName |
||
464 | * @return Zend_Search_Lucene_Document |
||
465 | */ |
||
466 | public function norm( $id, $fieldName ) |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Adds a document to this index. |
||
486 | * |
||
487 | * @param Zend_Search_Lucene_Document $document |
||
488 | */ |
||
489 | public function addDocument(Zend_Search_Lucene_Document $document) |
||
497 | |||
498 | |||
499 | /** |
||
500 | * Commit changes resulting from delete() or undeleteAll() operations. |
||
501 | * |
||
502 | * @todo delete() and undeleteAll processing. |
||
503 | */ |
||
504 | public function commit() |
||
521 | |||
522 | |||
523 | /************************************************************************* |
||
524 | @todo UNIMPLEMENTED |
||
525 | *************************************************************************/ |
||
526 | |||
527 | /** |
||
528 | * Returns an array of all terms in this index. |
||
529 | * |
||
530 | * @todo Implementation |
||
531 | * @return array |
||
532 | */ |
||
533 | public function terms() |
||
537 | |||
538 | |||
539 | /** |
||
540 | * Returns true if any documents have been deleted from this index. |
||
541 | * |
||
542 | * @todo Implementation |
||
543 | * @return boolean |
||
544 | */ |
||
545 | public function hasDeletions() |
||
549 | |||
550 | |||
551 | /** |
||
552 | * Deletes a document from the index. $doc may contain a Zend_Search_Lucene_Document |
||
553 | * or the number of the document to delete. |
||
554 | * |
||
555 | * @todo Implementation |
||
556 | * @param mixed $item_to_del |
||
557 | */ |
||
558 | public function delete($doc) |
||
560 | |||
561 | |||
562 | /** |
||
563 | * Undeletes all documents currently marked as deleted in this index. |
||
564 | * |
||
565 | * @todo Implementation |
||
566 | */ |
||
567 | public function undeleteAll() |
||
569 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.