| Conditions | 18 |
| Paths | 23 |
| Total Lines | 85 |
| Code Lines | 53 |
| 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 |
||
| 42 | protected function process(TvProcessingPassable $passable): TvProcessingResult |
||
| 43 | { |
||
| 44 | $parsedInfo = $passable->getParsedInfo(); |
||
| 45 | $context = $passable->context; |
||
| 46 | |||
| 47 | if ($parsedInfo === null || empty($parsedInfo['cleanname'])) { |
||
| 48 | return TvProcessingResult::notFound($this->getName()); |
||
| 49 | } |
||
| 50 | |||
| 51 | $cleanName = $parsedInfo['cleanname']; |
||
| 52 | $localDb = $this->getLocalDb(); |
||
| 53 | |||
| 54 | // Try to find the show in our local database by title |
||
| 55 | $videoId = $localDb->getByTitle($cleanName, self::TYPE_TV, 0); |
||
| 56 | |||
| 57 | // If not found and cleanName contains a year in parentheses, try without the year |
||
| 58 | if (($videoId === 0 || $videoId === false) && preg_match('/^(.+?)\s*\(\d{4}\)$/', $cleanName, $yearMatch)) { |
||
| 59 | $nameWithoutYear = trim($yearMatch[1]); |
||
| 60 | $videoId = $localDb->getByTitle($nameWithoutYear, self::TYPE_TV, 0); |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($videoId === 0 || $videoId === false) { |
||
| 64 | $this->outputNotFound($cleanName); |
||
| 65 | return TvProcessingResult::notFound($this->getName(), ['title' => $cleanName]); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Found a matching show in local DB |
||
| 69 | $episodeId = false; |
||
| 70 | $hasEpisodeNumbers = $this->hasEpisodeNumbers($parsedInfo); |
||
| 71 | $hasAirdate = ! empty($parsedInfo['airdate']); |
||
| 72 | |||
| 73 | if ($hasEpisodeNumbers || $hasAirdate) { |
||
| 74 | // Try to find the specific episode |
||
| 75 | $episodeId = $localDb->getBySeasonEp( |
||
| 76 | $videoId, |
||
| 77 | (int) ($parsedInfo['season'] ?? 0), |
||
| 78 | (int) ($parsedInfo['episode'] ?? 0), |
||
| 79 | $parsedInfo['airdate'] ?? '' |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($episodeId !== false && $episodeId > 0) { |
||
| 84 | // Complete match - both show and episode found |
||
| 85 | $localDb->setVideoIdFound($videoId, $context->releaseId, $episodeId); |
||
| 86 | |||
| 87 | $this->outputMatch( |
||
| 88 | $cleanName, |
||
| 89 | $hasEpisodeNumbers ? (int) $parsedInfo['season'] : null, |
||
| 90 | $hasEpisodeNumbers ? (int) $parsedInfo['episode'] : null, |
||
| 91 | $hasAirdate ? $parsedInfo['airdate'] : null |
||
| 92 | ); |
||
| 93 | |||
| 94 | return TvProcessingResult::matched($videoId, $episodeId, $this->getName()); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Series matched but no specific episode located |
||
| 98 | // Set video ID but mark episode as 0 (needs further lookup) |
||
| 99 | $localDb->setVideoIdFound($videoId, $context->releaseId, 0); |
||
| 100 | |||
| 101 | if ($this->echoOutput) { |
||
| 102 | $this->colorCli->primaryOver(' → '); |
||
| 103 | $this->colorCli->headerOver($this->truncateTitle($cleanName)); |
||
| 104 | if ($hasAirdate) { |
||
| 105 | $this->colorCli->primaryOver(' | '); |
||
| 106 | $this->colorCli->warningOver($parsedInfo['airdate']); |
||
| 107 | $this->colorCli->headerOver(' → '); |
||
| 108 | $this->colorCli->notice('Series matched, airdate not in local DB'); |
||
| 109 | } elseif ($hasEpisodeNumbers) { |
||
| 110 | $this->colorCli->primaryOver(' S'); |
||
| 111 | $this->colorCli->warningOver(sprintf('%02d', (int) $parsedInfo['season'])); |
||
| 112 | $this->colorCli->primaryOver('E'); |
||
| 113 | $this->colorCli->warningOver(sprintf('%02d', (int) $parsedInfo['episode'])); |
||
| 114 | $this->colorCli->headerOver(' → '); |
||
| 115 | $this->colorCli->notice('Series matched, episode not in local DB'); |
||
| 116 | } else { |
||
| 117 | $this->colorCli->primaryOver(' → '); |
||
| 118 | $this->colorCli->notice('Series matched (no episode info)'); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // Return not found so external APIs can try to get the episode |
||
| 123 | return TvProcessingResult::notFound($this->getName(), [ |
||
| 124 | 'video_id' => $videoId, |
||
| 125 | 'series_matched' => true, |
||
| 126 | 'episode_missing' => true, |
||
| 127 | ]); |
||
| 141 |