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