| Conditions | 13 |
| Paths | 54 |
| Total Lines | 99 |
| Code Lines | 54 |
| 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 |
||
| 42 | public function search( |
||
| 43 | string $queryString, |
||
| 44 | int $offset = 0, |
||
| 45 | int $length = 10, |
||
| 46 | array $extra = [], |
||
| 47 | int $countType = 0, |
||
| 48 | ): array { |
||
| 49 | if (!class_exists(XapianDatabase::class)) { |
||
| 50 | throw new RuntimeException('Xapian PHP extension is not loaded.'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $indexDir = $this->indexPathResolver->getIndexDir(); |
||
| 54 | $this->indexPathResolver->ensureIndexDirectoryExists(); |
||
| 55 | |||
| 56 | try { |
||
| 57 | $db = new XapianDatabase($indexDir); |
||
| 58 | } catch (Throwable $e) { |
||
| 59 | throw new RuntimeException( |
||
| 60 | sprintf('Unable to open Xapian database at "%s": %s', $indexDir, $e->getMessage()), |
||
| 61 | 0, |
||
| 62 | $e |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | $enquire = new XapianEnquire($db); |
||
| 67 | |||
| 68 | if ('' !== trim($queryString)) { |
||
| 69 | $queryParser = new XapianQueryParser(); |
||
| 70 | |||
| 71 | // Resolve language for stemming. Caller can pass: |
||
| 72 | // - $extra['language'] or $extra['language_iso'] (e.g. "fr_61", "fr_FR", "fr", "french") |
||
| 73 | // - $extra['locale'] (e.g. "es_PE") |
||
| 74 | $languageRaw = null; |
||
| 75 | foreach (['language', 'language_iso', 'locale'] as $k) { |
||
| 76 | if (isset($extra[$k]) && is_string($extra[$k]) && '' !== trim($extra[$k])) { |
||
| 77 | $languageRaw = trim((string) $extra[$k]); |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | // Normalize ISO/code into a Xapian stemmer language string (fallback to english). |
||
| 83 | $xapianLanguage = $this->mapLanguageToXapianStemmer($languageRaw); |
||
| 84 | |||
| 85 | $usedLanguage = $xapianLanguage; |
||
| 86 | |||
| 87 | try { |
||
| 88 | $stemmer = new XapianStem($xapianLanguage); |
||
| 89 | } catch (Throwable $e) { |
||
| 90 | $usedLanguage = 'english'; |
||
| 91 | $stemmer = new XapianStem($usedLanguage); |
||
| 92 | } |
||
| 93 | |||
| 94 | $queryParser->set_stemmer($stemmer); |
||
| 95 | $queryParser->set_database($db); |
||
| 96 | $queryParser->set_stemming_strategy(XapianQueryParser::STEM_SOME); |
||
| 97 | |||
| 98 | // Dynamic prefixes (t:, d:, k:, etc) |
||
| 99 | $this->configureDynamicFieldPrefixes($queryParser); |
||
| 100 | |||
| 101 | // IMPORTANT: make parsing consistent (phrases, boolean ops, etc) |
||
| 102 | $flags = $this->buildQueryParserFlags(); |
||
| 103 | |||
| 104 | try { |
||
| 105 | $query = $queryParser->parse_query($queryString, $flags); |
||
| 106 | } catch (Throwable $e) { |
||
| 107 | // Safe fallback: do not crash search endpoint on malformed queries. |
||
| 108 | error_log('[Xapian] XapianSearchService::search: parse_query failed: '.$e->getMessage()); |
||
| 109 | $query = new XapianQuery(''); |
||
| 110 | } |
||
| 111 | } else { |
||
| 112 | $query = new XapianQuery(''); |
||
| 113 | } |
||
| 114 | |||
| 115 | $enquire->set_query($query); |
||
| 116 | |||
| 117 | $matches = $enquire->get_mset($offset, $length); |
||
| 118 | |||
| 119 | $results = []; |
||
| 120 | for ($m = $matches->begin(); !$m->equals($matches->end()); $m->next()) { |
||
| 121 | $document = $m->get_document(); |
||
| 122 | if (!$document instanceof XapianDocument) { |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | |||
| 126 | $rawData = $document->get_data(); |
||
| 127 | $data = '' !== $rawData ? @unserialize($rawData) : null; |
||
| 128 | |||
| 129 | $results[] = [ |
||
| 130 | 'doc_id' => $m->get_docid(), |
||
| 131 | 'score' => $m->get_percent(), |
||
| 132 | 'data' => $data, |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | $count = $matches->get_matches_estimated(); |
||
| 137 | |||
| 138 | return [ |
||
| 139 | 'count' => $count, |
||
| 140 | 'results' => $results, |
||
| 141 | ]; |
||
| 267 |