| Conditions | 23 |
| Paths | 122 |
| Total Lines | 112 |
| Code Lines | 70 |
| 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 |
||
| 235 | private function consolidateOne(Connection $conn, $accessUrl, string $category, string $variable): void |
||
| 236 | { |
||
| 237 | // Build SELECT based on nullability of access_url |
||
| 238 | if (null === $accessUrl) { |
||
| 239 | $items = $conn->fetchAllAssociative( |
||
| 240 | 'SELECT id, selected_value, subkey |
||
| 241 | FROM settings |
||
| 242 | WHERE access_url IS NULL AND category = ? AND variable = ? |
||
| 243 | ORDER BY id ASC', |
||
| 244 | [$category, $variable] |
||
| 245 | ); |
||
| 246 | } else { |
||
| 247 | $items = $conn->fetchAllAssociative( |
||
| 248 | 'SELECT id, selected_value, subkey |
||
| 249 | FROM settings |
||
| 250 | WHERE access_url = ? AND category = ? AND variable = ? |
||
| 251 | ORDER BY id ASC', |
||
| 252 | [$accessUrl, $category, $variable] |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | if (empty($items)) { |
||
| 257 | $this->dbg("[SKIP] No rows for variable='{$variable}' category='{$category}' access_url=". |
||
| 258 | (null === $accessUrl ? 'NULL' : (string) $accessUrl)); |
||
| 259 | |||
| 260 | return; |
||
| 261 | } |
||
| 262 | |||
| 263 | $this->dbg("[WORK] Processing variable='{$variable}' category='{$category}' access_url=". |
||
| 264 | (null === $accessUrl ? 'NULL' : (string) $accessUrl).' (row_count='.\count($items).')'); |
||
| 265 | |||
| 266 | // Determine if there is at least one subkey among rows |
||
| 267 | $hasSubkey = false; |
||
| 268 | foreach ($items as $it) { |
||
| 269 | if (!empty($it['subkey'])) { |
||
| 270 | $hasSubkey = true; |
||
| 271 | |||
| 272 | break; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | // Build final token set (preserve first-seen order) |
||
| 277 | $enabled = []; |
||
| 278 | |||
| 279 | if ($hasSubkey) { |
||
| 280 | // Multi-row with subkeys: include subkey when value is truthy OR equal to subkey |
||
| 281 | foreach ($items as $it) { |
||
| 282 | $id = (int) $it['id']; |
||
| 283 | $rawVal = (string) ($it['selected_value'] ?? ''); |
||
| 284 | $subkey = $it['subkey'] ?? null; |
||
| 285 | |||
| 286 | $clean = str_replace(['[', ']', '"', "'", ' '], '', $rawVal); |
||
| 287 | $this->dbg("[TOKENIZE] id={$id} subkey=". |
||
| 288 | (null === $subkey ? 'NULL' : "'".$subkey."'"). |
||
| 289 | " raw='{$rawVal}' cleaned='{$clean}'"); |
||
| 290 | |||
| 291 | if (null !== $subkey && '' !== $subkey) { |
||
| 292 | $v = strtolower(trim($rawVal)); |
||
| 293 | if ($this->isTruthy($v) || $v === strtolower($subkey)) { |
||
| 294 | if (!isset($enabled[$subkey])) { |
||
| 295 | $enabled[$subkey] = true; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } else { |
||
| 299 | // Defensive: a row without subkey inside a subkey group -> parse as CSV |
||
| 300 | foreach ($this->splitCsvTokens($clean) as $tok) { |
||
| 301 | if (!isset($enabled[$tok])) { |
||
| 302 | $enabled[$tok] = true; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | // No subkeys at all: merge CSV tokens from all rows |
||
| 309 | foreach ($items as $it) { |
||
| 310 | $id = (int) $it['id']; |
||
| 311 | $rawVal = (string) ($it['selected_value'] ?? ''); |
||
| 312 | $clean = str_replace(['[', ']', '"', "'", ' '], '', $rawVal); |
||
| 313 | |||
| 314 | $this->dbg("[TOKENIZE] id={$id} subkey=NULL raw='{$rawVal}' cleaned='{$clean}'"); |
||
| 315 | |||
| 316 | foreach ($this->splitCsvTokens($clean) as $tok) { |
||
| 317 | if (!isset($enabled[$tok])) { |
||
| 318 | $enabled[$tok] = true; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | $finalTokens = array_keys($enabled); |
||
| 325 | $csv = implode(',', $finalTokens); |
||
| 326 | $keepId = (int) $items[0]['id']; |
||
| 327 | |||
| 328 | $this->dbg("[UPDATE] KEEP id={$keepId} variable='{$variable}' category='{$category}' access_url=". |
||
| 329 | (null === $accessUrl ? 'NULL' : (string) $accessUrl)." final_csv='{$csv}'"); |
||
| 330 | |||
| 331 | $conn->executeStatement( |
||
| 332 | 'UPDATE settings SET selected_value = ? WHERE id = ?', |
||
| 333 | [$csv, $keepId] |
||
| 334 | ); |
||
| 335 | |||
| 336 | // Delete the rest |
||
| 337 | $idsToDelete = array_map(static fn ($it) => (int) $it['id'], \array_slice($items, 1)); |
||
| 338 | if ($idsToDelete) { |
||
| 339 | $this->dbg("[DELETE] variable='{$variable}' category='{$category}' access_url=". |
||
| 340 | (null === $accessUrl ? 'NULL' : (string) $accessUrl).' deleting_ids='. |
||
| 341 | json_encode($idsToDelete)); |
||
| 342 | |||
| 343 | $in = implode(',', array_fill(0, \count($idsToDelete), '?')); |
||
| 344 | $conn->executeStatement("DELETE FROM settings WHERE id IN ($in)", $idsToDelete); |
||
| 345 | } else { |
||
| 346 | $this->dbg('[KEEPONLY] Only one row existed; nothing deleted.'); |
||
| 347 | } |
||
| 375 |
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.