| Conditions | 7 |
| Paths | 35 |
| Total Lines | 54 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 21 | public function handle(ToonService $toonService): int |
||
| 22 | { |
||
| 23 | $file = $this->argument('file'); |
||
| 24 | $format = $this->option('format'); |
||
| 25 | $output = $this->option('output'); |
||
| 26 | $decode = $this->option('decode'); |
||
| 27 | $pretty = $this->option('pretty'); |
||
| 28 | |||
| 29 | if (!file_exists($file)) { |
||
| 30 | $this->error("Archivo no encontrado: {$file}"); |
||
| 31 | |||
| 32 | return self::FAILURE; |
||
| 33 | } |
||
| 34 | |||
| 35 | $content = file_get_contents($file); |
||
| 36 | if (false === $content) { |
||
| 37 | $this->error("No se pudo leer el archivo: {$file}"); |
||
| 38 | |||
| 39 | return self::FAILURE; |
||
| 40 | } |
||
| 41 | |||
| 42 | $this->info('Procesando archivo...'); |
||
| 43 | $this->newLine(); |
||
| 44 | |||
| 45 | try { |
||
| 46 | if ($decode) { |
||
| 47 | $result = $toonService->decode($content); |
||
| 48 | if ($pretty) { |
||
| 49 | $result = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
||
| 50 | } else { |
||
| 51 | $result = json_encode($result, JSON_THROW_ON_ERROR); |
||
| 52 | } |
||
| 53 | $outputFormat = 'JSON'; |
||
| 54 | } else { |
||
| 55 | $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); |
||
| 56 | $result = $toonService->convert($data, $format); |
||
| 57 | $outputFormat = strtoupper($format).' TOON'; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($output) { |
||
| 61 | file_put_contents($output, $result); |
||
| 62 | $this->info("✓ Archivo guardado: {$output}"); |
||
| 63 | } else { |
||
| 64 | $this->line($result); |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->newLine(); |
||
| 68 | $this->info("Conversión completada exitosamente a {$outputFormat}"); |
||
| 69 | |||
| 70 | return self::SUCCESS; |
||
| 71 | } catch (\Exception $e) { |
||
| 72 | $this->error('Error durante la conversión: '.$e->getMessage()); |
||
| 73 | |||
| 74 | return self::FAILURE; |
||
| 75 | } |
||
| 79 |