| Conditions | 20 |
| Paths | 648 |
| Total Lines | 127 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 108 | private function checkManticoreIndex(string $index): void |
||
| 109 | { |
||
| 110 | try { |
||
| 111 | $indexName = $index === 'releases' ? 'releases_rt' : 'predb_rt'; |
||
| 112 | $host = config('nntmux.manticore.host', '127.0.0.1'); |
||
| 113 | $port = config('nntmux.manticore.port', 9308); |
||
| 114 | |||
| 115 | // Use HTTP API endpoint |
||
| 116 | $baseUrl = "http://{$host}:{$port}"; |
||
| 117 | |||
| 118 | // Check if table exists using HTTP API |
||
| 119 | $client = new \GuzzleHttp\Client([ |
||
| 120 | 'base_uri' => $baseUrl, |
||
| 121 | 'timeout' => 10, |
||
| 122 | 'http_errors' => false, // Don't throw on HTTP errors |
||
| 123 | ]); |
||
| 124 | |||
| 125 | // Use raw mode which seems to work |
||
| 126 | $query = "SELECT COUNT(*) FROM {$indexName}"; |
||
| 127 | |||
| 128 | $response = $client->post('/sql?mode=raw', [ |
||
| 129 | 'body' => $query, |
||
| 130 | 'headers' => [ |
||
| 131 | 'Content-Type' => 'text/plain', |
||
| 132 | ], |
||
| 133 | ]); |
||
| 134 | |||
| 135 | $statusCode = $response->getStatusCode(); |
||
| 136 | $body = $response->getBody()->getContents(); |
||
| 137 | |||
| 138 | if ($statusCode !== 200) { |
||
| 139 | $this->error("HTTP Status: {$statusCode}"); |
||
| 140 | |||
| 141 | // Check if it's a table not found error |
||
| 142 | if (strpos($body, 'no such table') !== false || |
||
| 143 | strpos($body, 'unknown table') !== false || |
||
| 144 | strpos($body, "doesn't exist") !== false) { |
||
| 145 | $this->error("ManticoreSearch table '{$indexName}' does not exist."); |
||
| 146 | |||
| 147 | return; |
||
| 148 | } |
||
| 149 | $this->error("Response: {$body}"); |
||
| 150 | |||
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Parse the JSON response |
||
| 155 | $jsonData = json_decode($body, true); |
||
| 156 | $docCount = 0; |
||
| 157 | |||
| 158 | if ($jsonData !== null && isset($jsonData['data'])) { |
||
| 159 | // ManticoreSearch returns data as array of arrays |
||
| 160 | $docCount = $jsonData['data'][0]['COUNT(*)'] ?? $jsonData['data'][0][0] ?? 0; |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->info("ManticoreSearch table '{$indexName}' exists."); |
||
| 164 | $this->info('Document count: '.number_format($docCount)); |
||
| 165 | |||
| 166 | // Get a sample document if there are any |
||
| 167 | if ($docCount > 0) { |
||
| 168 | $sampleQuery = "SELECT * FROM {$indexName} LIMIT 1"; |
||
| 169 | |||
| 170 | $sampleResponse = $client->post('/sql?mode=raw', [ |
||
| 171 | 'body' => $sampleQuery, |
||
| 172 | 'headers' => [ |
||
| 173 | 'Content-Type' => 'text/plain', |
||
| 174 | ], |
||
| 175 | ]); |
||
| 176 | |||
| 177 | if ($sampleResponse->getStatusCode() === 200) { |
||
| 178 | $sampleBody = $sampleResponse->getBody()->getContents(); |
||
| 179 | $sampleJson = json_decode($sampleBody, true); |
||
| 180 | |||
| 181 | if ($sampleJson !== null && isset($sampleJson['data'][0])) { |
||
| 182 | $this->info('Sample document:'); |
||
| 183 | |||
| 184 | $document = []; |
||
| 185 | $columns = $sampleJson['columns'] ?? []; |
||
| 186 | $data = $sampleJson['data'][0] ?? []; |
||
| 187 | |||
| 188 | foreach ($columns as $i => $column) { |
||
| 189 | // Extract column name from the nested structure |
||
| 190 | $columnName = array_keys($column)[0]; |
||
| 191 | $document[$columnName] = $data[$i] ?? null; |
||
| 192 | } |
||
| 193 | |||
| 194 | $this->info(json_encode($document, JSON_PRETTY_PRINT)); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // Get table structure |
||
| 200 | try { |
||
| 201 | $describeQuery = "DESCRIBE {$indexName}"; |
||
| 202 | $describeResponse = $client->post('/sql?mode=raw', [ |
||
| 203 | 'body' => $describeQuery, |
||
| 204 | 'headers' => [ |
||
| 205 | 'Content-Type' => 'text/plain', |
||
| 206 | ], |
||
| 207 | ]); |
||
| 208 | |||
| 209 | if ($describeResponse->getStatusCode() === 200) { |
||
| 210 | $describeBody = $describeResponse->getBody()->getContents(); |
||
| 211 | $describeJson = json_decode($describeBody, true); |
||
| 212 | |||
| 213 | if ($describeJson !== null && isset($describeJson['data'])) { |
||
| 214 | $this->info("\nTable structure:"); |
||
| 215 | $this->table( |
||
| 216 | ['Field', 'Type', 'Properties'], |
||
| 217 | array_map(function ($row) { |
||
| 218 | return [ |
||
| 219 | $row['Field'] ?? $row[0] ?? '', |
||
| 220 | $row['Type'] ?? $row[1] ?? '', |
||
| 221 | $row['Properties'] ?? $row[2] ?? '', |
||
| 222 | ]; |
||
| 223 | }, $describeJson['data']) |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } catch (\Exception $e) { |
||
| 228 | // Ignore describe errors |
||
| 229 | } |
||
| 230 | |||
| 231 | } catch (\Exception $e) { |
||
| 232 | $this->error("Error checking ManticoreSearch table: {$e->getMessage()}"); |
||
| 233 | if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { |
||
| 234 | $this->error('Response body: '.$e->getResponse()->getBody()); |
||
| 235 | } |
||
| 270 |