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