| Conditions | 8 |
| Paths | 10 |
| Total Lines | 77 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | call_user_func_array( |
||
| 58 | $this->builder->getMessageCb(), |
||
| 59 | ['POSTPROCESS', sprintf('Post-processing %s', $this->type)] |
||
| 60 | ); |
||
| 61 | |||
| 62 | $extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type)); |
||
| 63 | if (empty($extensions)) { |
||
| 64 | throw new Exception(sprintf('The config key "postprocess.%s.ext" is empty', $this->type)); |
||
| 65 | } |
||
| 66 | |||
| 67 | $files = Finder::create() |
||
| 68 | ->files() |
||
| 69 | ->in($this->builder->getConfig()->getOutputPath()) |
||
| 70 | ->name('/\.('.implode('|', $extensions).')$/') |
||
| 71 | ->notName('/\.min\.('.implode('|', $extensions).')$/') |
||
| 72 | ->sortByName(true); |
||
| 73 | $max = count($files); |
||
| 74 | |||
| 75 | if ($max <= 0) { |
||
| 76 | $message = 'No files'; |
||
| 77 | call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]); |
||
| 78 | |||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | $count = 0; |
||
| 83 | $postprocessed = 0; |
||
| 84 | |||
| 85 | /** @var \Symfony\Component\Finder\SplFileInfo $file */ |
||
| 86 | foreach ($files as $file) { |
||
| 87 | $sizeBefore = $file->getSize(); |
||
| 88 | |||
| 89 | $processedFile = Util::joinFile( |
||
| 90 | $this->config->getCachePath(), |
||
| 91 | self::CACHE_FILES, |
||
| 92 | $file->getRelativePathname() |
||
| 93 | ); |
||
| 94 | $hash = hash_file('md5', $file->getPathname()); |
||
| 95 | $hashFile = Util::joinFile( |
||
| 96 | $this->config->getCachePath(), |
||
| 97 | self::CACHE_HASH, |
||
| 98 | $this->preparesHashFile($file->getRelativePathname()).$hash |
||
| 99 | ); |
||
| 100 | |||
| 101 | if (!Util::getFS()->exists($processedFile) |
||
| 102 | || !Util::getFS()->exists($hashFile) |
||
| 103 | ) { |
||
| 104 | $count++; |
||
| 105 | |||
| 106 | $this->processFile($file); |
||
| 107 | $postprocessed++; |
||
| 108 | $sizeAfter = $file->getSize(); |
||
| 109 | |||
| 110 | $this->removesHashFile($file->getRelativePathname()); |
||
| 111 | Util::getFS()->copy($file->getPathname(), $processedFile, true); |
||
| 112 | Util::getFS()->mkdir(Util::joinFile($this->config->getCachePath(), self::CACHE_HASH)); |
||
| 113 | Util::getFS()->touch($hashFile); |
||
| 114 | |||
| 115 | $message = $file->getRelativePathname(); |
||
| 116 | if ($sizeAfter < $sizeBefore) { |
||
| 117 | $message = sprintf( |
||
| 118 | '%s (%s Ko -> %s Ko)', |
||
| 119 | $file->getRelativePathname(), |
||
| 120 | ceil($sizeBefore / 1000), |
||
| 121 | ceil($sizeAfter / 1000) |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message, $count, $max]); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | if ($postprocessed == 0) { |
||
| 128 | $message = 'Nothing to do'; |
||
| 129 | call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]); |
||
| 130 | } |
||
| 172 |