| Conditions | 15 |
| Paths | 115 |
| Total Lines | 56 |
| Code Lines | 42 |
| 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 |
||
| 65 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 66 | { |
||
| 67 | $from = $input->getArgument('from'); |
||
| 68 | $type = $input->getOption('type'); |
||
| 69 | $preserveComments = $input->getOption('preserveComments'); |
||
| 70 | $outputFile = $input->getOption('output'); |
||
| 71 | $appendFile = $input->getOption('append'); |
||
| 72 | if ($outputFile === null && $appendFile) { |
||
| 73 | throw new RuntimeException('Append file used without output file specification!'); |
||
| 74 | } |
||
| 75 | if (!is_array($from)) { |
||
| 76 | $from = (array) $from; |
||
| 77 | } |
||
| 78 | if (empty($type)) { |
||
| 79 | $autoDetectedType = self::getFileExt($from[0]); |
||
| 80 | foreach ($from as $fromFile) { |
||
| 81 | $fileExt = self::getFileExt($fromFile); |
||
| 82 | if (strcasecmp($fileExt, $autoDetectedType) !== 0) { |
||
| 83 | throw new RuntimeException('Type of input files is not all the same!'); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | $type = $autoDetectedType; |
||
| 87 | } |
||
| 88 | if (empty($type)) { |
||
| 89 | throw new RuntimeException('Error: cannot find the type of input file!'); |
||
| 90 | } |
||
| 91 | switch (strtolower($type)) { |
||
| 92 | case 'css': |
||
| 93 | $minifier = new CSS(); |
||
| 94 | break; |
||
| 95 | case 'js': |
||
| 96 | $minifier = new JS(); |
||
| 97 | break; |
||
| 98 | default: |
||
| 99 | throw new RuntimeException("Unsupported type: {$type}"); |
||
| 100 | } |
||
| 101 | foreach ($from as $fromFile) { |
||
| 102 | if (!file_exists($fromFile)) { |
||
| 103 | throw new RuntimeException("File '{$fromFile}' not found!"); |
||
| 104 | } |
||
| 105 | $minifier->add($fromFile); |
||
| 106 | } |
||
| 107 | if ($preserveComments) { |
||
| 108 | $minifier->setLeavePreservedComments(true); |
||
| 109 | } |
||
| 110 | $result = $minifier->minify(); |
||
| 111 | if ($outputFile === null) { |
||
| 112 | $output->writeln($result, OutputInterface::OUTPUT_RAW); |
||
| 113 | } elseif ($appendFile) { |
||
| 114 | file_put_contents($outputFile, $result, FILE_APPEND); |
||
| 115 | } else { |
||
| 116 | file_put_contents($outputFile, $result); |
||
| 117 | } |
||
| 118 | |||
| 119 | return 0; |
||
| 120 | } |
||
| 121 | |||
| 132 |