| Conditions | 20 |
| Paths | 102 |
| Total Lines | 106 |
| Code Lines | 73 |
| 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 |
||
| 306 | protected function updateReleaseFromPredb($release, $predb, string $matchType, bool $dryRun, bool $show, Categorize $categorize): bool |
||
| 307 | { |
||
| 308 | // Get old category name |
||
| 309 | $oldCategory = Category::query()->where('id', $release->categories_id)->first(); |
||
| 310 | $oldCategoryName = $oldCategory ? $oldCategory->title : "Unknown (ID: {$release->categories_id})"; |
||
| 311 | |||
| 312 | if ($release->searchname === $predb->title) { |
||
| 313 | // Names already match, just update predb_id |
||
| 314 | if (! $dryRun) { |
||
| 315 | Release::where('id', $release->id)->update(['predb_id' => $predb->id]); |
||
| 316 | } |
||
| 317 | |||
| 318 | if ($show) { |
||
| 319 | $this->colorCLI->primary('═══════════════════════════════════════════════════════════'); |
||
| 320 | $this->colorCLI->header("Release ID: {$release->id}"); |
||
| 321 | $this->colorCLI->primary("GUID: {$release->guid}"); |
||
| 322 | $this->colorCLI->info("Match Type: {$matchType}"); |
||
| 323 | $this->colorCLI->info("Searchname: {$release->searchname}"); |
||
| 324 | $this->colorCLI->info("Category: {$oldCategoryName}"); |
||
| 325 | $this->colorCLI->info("PreDB Title: {$predb->title}"); |
||
| 326 | $this->colorCLI->info("PreDB Source: {$predb->source}"); |
||
| 327 | $this->colorCLI->warning('Action: Same name, only updating predb_id'); |
||
| 328 | if ($dryRun) { |
||
| 329 | $this->colorCLI->info('[DRY RUN - Not actually updated]'); |
||
| 330 | } |
||
| 331 | $this->colorCLI->primary('═══════════════════════════════════════════════════════════'); |
||
| 332 | echo PHP_EOL; |
||
| 333 | } |
||
| 334 | |||
| 335 | return true; |
||
| 336 | } |
||
| 337 | |||
| 338 | // Names differ, perform full rename |
||
| 339 | $oldName = $release->name; |
||
| 340 | $oldSearchName = $release->searchname; |
||
| 341 | $newName = $predb->title; |
||
| 342 | $newCategory = null; |
||
| 343 | $newCategoryName = $oldCategoryName; |
||
| 344 | |||
| 345 | if (! $dryRun) { |
||
| 346 | // Update release |
||
| 347 | Release::where('id', $release->id)->update([ |
||
| 348 | 'name' => $newName, |
||
| 349 | 'searchname' => $newName, |
||
| 350 | 'isrenamed' => 1, |
||
| 351 | 'predb_id' => $predb->id, |
||
| 352 | ]); |
||
| 353 | |||
| 354 | // Recategorize if needed |
||
| 355 | $newCategory = $categorize->determineCategory($release->groups_id, $newName); |
||
| 356 | if ($newCategory !== null && $newCategory !== $release->categories_id) { |
||
| 357 | Release::where('id', $release->id)->update(['categories_id' => $newCategory]); |
||
| 358 | $newCategoryObj = Category::query()->where('id', $newCategory)->first(); |
||
| 359 | if ($newCategoryObj && isset($newCategoryObj->title)) { |
||
| 360 | $newCategoryName = $newCategoryObj->title; |
||
| 361 | } else { |
||
| 362 | $newCategoryName = "Unknown (ID: {$newCategory})"; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | // Update search indexes |
||
| 367 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
| 368 | (new ElasticSearchSiteSearch)->updateRelease($release->id); |
||
| 369 | } else { |
||
| 370 | (new ManticoreSearch)->updateRelease($release->id); |
||
| 371 | } |
||
| 372 | } else { |
||
| 373 | // Dry run: calculate what the new category would be |
||
| 374 | $newCategory = $categorize->determineCategory($release->groups_id, $newName); |
||
| 375 | if ($newCategory !== null && $newCategory !== $release->categories_id) { |
||
| 376 | $newCategoryObj = Category::query()->where('id', $newCategory)->first(); |
||
| 377 | if ($newCategoryObj && isset($newCategoryObj->title)) { |
||
| 378 | $newCategoryName = $newCategoryObj->title; |
||
| 379 | } else { |
||
| 380 | $newCategoryName = "Unknown (ID: {$newCategory})"; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | $this->renamed++; |
||
| 386 | |||
| 387 | if ($show) { |
||
| 388 | $this->colorCLI->primary('═══════════════════════════════════════════════════════════'); |
||
| 389 | $this->colorCLI->header("Release ID: {$release->id}"); |
||
| 390 | $this->colorCLI->primary("GUID: {$release->guid}"); |
||
| 391 | $this->colorCLI->info("Match Type: {$matchType}"); |
||
| 392 | echo PHP_EOL; |
||
| 393 | $this->colorCLI->warning("OLD Searchname: {$oldSearchName}"); |
||
| 394 | $this->colorCLI->warning("OLD Category: {$oldCategoryName}"); |
||
| 395 | echo PHP_EOL; |
||
| 396 | $this->colorCLI->header("NEW Searchname: {$newName}"); |
||
| 397 | if ($newCategory !== null && $newCategory !== $release->categories_id) { |
||
| 398 | $this->colorCLI->header("NEW Category: {$newCategoryName}"); |
||
| 399 | } else { |
||
| 400 | $this->colorCLI->info("NEW Category: {$newCategoryName} (unchanged)"); |
||
| 401 | } |
||
| 402 | echo PHP_EOL; |
||
| 403 | $this->colorCLI->info("PreDB Source: {$predb->source}"); |
||
| 404 | if ($dryRun) { |
||
| 405 | $this->colorCLI->info('[DRY RUN - Not actually updated]'); |
||
| 406 | } |
||
| 407 | $this->colorCLI->primary('═══════════════════════════════════════════════════════════'); |
||
| 408 | echo PHP_EOL; |
||
| 409 | } |
||
| 410 | |||
| 411 | return true; |
||
| 412 | } |
||
| 431 |