| Conditions | 11 |
| Paths | 48 |
| Total Lines | 87 |
| Code Lines | 57 |
| 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 |
||
| 16 | public function handle() |
||
| 17 | { |
||
| 18 | $threshold = $this->option('threshold'); // Percentage difference threshold |
||
| 19 | $limit = $this->option('limit'); |
||
| 20 | $checkSeasonPack = $this->option('season-pack'); |
||
| 21 | $direction = $this->option('direction'); |
||
| 22 | $shouldRename = $this->option('rename'); |
||
| 23 | $nameFixer = new NameFixer; |
||
| 24 | |||
| 25 | $query = Release::query() |
||
| 26 | ->select([ |
||
| 27 | 'releases.id', |
||
| 28 | 'releases.searchname', |
||
| 29 | 'releases.name', |
||
| 30 | 'releases.groups_id', |
||
| 31 | 'releases.categories_id', |
||
| 32 | DB::raw('releases.size / POW(1024, 3) as release_size'), |
||
| 33 | DB::raw('SUM(release_files.size) / POW(1024, 3) as files_total_size'), |
||
| 34 | DB::raw('(releases.size - SUM(release_files.size)) / POW(1024, 3) as size_diff'), |
||
| 35 | DB::raw('((releases.size - SUM(release_files.size)) / releases.size * 100) as diff_percent'), |
||
| 36 | ]) |
||
| 37 | ->join('release_files', 'releases.id', '=', 'release_files.releases_id') |
||
| 38 | ->where('releases.searchname', 'REGEXP', 'S[0-9]{1,3}E[0-9]{1,3}') |
||
| 39 | ->groupBy('releases.id'); |
||
| 40 | |||
| 41 | // Apply direction filter |
||
| 42 | if ($direction === 'bigger') { |
||
| 43 | $query->having('size_diff', '>', 0) |
||
| 44 | ->having('diff_percent', '>', $threshold); |
||
| 45 | } elseif ($direction === 'smaller') { |
||
| 46 | $query->having('size_diff', '<', 0) |
||
| 47 | ->having('diff_percent', '<', -$threshold); |
||
| 48 | } else { |
||
| 49 | $query->having(DB::raw('ABS(diff_percent)'), '>', $threshold); |
||
| 50 | } |
||
| 51 | |||
| 52 | // Order by ID if renaming, otherwise by diff_percent |
||
| 53 | $query->orderBy($shouldRename ? 'releases.id' : 'diff_percent', $shouldRename ? 'asc' : 'desc'); |
||
|
|
|||
| 54 | |||
| 55 | if ($limit > 0) { |
||
| 56 | $query->limit($limit); |
||
| 57 | } |
||
| 58 | |||
| 59 | $mismatches = $query->get(); |
||
| 60 | |||
| 61 | if ($checkSeasonPack) { |
||
| 62 | $mismatches = $mismatches->filter(function ($release) use ($nameFixer) { |
||
| 63 | return $nameFixer->isSeasonPack($release->name); |
||
| 64 | }); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($mismatches->isEmpty()) { |
||
| 68 | $this->info('No releases found with size mismatches above '.$threshold.'%' |
||
| 69 | .($checkSeasonPack ? ' that are season packs' : '')); |
||
| 70 | |||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($shouldRename) { |
||
| 75 | $this->info("\nAttempting to rename ".$mismatches->count()." releases...\n"); |
||
| 76 | |||
| 77 | foreach ($mismatches as $release) { |
||
| 78 | $this->attemptRename($release, $nameFixer); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->outputReleaseIdsAsCsv($mismatches); |
||
| 82 | |||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Regular table output for non-rename mode |
||
| 87 | $headers = ['Release ID', 'Searchname', 'Release Size', 'Files Total', 'Difference', 'Diff %']; |
||
| 88 | $rows = $mismatches->map(function ($release) { |
||
| 89 | return [ |
||
| 90 | $release->id, |
||
| 91 | $release->searchname, |
||
| 92 | number_format($release->release_size, 2).' GiB', |
||
| 93 | number_format($release->files_total_size, 2).' GiB', |
||
| 94 | number_format($release->size_diff, 2).' GiB', |
||
| 95 | number_format($release->diff_percent, 2).'%', |
||
| 96 | ]; |
||
| 97 | }); |
||
| 98 | |||
| 99 | $this->table($headers, $rows); |
||
| 100 | $this->info("\nFound ".$mismatches->count().' releases with size mismatches above '.$threshold.'%'); |
||
| 101 | |||
| 102 | $this->outputReleaseIdsAsCsv($mismatches); |
||
| 103 | } |
||
| 140 |