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