| Conditions | 7 |
| Paths | 10 |
| Total Lines | 56 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 52 | public function process() |
||
| 53 | { |
||
| 54 | $this->setProcessor(); |
||
| 55 | |||
| 56 | $extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type)); |
||
| 57 | if (empty($extensions)) { |
||
| 58 | throw new Exception(sprintf('The config key "postprocess.%s.ext" is empty', $this->type)); |
||
| 59 | } |
||
| 60 | |||
| 61 | $files = Finder::create() |
||
| 62 | ->files() |
||
| 63 | ->in($this->builder->getConfig()->getOutputPath()) |
||
| 64 | ->name('/\.('.implode('|', $extensions).')$/') |
||
| 65 | ->notName('/\.min\.('.implode('|', $extensions).')$/') |
||
| 66 | ->sortByName(true); |
||
| 67 | $max = count($files); |
||
| 68 | |||
| 69 | if ($max <= 0) { |
||
| 70 | $this->builder->getLogger()->info('No files'); |
||
| 71 | |||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $count = 0; |
||
| 76 | $postprocessed = 0; |
||
| 77 | $cache = new Cache($this->builder, 'postprocess'); |
||
| 78 | |||
| 79 | /** @var \Symfony\Component\Finder\SplFileInfo $file */ |
||
| 80 | foreach ($files as $file) { |
||
| 81 | $count++; |
||
| 82 | $sizeBefore = $file->getSize(); |
||
| 83 | $message = $file->getRelativePathname(); |
||
| 84 | |||
| 85 | $cacheKey = $cache->createKeyFromPath($file->getPathname(), $file->getRelativePathname()); |
||
| 86 | if (!$cache->has($cacheKey)) { |
||
| 87 | $processed = $this->processFile($file); |
||
| 88 | $sizeAfter = $file->getSize(); |
||
| 89 | if ($sizeAfter < $sizeBefore) { |
||
| 90 | $message = sprintf( |
||
| 91 | '%s (%s Ko -> %s Ko)', |
||
| 92 | $file->getRelativePathname(), |
||
| 93 | ceil($sizeBefore / 1000), |
||
| 94 | ceil($sizeAfter / 1000) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | $postprocessed++; |
||
| 98 | $processed = $this->encode($processed); |
||
| 99 | $cache->set($cacheKey, $processed); |
||
| 100 | $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); |
||
| 101 | } |
||
| 102 | $processed = $cache->get($cacheKey); |
||
| 103 | $processed = $this->decode($processed); |
||
|
|
|||
| 104 | Util\File::getFS()->dumpFile($file->getPathname(), $processed); |
||
| 105 | } |
||
| 106 | if ($postprocessed == 0) { |
||
| 107 | $this->builder->getLogger()->info('Nothing to do'); |
||
| 108 | } |
||
| 137 |