Complex classes like Zend_Search_Lucene_MultiSearcher 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_MultiSearcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Zend_Search_Lucene_MultiSearcher implements Zend_Search_Lucene_Interface |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * List of indices for searching. |
||
| 44 | * Array of Zend_Search_Lucene_Interface objects |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $_indices; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Object constructor. |
||
| 52 | * |
||
| 53 | * @param array $indices Arrays of indices for search |
||
| 54 | * @throws Zend_Search_Lucene_Exception |
||
| 55 | */ |
||
| 56 | public function __construct($indices = array()) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add index for searching. |
||
| 70 | * |
||
| 71 | * @param Zend_Search_Lucene_Interface $index |
||
| 72 | */ |
||
| 73 | public function addIndex(Zend_Search_Lucene_Interface $index) |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Get current generation number |
||
| 81 | * |
||
| 82 | * Returns generation number |
||
| 83 | * 0 means pre-2.1 index format |
||
| 84 | * -1 means there are no segments files. |
||
| 85 | * |
||
| 86 | * @param Zend_Search_Lucene_Storage_Directory $directory |
||
| 87 | * @return integer |
||
| 88 | * @throws Zend_Search_Lucene_Exception |
||
| 89 | */ |
||
| 90 | public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get segments file name |
||
| 98 | * |
||
| 99 | * @param integer $generation |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public static function getSegmentFileName($generation) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get index format version |
||
| 109 | * |
||
| 110 | * @return integer |
||
| 111 | * @throws Zend_Search_Lucene_Exception |
||
| 112 | */ |
||
| 113 | public function getFormatVersion() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Set index format version. |
||
| 121 | * Index is converted to this format at the nearest upfdate time |
||
| 122 | * |
||
| 123 | * @param int $formatVersion |
||
| 124 | */ |
||
| 125 | public function setFormatVersion($formatVersion) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the Zend_Search_Lucene_Storage_Directory instance for this index. |
||
| 134 | * |
||
| 135 | * @return Zend_Search_Lucene_Storage_Directory |
||
| 136 | */ |
||
| 137 | public function getDirectory() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the total number of documents in this index (including deleted documents). |
||
| 145 | * |
||
| 146 | * @return integer |
||
| 147 | */ |
||
| 148 | public function count() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns one greater than the largest possible document number. |
||
| 161 | * This may be used to, e.g., determine how big to allocate a structure which will have |
||
| 162 | * an element for every document number in an index. |
||
| 163 | * |
||
| 164 | * @return integer |
||
| 165 | */ |
||
| 166 | public function maxDoc() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the total number of non-deleted documents in this index. |
||
| 173 | * |
||
| 174 | * @return integer |
||
| 175 | */ |
||
| 176 | public function numDocs() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Checks, that document is deleted |
||
| 189 | * |
||
| 190 | * @param integer $id |
||
| 191 | * @return boolean |
||
| 192 | * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range |
||
| 193 | */ |
||
| 194 | public function isDeleted($id) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set default search field. |
||
| 212 | * |
||
| 213 | * Null means, that search is performed through all fields by default |
||
| 214 | * |
||
| 215 | * Default value is null |
||
| 216 | * |
||
| 217 | * @param string $fieldName |
||
| 218 | */ |
||
| 219 | public static function setDefaultSearchField($fieldName) |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Get default search field. |
||
| 229 | * |
||
| 230 | * Null means, that search is performed through all fields by default |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | * @throws Zend_Search_Lucene_Exception |
||
| 234 | */ |
||
| 235 | public static function getDefaultSearchField() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set result set limit. |
||
| 256 | * |
||
| 257 | * 0 (default) means no limit |
||
| 258 | * |
||
| 259 | * @param integer $limit |
||
| 260 | */ |
||
| 261 | public static function setResultSetLimit($limit) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set result set limit. |
||
| 270 | * |
||
| 271 | * 0 means no limit |
||
| 272 | * |
||
| 273 | * @return integer |
||
| 274 | * @throws Zend_Search_Lucene_Exception |
||
| 275 | */ |
||
| 276 | public static function getResultSetLimit() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Retrieve index maxBufferedDocs option |
||
| 297 | * |
||
| 298 | * maxBufferedDocs is a minimal number of documents required before |
||
| 299 | * the buffered in-memory documents are written into a new Segment |
||
| 300 | * |
||
| 301 | * Default value is 10 |
||
| 302 | * |
||
| 303 | * @return integer |
||
| 304 | * @throws Zend_Search_Lucene_Exception |
||
| 305 | */ |
||
| 306 | public function getMaxBufferedDocs() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set index maxBufferedDocs option |
||
| 327 | * |
||
| 328 | * maxBufferedDocs is a minimal number of documents required before |
||
| 329 | * the buffered in-memory documents are written into a new Segment |
||
| 330 | * |
||
| 331 | * Default value is 10 |
||
| 332 | * |
||
| 333 | * @param integer $maxBufferedDocs |
||
| 334 | */ |
||
| 335 | public function setMaxBufferedDocs($maxBufferedDocs) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Retrieve index maxMergeDocs option |
||
| 344 | * |
||
| 345 | * maxMergeDocs is a largest number of documents ever merged by addDocument(). |
||
| 346 | * Small values (e.g., less than 10,000) are best for interactive indexing, |
||
| 347 | * as this limits the length of pauses while indexing to a few seconds. |
||
| 348 | * Larger values are best for batched indexing and speedier searches. |
||
| 349 | * |
||
| 350 | * Default value is PHP_INT_MAX |
||
| 351 | * |
||
| 352 | * @return integer |
||
| 353 | * @throws Zend_Search_Lucene_Exception |
||
| 354 | */ |
||
| 355 | public function getMaxMergeDocs() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set index maxMergeDocs option |
||
| 376 | * |
||
| 377 | * maxMergeDocs is a largest number of documents ever merged by addDocument(). |
||
| 378 | * Small values (e.g., less than 10,000) are best for interactive indexing, |
||
| 379 | * as this limits the length of pauses while indexing to a few seconds. |
||
| 380 | * Larger values are best for batched indexing and speedier searches. |
||
| 381 | * |
||
| 382 | * Default value is PHP_INT_MAX |
||
| 383 | * |
||
| 384 | * @param integer $maxMergeDocs |
||
| 385 | */ |
||
| 386 | public function setMaxMergeDocs($maxMergeDocs) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieve index mergeFactor option |
||
| 395 | * |
||
| 396 | * mergeFactor determines how often segment indices are merged by addDocument(). |
||
| 397 | * With smaller values, less RAM is used while indexing, |
||
| 398 | * and searches on unoptimized indices are faster, |
||
| 399 | * but indexing speed is slower. |
||
| 400 | * With larger values, more RAM is used during indexing, |
||
| 401 | * and while searches on unoptimized indices are slower, |
||
| 402 | * indexing is faster. |
||
| 403 | * Thus larger values (> 10) are best for batch index creation, |
||
| 404 | * and smaller values (< 10) for indices that are interactively maintained. |
||
| 405 | * |
||
| 406 | * Default value is 10 |
||
| 407 | * |
||
| 408 | * @return integer |
||
| 409 | * @throws Zend_Search_Lucene_Exception |
||
| 410 | */ |
||
| 411 | public function getMergeFactor() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set index mergeFactor option |
||
| 432 | * |
||
| 433 | * mergeFactor determines how often segment indices are merged by addDocument(). |
||
| 434 | * With smaller values, less RAM is used while indexing, |
||
| 435 | * and searches on unoptimized indices are faster, |
||
| 436 | * but indexing speed is slower. |
||
| 437 | * With larger values, more RAM is used during indexing, |
||
| 438 | * and while searches on unoptimized indices are slower, |
||
| 439 | * indexing is faster. |
||
| 440 | * Thus larger values (> 10) are best for batch index creation, |
||
| 441 | * and smaller values (< 10) for indices that are interactively maintained. |
||
| 442 | * |
||
| 443 | * Default value is 10 |
||
| 444 | * |
||
| 445 | * @param integer $maxMergeDocs |
||
|
|
|||
| 446 | */ |
||
| 447 | public function setMergeFactor($mergeFactor) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Performs a query against the index and returns an array |
||
| 456 | * of Zend_Search_Lucene_Search_QueryHit objects. |
||
| 457 | * Input is a string or Zend_Search_Lucene_Search_Query. |
||
| 458 | * |
||
| 459 | * @param mixed $query |
||
| 460 | * @return array Zend_Search_Lucene_Search_QueryHit |
||
| 461 | * @throws Zend_Search_Lucene_Exception |
||
| 462 | */ |
||
| 463 | public function find($query) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns a list of all unique field names that exist in this index. |
||
| 492 | * |
||
| 493 | * @param boolean $indexed |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | public function getFieldNames($indexed = false) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns a Zend_Search_Lucene_Document object for the document |
||
| 509 | * number $id in this index. |
||
| 510 | * |
||
| 511 | * @param integer|Zend_Search_Lucene_Search_QueryHit $id |
||
| 512 | * @return Zend_Search_Lucene_Document |
||
| 513 | * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range |
||
| 514 | */ |
||
| 515 | public function getDocument($id) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Returns true if index contain documents with specified term. |
||
| 538 | * |
||
| 539 | * Is used for query optimization. |
||
| 540 | * |
||
| 541 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 542 | * @return boolean |
||
| 543 | */ |
||
| 544 | public function hasTerm(Zend_Search_Lucene_Index_Term $term) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns IDs of all the documents containing term. |
||
| 557 | * |
||
| 558 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 559 | * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
||
| 560 | * @return array |
||
| 561 | * @throws Zend_Search_Lucene_Exception |
||
| 562 | */ |
||
| 563 | public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns documents filter for all documents containing term. |
||
| 591 | * |
||
| 592 | * It performs the same operation as termDocs, but return result as |
||
| 593 | * Zend_Search_Lucene_Index_DocsFilter object |
||
| 594 | * |
||
| 595 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 596 | * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
||
| 597 | * @return Zend_Search_Lucene_Index_DocsFilter |
||
| 598 | * @throws Zend_Search_Lucene_Exception |
||
| 599 | */ |
||
| 600 | public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Returns an array of all term freqs. |
||
| 608 | * Return array structure: array( docId => freq, ...) |
||
| 609 | * |
||
| 610 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 611 | * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
||
| 612 | * @return integer |
||
| 613 | * @throws Zend_Search_Lucene_Exception |
||
| 614 | */ |
||
| 615 | public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Returns an array of all term positions in the documents. |
||
| 646 | * Return array structure: array( docId => array( pos1, pos2, ...), ...) |
||
| 647 | * |
||
| 648 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 649 | * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter |
||
| 650 | * @return array |
||
| 651 | * @throws Zend_Search_Lucene_Exception |
||
| 652 | */ |
||
| 653 | public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Returns the number of documents in this index containing the $term. |
||
| 684 | * |
||
| 685 | * @param Zend_Search_Lucene_Index_Term $term |
||
| 686 | * @return integer |
||
| 687 | */ |
||
| 688 | public function docFreq(Zend_Search_Lucene_Index_Term $term) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Retrive similarity used by index reader |
||
| 701 | * |
||
| 702 | * @return Zend_Search_Lucene_Search_Similarity |
||
| 703 | * @throws Zend_Search_Lucene_Exception |
||
| 704 | */ |
||
| 705 | public function getSimilarity() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Returns a normalization factor for "field, document" pair. |
||
| 726 | * |
||
| 727 | * @param integer $id |
||
| 728 | * @param string $fieldName |
||
| 729 | * @return float |
||
| 730 | */ |
||
| 731 | public function norm($id, $fieldName) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Returns true if any documents have been deleted from this index. |
||
| 748 | * |
||
| 749 | * @return boolean |
||
| 750 | */ |
||
| 751 | public function hasDeletions() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Deletes a document from the index. |
||
| 764 | * $id is an internal document id |
||
| 765 | * |
||
| 766 | * @param integer|Zend_Search_Lucene_Search_QueryHit $id |
||
| 767 | * @throws Zend_Search_Lucene_Exception |
||
| 768 | */ |
||
| 769 | public function delete($id) |
||
| 785 | |||
| 786 | |||
| 787 | /** |
||
| 788 | * Callback used to choose target index for new documents |
||
| 789 | * |
||
| 790 | * Function/method signature: |
||
| 791 | * Zend_Search_Lucene_Interface callbackFunction(Zend_Search_Lucene_Document $document, array $indices); |
||
| 792 | * |
||
| 793 | * null means "default documents distributing algorithm" |
||
| 794 | * |
||
| 795 | * @var callback |
||
| 796 | */ |
||
| 797 | protected $_documentDistributorCallBack = null; |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Set callback for choosing target index. |
||
| 801 | * |
||
| 802 | * @param callback $callback |
||
| 803 | * @throws Zend_Search_Lucene_Exception |
||
| 804 | */ |
||
| 805 | public function setDocumentDistributorCallback($callback) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Get callback for choosing target index. |
||
| 817 | * |
||
| 818 | * @return callback |
||
| 819 | */ |
||
| 820 | public function getDocumentDistributorCallback() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Adds a document to this index. |
||
| 827 | * |
||
| 828 | * @param Zend_Search_Lucene_Document $document |
||
| 829 | * @throws Zend_Search_Lucene_Exception |
||
| 830 | */ |
||
| 831 | public function addDocument(Zend_Search_Lucene_Document $document) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Commit changes resulting from delete() or undeleteAll() operations. |
||
| 844 | */ |
||
| 845 | public function commit() |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Optimize index. |
||
| 854 | * |
||
| 855 | * Merges all segments into one |
||
| 856 | */ |
||
| 857 | public function optimize() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Returns an array of all terms in this index. |
||
| 866 | * |
||
| 867 | * @return array |
||
| 868 | */ |
||
| 869 | public function terms() |
||
| 879 | |||
| 880 | |||
| 881 | /** |
||
| 882 | * Terms stream priority queue object |
||
| 883 | * |
||
| 884 | * @var Zend_Search_Lucene_TermStreamsPriorityQueue |
||
| 885 | */ |
||
| 886 | private $_termsStream = null; |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Reset terms stream. |
||
| 890 | */ |
||
| 891 | public function resetTermsStream() |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Skip terms stream up to specified term preffix. |
||
| 905 | * |
||
| 906 | * Prefix contains fully specified field info and portion of searched term |
||
| 907 | * |
||
| 908 | * @param Zend_Search_Lucene_Index_Term $prefix |
||
| 909 | */ |
||
| 910 | public function skipTo(Zend_Search_Lucene_Index_Term $prefix) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Scans terms dictionary and returns next term |
||
| 917 | * |
||
| 918 | * @return Zend_Search_Lucene_Index_Term|null |
||
| 919 | */ |
||
| 920 | public function nextTerm() |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Returns term in current position |
||
| 927 | * |
||
| 928 | * @return Zend_Search_Lucene_Index_Term|null |
||
| 929 | */ |
||
| 930 | public function currentTerm() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Close terms stream |
||
| 937 | * |
||
| 938 | * Should be used for resources clean up if stream is not read up to the end |
||
| 939 | */ |
||
| 940 | public function closeTermsStream() |
||
| 945 | |||
| 946 | |||
| 947 | /** |
||
| 948 | * Undeletes all documents currently marked as deleted in this index. |
||
| 949 | */ |
||
| 950 | public function undeleteAll() |
||
| 956 | |||
| 957 | |||
| 958 | /** |
||
| 959 | * Add reference to the index object |
||
| 960 | * |
||
| 961 | * @internal |
||
| 962 | */ |
||
| 963 | public function addReference() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Remove reference from the index object |
||
| 970 | * |
||
| 971 | * When reference count becomes zero, index is closed and resources are cleaned up |
||
| 972 | * |
||
| 973 | * @internal |
||
| 974 | */ |
||
| 975 | public function removeReference() |
||
| 979 | } |
||
| 980 | |||
| 993 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.