| Conditions | 23 |
| Paths | 122 |
| Total Lines | 101 |
| Code Lines | 65 |
| 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 |
||
| 225 | private function consolidateOne(Connection $conn, $accessUrl, string $category, string $variable): void |
||
| 226 | { |
||
| 227 | // Build SELECT based on nullability of access_url |
||
| 228 | if ($accessUrl === null) { |
||
| 229 | $items = $conn->fetchAllAssociative( |
||
| 230 | "SELECT id, selected_value, subkey |
||
| 231 | FROM settings |
||
| 232 | WHERE access_url IS NULL AND category = ? AND variable = ? |
||
| 233 | ORDER BY id ASC", |
||
| 234 | [$category, $variable] |
||
| 235 | ); |
||
| 236 | } else { |
||
| 237 | $items = $conn->fetchAllAssociative( |
||
| 238 | "SELECT id, selected_value, subkey |
||
| 239 | FROM settings |
||
| 240 | WHERE access_url = ? AND category = ? AND variable = ? |
||
| 241 | ORDER BY id ASC", |
||
| 242 | [$accessUrl, $category, $variable] |
||
| 243 | ); |
||
| 244 | } |
||
| 245 | |||
| 246 | if (empty($items)) { |
||
| 247 | $this->dbg("[SKIP] No rows for variable='{$variable}' category='{$category}' access_url=". |
||
| 248 | ($accessUrl === null ? 'NULL' : (string) $accessUrl)); |
||
| 249 | return; |
||
| 250 | } |
||
| 251 | |||
| 252 | $this->dbg("[WORK] Processing variable='{$variable}' category='{$category}' access_url=". |
||
| 253 | ($accessUrl === null ? 'NULL' : (string) $accessUrl)." (row_count=".count($items).')'); |
||
| 254 | |||
| 255 | // Determine if there is at least one subkey among rows |
||
| 256 | $hasSubkey = false; |
||
| 257 | foreach ($items as $it) { |
||
| 258 | if (!empty($it['subkey'])) { $hasSubkey = true; break; } |
||
| 259 | } |
||
| 260 | |||
| 261 | // Build final token set (preserve first-seen order) |
||
| 262 | $enabled = []; |
||
| 263 | |||
| 264 | if ($hasSubkey) { |
||
| 265 | // Multi-row with subkeys: include subkey when value is truthy OR equal to subkey |
||
| 266 | foreach ($items as $it) { |
||
| 267 | $id = (int) $it['id']; |
||
| 268 | $rawVal = (string) ($it['selected_value'] ?? ''); |
||
| 269 | $subkey = $it['subkey'] ?? null; |
||
| 270 | |||
| 271 | $clean = str_replace(['[',']','"',"'", ' '], '', $rawVal); |
||
| 272 | $this->dbg("[TOKENIZE] id={$id} subkey=". |
||
| 273 | ($subkey === null ? 'NULL' : "'".$subkey."'"). |
||
| 274 | " raw='{$rawVal}' cleaned='{$clean}'"); |
||
| 275 | |||
| 276 | if ($subkey !== null && $subkey !== '') { |
||
| 277 | $v = strtolower(trim($rawVal)); |
||
| 278 | if ($this->isTruthy($v) || $v === strtolower($subkey)) { |
||
| 279 | if (!isset($enabled[$subkey])) { $enabled[$subkey] = true; } |
||
| 280 | } |
||
| 281 | } else { |
||
| 282 | // Defensive: a row without subkey inside a subkey group -> parse as CSV |
||
| 283 | foreach ($this->splitCsvTokens($clean) as $tok) { |
||
| 284 | if (!isset($enabled[$tok])) { $enabled[$tok] = true; } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | // No subkeys at all: merge CSV tokens from all rows |
||
| 290 | foreach ($items as $it) { |
||
| 291 | $id = (int) $it['id']; |
||
| 292 | $rawVal = (string) ($it['selected_value'] ?? ''); |
||
| 293 | $clean = str_replace(['[',']','"',"'", ' '], '', $rawVal); |
||
| 294 | |||
| 295 | $this->dbg("[TOKENIZE] id={$id} subkey=NULL raw='{$rawVal}' cleaned='{$clean}'"); |
||
| 296 | |||
| 297 | foreach ($this->splitCsvTokens($clean) as $tok) { |
||
| 298 | if (!isset($enabled[$tok])) { $enabled[$tok] = true; } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | $finalTokens = array_keys($enabled); |
||
| 304 | $csv = implode(',', $finalTokens); |
||
| 305 | $keepId = (int) $items[0]['id']; |
||
| 306 | |||
| 307 | $this->dbg("[UPDATE] KEEP id={$keepId} variable='{$variable}' category='{$category}' access_url=". |
||
| 308 | ($accessUrl === null ? 'NULL' : (string) $accessUrl)." final_csv='{$csv}'"); |
||
| 309 | |||
| 310 | $conn->executeStatement( |
||
| 311 | "UPDATE settings SET selected_value = ? WHERE id = ?", |
||
| 312 | [$csv, $keepId] |
||
| 313 | ); |
||
| 314 | |||
| 315 | // Delete the rest |
||
| 316 | $idsToDelete = array_map(static fn($it) => (int) $it['id'], array_slice($items, 1)); |
||
| 317 | if ($idsToDelete) { |
||
| 318 | $this->dbg("[DELETE] variable='{$variable}' category='{$category}' access_url=". |
||
| 319 | ($accessUrl === null ? 'NULL' : (string) $accessUrl).' deleting_ids='. |
||
| 320 | json_encode($idsToDelete)); |
||
| 321 | |||
| 322 | $in = implode(',', array_fill(0, count($idsToDelete), '?')); |
||
| 323 | $conn->executeStatement("DELETE FROM settings WHERE id IN ($in)", $idsToDelete); |
||
| 324 | } else { |
||
| 325 | $this->dbg("[KEEPONLY] Only one row existed; nothing deleted."); |
||
| 326 | } |
||
| 350 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.