| Conditions | 7 |
| Paths | 26 |
| Total Lines | 78 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 113 | public function convert() |
||
| 114 | { |
||
| 115 | try { |
||
| 116 | $this->checkRequirements(); |
||
| 117 | |||
| 118 | // Preparing array holding possible binaries |
||
| 119 | $binaries = $this->setUpBinaries(); |
||
| 120 | } catch (Exception $e) { |
||
| 121 | return false; // TODO: `throw` custom \Exception $e & handle it smoothly on top-level. |
||
| 122 | } |
||
| 123 | |||
| 124 | /* |
||
| 125 | * Preparing options |
||
| 126 | */ |
||
| 127 | |||
| 128 | // lossless PNG conversion |
||
| 129 | $lossless = ( |
||
| 130 | $this->extension == 'png' |
||
| 131 | ? '-lossless' |
||
| 132 | : '' |
||
| 133 | ); |
||
| 134 | |||
| 135 | // Built-in method option |
||
| 136 | $method = '-m 6'; |
||
| 137 | |||
| 138 | // Metadata (all, exif, icc, xmp or none (default)) |
||
| 139 | // Comma-separated list of existing metadata to copy from input to output |
||
| 140 | $metadata = ( |
||
| 141 | $this->strip |
||
| 142 | ? '-metadata none' |
||
| 143 | : '-metadata all' |
||
| 144 | ); |
||
| 145 | |||
| 146 | // Built-in low memory option |
||
| 147 | $lowMemory = '-low_memory'; |
||
| 148 | |||
| 149 | $optionsArray = [ |
||
| 150 | $lossless, |
||
| 151 | $quality = '-q ' . $this->quality, |
||
|
|
|||
| 152 | $method, |
||
| 153 | $metadata, |
||
| 154 | $lowMemory, |
||
| 155 | $input = $this->escapeFilename($this->source), |
||
| 156 | $output = '-o ' . $this->escapeFilename($this->destination), |
||
| 157 | $stderrRedirect = '2>&1', |
||
| 158 | ]; |
||
| 159 | |||
| 160 | $options = implode(' ', array_filter($optionsArray)); |
||
| 161 | |||
| 162 | $nice = ( |
||
| 163 | $this->hasNiceSupport() |
||
| 164 | ? 'nice' |
||
| 165 | : '' |
||
| 166 | ); |
||
| 167 | |||
| 168 | $success = false; |
||
| 169 | |||
| 170 | // Try all paths |
||
| 171 | foreach ($binaries as $index => $binary) { |
||
| 172 | $command = $nice . ' ' . $binary . ' ' . $options; |
||
| 173 | exec($command, $result, $returnCode); |
||
| 174 | |||
| 175 | if ($returnCode == 0) { // Everything okay! |
||
| 176 | // cwebp sets file permissions to 664 but instead .. |
||
| 177 | // .. $destination's parent folder's permissions should be used (except executable bits) |
||
| 178 | $destinationParent = dirname($this->destination); |
||
| 179 | $fileStatistics = stat($destinationParent); |
||
| 180 | |||
| 181 | // Apply same permissions as parent folder but strip off the executable bits |
||
| 182 | $permissions = $fileStatistics['mode'] & 0000666; |
||
| 183 | chmod($this->destination, $permissions); |
||
| 184 | |||
| 185 | $success = true; |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return $success; |
||
| 191 | } |
||
| 193 |