| Conditions | 7 |
| Paths | 10 |
| Total Lines | 70 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 35 | public function search( |
||
| 36 | string $queryString, |
||
| 37 | int $offset = 0, |
||
| 38 | int $length = 10, |
||
| 39 | array $extra = [], // Kept for signature compatibility but unused in the demo |
||
| 40 | int $countType = 0, // Kept for signature compatibility but unused in the demo |
||
| 41 | ): array { |
||
| 42 | if (!\class_exists(\XapianDatabase::class)) { |
||
| 43 | throw new RuntimeException('Xapian PHP extension is not loaded.'); |
||
| 44 | } |
||
| 45 | |||
| 46 | $indexDir = $this->indexPathResolver->getIndexDir(); |
||
| 47 | $this->indexPathResolver->ensureIndexDirectoryExists(); |
||
| 48 | |||
| 49 | try { |
||
| 50 | $db = new \XapianDatabase($indexDir); |
||
| 51 | } catch (\Throwable $e) { |
||
| 52 | throw new RuntimeException( |
||
| 53 | \sprintf('Unable to open Xapian database at "%s": %s', $indexDir, $e->getMessage()), |
||
| 54 | 0, |
||
| 55 | $e |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $enquire = new \XapianEnquire($db); |
||
| 60 | |||
| 61 | if ($queryString !== '') { |
||
| 62 | // Free-text query with stemming |
||
| 63 | $queryParser = new \XapianQueryParser(); |
||
| 64 | $stemmer = new \XapianStem('english'); // TODO: pick language from user/platform |
||
| 65 | |||
| 66 | $queryParser->set_stemmer($stemmer); |
||
| 67 | $queryParser->set_database($db); |
||
| 68 | $queryParser->set_stemming_strategy(\XapianQueryParser::STEM_SOME); |
||
| 69 | |||
| 70 | $parsedQuery = $queryParser->parse_query($queryString); |
||
| 71 | $query = $parsedQuery; |
||
| 72 | } else { |
||
| 73 | // Empty query = match everything |
||
| 74 | $query = new \XapianQuery(''); |
||
| 75 | } |
||
| 76 | |||
| 77 | $enquire->set_query($query); |
||
| 78 | |||
| 79 | $matches = $enquire->get_mset($offset, $length); |
||
| 80 | |||
| 81 | $results = []; |
||
| 82 | |||
| 83 | for ($m = $matches->begin(); !$m->equals($matches->end()); $m->next()) { |
||
| 84 | $document = $m->get_document(); |
||
| 85 | |||
| 86 | if (!$document instanceof \XapianDocument) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $rawData = $document->get_data(); |
||
| 91 | $data = $rawData !== '' ? @\unserialize($rawData) : null; |
||
| 92 | |||
| 93 | $results[] = [ |
||
| 94 | 'doc_id' => $m->get_docid(), |
||
| 95 | 'score' => $m->get_percent(), |
||
| 96 | 'data' => $data, |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | $count = $matches->get_matches_estimated(); |
||
| 101 | |||
| 102 | return [ |
||
| 103 | 'count' => $count, |
||
| 104 | 'results' => $results, |
||
| 105 | ]; |
||
| 108 |