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