| Conditions | 30 |
| Paths | 8100 |
| Total Lines | 176 |
| Code Lines | 110 |
| 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 |
||
| 151 | protected function doActualConvert() |
||
| 152 | { |
||
| 153 | $errorMsg = ''; |
||
| 154 | $options = $this->options; |
||
| 155 | $useNice = (($options['use-nice']) && self::hasNiceSupport()); |
||
| 156 | |||
| 157 | $commandOptions = $this->createCommandLineOptions(); |
||
| 158 | |||
| 159 | |||
| 160 | // Init with common system paths |
||
| 161 | $cwebpPathsToTest = self::$cwebpDefaultPaths; |
||
| 162 | |||
| 163 | // Remove paths that doesn't exist |
||
| 164 | /* |
||
| 165 | $cwebpPathsToTest = array_filter($cwebpPathsToTest, function ($binary) { |
||
| 166 | //return file_exists($binary); |
||
| 167 | return @is_readable($binary); |
||
| 168 | }); |
||
| 169 | */ |
||
| 170 | |||
| 171 | // Try all common paths that exists |
||
| 172 | $success = false; |
||
| 173 | $failures = []; |
||
| 174 | $failureCodes = []; |
||
| 175 | |||
| 176 | |||
| 177 | $returnCode = 0; |
||
| 178 | $majorFailCode = 0; |
||
| 179 | if ($options['try-common-system-paths']) { |
||
| 180 | foreach ($cwebpPathsToTest as $index => $binary) { |
||
| 181 | $returnCode = $this->executeBinary($binary, $commandOptions, $useNice); |
||
| 182 | if ($returnCode == 0) { |
||
| 183 | $this->logLn('Successfully executed binary: ' . $binary); |
||
| 184 | $success = true; |
||
| 185 | break; |
||
| 186 | } else { |
||
| 187 | $failures[] = [$binary, $returnCode]; |
||
| 188 | if (!in_array($returnCode, $failureCodes)) { |
||
| 189 | $failureCodes[] = $returnCode; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | if (!$success) { |
||
| 195 | if (count($failureCodes) == 1) { |
||
| 196 | $majorFailCode = $failureCodes[0]; |
||
| 197 | switch ($majorFailCode) { |
||
| 198 | case 126: |
||
| 199 | $errorMsg = 'Permission denied. The user that the command was run with (' . |
||
| 200 | shell_exec('whoami') . ') does not have permission to execute any of the ' . |
||
| 201 | 'cweb binaries found in common system locations. '; |
||
| 202 | break; |
||
| 203 | case 127: |
||
| 204 | $errorMsg .= 'Found no cwebp binaries in any common system locations. '; |
||
| 205 | break; |
||
| 206 | default: |
||
| 207 | $errorMsg .= 'Tried executing cwebp binaries in common system locations. ' . |
||
| 208 | 'All failed (exit code: ' . $majorFailCode . '). '; |
||
| 209 | } |
||
| 210 | } else { |
||
| 211 | /** |
||
| 212 | * $failureCodesBesides127 is used to check first position ($failureCodesBesides127[0]) |
||
| 213 | * however position can vary as index can be 1 or something else. array_values() would |
||
| 214 | * always start from 0. |
||
| 215 | */ |
||
| 216 | $failureCodesBesides127 = array_values(array_diff($failureCodes, [127])); |
||
| 217 | |||
| 218 | if (count($failureCodesBesides127) == 1) { |
||
| 219 | $majorFailCode = $failureCodesBesides127[0]; |
||
| 220 | switch ($returnCode) { |
||
| 221 | case 126: |
||
| 222 | $errorMsg = 'Permission denied. The user that the command was run with (' . |
||
| 223 | shell_exec('whoami') . ') does not have permission to execute any of the cweb ' . |
||
| 224 | 'binaries found in common system locations. '; |
||
| 225 | break; |
||
| 226 | default: |
||
| 227 | $errorMsg .= 'Tried executing cwebp binaries in common system locations. ' . |
||
| 228 | 'All failed (exit code: ' . $majorFailCode . '). '; |
||
| 229 | } |
||
| 230 | } else { |
||
| 231 | $errorMsg .= 'None of the cwebp binaries in the common system locations could be executed ' . |
||
| 232 | '(mixed results - got the following exit codes: ' . implode(',', $failureCodes) . '). '; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!$success && $options['try-supplied-binary-for-os']) { |
||
| 239 | // Try supplied binary (if available for OS, and hash is correct) |
||
| 240 | if (isset(self::$suppliedBinariesInfo[PHP_OS])) { |
||
| 241 | $info = self::$suppliedBinariesInfo[PHP_OS]; |
||
| 242 | |||
| 243 | $file = $info[0]; |
||
| 244 | $hash = $info[1]; |
||
| 245 | |||
| 246 | $binaryFile = __DIR__ . '/' . $options['rel-path-to-precompiled-binaries'] . '/' . $file; |
||
| 247 | |||
| 248 | // The file should exist, but may have been removed manually. |
||
| 249 | if (file_exists($binaryFile)) { |
||
| 250 | // File exists, now generate its hash |
||
| 251 | |||
| 252 | // hash_file() is normally available, but it is not always |
||
| 253 | // - https://stackoverflow.com/questions/17382712/php-5-3-20-undefined-function-hash |
||
| 254 | // If available, validate that hash is correct. |
||
| 255 | $proceedAfterHashCheck = true; |
||
| 256 | if (function_exists('hash_file')) { |
||
| 257 | $binaryHash = hash_file('sha256', $binaryFile); |
||
| 258 | |||
| 259 | if ($binaryHash != $hash) { |
||
| 260 | $errorMsg .= 'Binary checksum of supplied binary is invalid! ' . |
||
| 261 | 'Did you transfer with FTP, but not in binary mode? ' . |
||
| 262 | 'File:' . $binaryFile . '. ' . |
||
| 263 | 'Expected checksum: ' . $hash . '. ' . |
||
| 264 | 'Actual checksum:' . $binaryHash . '.'; |
||
| 265 | $proceedAfterHashCheck = false; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | if ($proceedAfterHashCheck) { |
||
| 269 | $returnCode = $this->executeBinary($binaryFile, $commandOptions, $useNice); |
||
| 270 | if ($returnCode == 0) { |
||
| 271 | $success = true; |
||
| 272 | } else { |
||
| 273 | $errorMsg .= 'Tried executing supplied binary for ' . PHP_OS . ', ' . |
||
| 274 | ($options['try-common-system-paths'] ? 'but that failed too' : 'but failed'); |
||
| 275 | if ($options['try-common-system-paths'] && ($majorFailCode > 0)) { |
||
| 276 | $errorMsg .= ' (same error)'; |
||
| 277 | } else { |
||
| 278 | if ($returnCode > 128) { |
||
| 279 | $errorMsg .= '. The binary did not work (exit code: ' . $returnCode . '). ' . |
||
| 280 | 'Check out https://github.com/rosell-dk/webp-convert/issues/92'; |
||
| 281 | } else { |
||
| 282 | switch ($returnCode) { |
||
| 283 | case 0: |
||
| 284 | $success = true; |
||
| 285 | ; |
||
| 286 | break; |
||
| 287 | case 126: |
||
| 288 | $errorMsg .= ': Permission denied. The user that the command was run' . |
||
| 289 | ' with (' . shell_exec('whoami') . ') does not have permission to ' . |
||
| 290 | 'execute that binary.'; |
||
| 291 | break; |
||
| 292 | case 127: |
||
| 293 | $errorMsg .= '. The binary was not found! ' . |
||
| 294 | 'It ought to be here: ' . $binaryFile; |
||
| 295 | break; |
||
| 296 | default: |
||
| 297 | $errorMsg .= ' (exit code:' . $returnCode . ').'; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | $errorMsg .= 'Supplied binary not found! It ought to be here:' . $binaryFile; |
||
| 305 | } |
||
| 306 | } else { |
||
| 307 | $errorMsg .= 'No supplied binaries found for OS:' . PHP_OS; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | // cwebp sets file permissions to 664 but instead .. |
||
| 312 | // .. $destination's parent folder's permissions should be used (except executable bits) |
||
| 313 | // (or perhaps the current umask instead? https://www.php.net/umask) |
||
| 314 | |||
| 315 | if ($success) { |
||
| 316 | $destinationParent = dirname($this->destination); |
||
| 317 | $fileStatistics = stat($destinationParent); |
||
| 318 | if ($fileStatistics !== false) { |
||
| 319 | // Apply same permissions as parent folder but strip off the executable bits |
||
| 320 | $permissions = $fileStatistics['mode'] & 0000666; |
||
| 321 | chmod($this->destination, $permissions); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | if (!$success) { |
||
| 326 | throw new SystemRequirementsNotMetException($errorMsg); |
||
| 327 | } |
||
| 330 |