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