| Conditions | 15 |
| Paths | 289 |
| Total Lines | 93 |
| Code Lines | 50 |
| 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 |
||
| 68 | public function indexDocument( |
||
| 69 | array $fields, |
||
| 70 | array $terms = [], |
||
| 71 | ?string $language = null, |
||
| 72 | array $fieldValuesByCode = [] // e.g. ['t'=>'...', 'd'=>'...', 'k'=>'...'] |
||
| 73 | ): int { |
||
| 74 | if (!class_exists(XapianWritableDatabase::class)) { |
||
| 75 | throw new RuntimeException('Xapian PHP extension is not loaded.'); |
||
| 76 | } |
||
| 77 | |||
| 78 | $db = $this->openWritableDatabase(); |
||
| 79 | |||
| 80 | $doc = new XapianDocument(); |
||
| 81 | $termGen = new XapianTermGenerator(); |
||
| 82 | |||
| 83 | // normalize ISO/code into a Xapian stemmer language string |
||
| 84 | $xapianLanguage = $this->mapLanguageToXapianStemmer($language); |
||
| 85 | |||
| 86 | try { |
||
| 87 | $stemmer = new XapianStem($xapianLanguage); |
||
| 88 | } catch (Throwable $e) { |
||
| 89 | error_log( |
||
| 90 | '[Xapian] indexDocument: failed to init stemmer for lang=' |
||
| 91 | .var_export($xapianLanguage, true) |
||
| 92 | .', fallback=english, error='.$e->getMessage() |
||
| 93 | ); |
||
| 94 | |||
| 95 | $stemmer = new XapianStem(self::DEFAULT_LANGUAGE); |
||
| 96 | } |
||
| 97 | |||
| 98 | $termGen->set_stemmer($stemmer); |
||
| 99 | $termGen->set_document($doc); |
||
| 100 | |||
| 101 | // Unprefixed free-text (general search) |
||
| 102 | foreach ($fields as $value) { |
||
| 103 | if (null === $value) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | $value = is_string($value) ? $value : (string) $value; |
||
| 107 | $value = trim($value); |
||
| 108 | if ('' === $value) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | $termGen->index_text($value, 1); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Prefixed dynamic fields: t:, d:, k:, etc. |
||
| 115 | if (!empty($fieldValuesByCode)) { |
||
| 116 | error_log('[Xapian] indexDocument: fieldValuesByCode='.json_encode(array_keys($fieldValuesByCode))); |
||
| 117 | |||
| 118 | foreach ($fieldValuesByCode as $code => $val) { |
||
| 119 | $code = strtolower(trim((string) $code)); |
||
| 120 | if ('' === $code) { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $val = is_string($val) ? $val : (string) $val; |
||
| 125 | $val = trim($val); |
||
| 126 | if ('' === $val) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Must match query parser convention: F + strtoupper(code) |
||
| 131 | $prefix = 'F'.strtoupper($code); |
||
| 132 | |||
| 133 | // This is what makes t: / d: / k: work |
||
| 134 | $termGen->index_text($val, 1, $prefix); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Optional: keep it in stored data for debugging |
||
| 138 | $fields['searchFieldValues'] = $fieldValuesByCode; |
||
| 139 | } |
||
| 140 | |||
| 141 | // Extra terms (Tdocument, Cxx, Sxx...) |
||
| 142 | foreach ($terms as $term) { |
||
| 143 | $term = (string) $term; |
||
| 144 | if ('' === $term) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | $doc->add_term($term, 1); |
||
| 148 | } |
||
| 149 | |||
| 150 | $doc->set_data(serialize($fields)); |
||
| 151 | |||
| 152 | try { |
||
| 153 | $docId = $db->add_document($doc); |
||
| 154 | $db->flush(); |
||
| 155 | |||
| 156 | error_log('[Xapian] indexDocument: added docId='.$docId); |
||
| 157 | |||
| 158 | return $docId; |
||
| 159 | } catch (Throwable $e) { |
||
| 160 | throw new RuntimeException(sprintf('Failed to index document in Xapian: %s', $e->getMessage()), 0, $e); |
||
| 161 | } |
||
| 256 |