Passed
Push — develop ( 875640...705a72 )
by Andrew
06:19
created

Image::getIsAnimatedGif()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 63
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 63
rs 6.9666
cc 12
nc 28
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
3
 * @link      https://craftcms.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
4
 * @copyright Copyright (c) Pixel & Tonic, Inc.
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
5
 * @license   https://craftcms.github.io/license/
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
6
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
7
8
namespace nystudio107\imageoptimize\helpers;
9
10
use Craft;
11
use craft\errors\ImageException;
12
use craft\helpers\FileHelper;
13
14
use Imagine\Gd\Imagine as GdImagine;
15
use Imagine\Imagick\Imagine as ImagickImagine;
16
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
19
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @since     1.6.4
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class Image
23
{
24
    // Public Static Methods
25
    // =========================================================================
26
27
    /**
28
     * This is a hacked version of `Raster::loadImage()` because the $_isAnimatedGif
29
     * property is private
30
     *
31
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
32
     *
33
     * @return bool
34
     *
35
     * @throws ImageException
36
     * @throws \yii\base\InvalidConfigException
37
     */
38
    public static function getIsAnimatedGif(string $path): bool
39
    {
40
        $generalConfig = Craft::$app->getConfig()->getGeneral();
41
42
        $extension = strtolower($generalConfig->imageDriver);
43
44
        // If it's explicitly set, take their word for it.
45
        if ($extension === 'gd') {
46
            $instance = new GdImagine();
47
        } else {
48
            if ($extension === 'imagick') {
49
                $instance = new ImagickImagine();
50
            } else {
51
                // Let's try to auto-detect.
52
                if (Craft::$app->getImages()->getIsGd()) {
53
                    $instance = new GdImagine();
54
                } else {
55
                    $instance = new ImagickImagine();
56
                }
57
            }
58
        }
59
60
        $imageService = Craft::$app->getImages();
61
        if ($imageService->getIsGd()) {
62
            return false;
63
        }
64
        
65
        if (!is_file($path)) {
66
            Craft::error('Tried to load an image at '.$path.', but the file does not exist.', __METHOD__);
67
            throw new ImageException(Craft::t('app', 'No file exists at the given path.'));
68
        }
69
70
        if (!$imageService->checkMemoryForImage($path)) {
71
            throw new ImageException(Craft::t(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
72
                'app',
73
                'Not enough memory available to perform this image operation.'
74
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
75
        }
76
77
        // Make sure the image says it's an image
78
        $mimeType = FileHelper::getMimeType($path, null, false);
79
80
        if ($mimeType !== null && strpos($mimeType, 'image/') !== 0 && strpos($mimeType, 'application/pdf') !== 0) {
81
            throw new ImageException(Craft::t(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
82
                'app',
83
                'The file “{name}” does not appear to be an image.',
84
                ['name' => pathinfo($path, PATHINFO_BASENAME)]
85
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
86
        }
87
88
        try {
89
            $image = $instance->open($path);
90
        } catch (\Throwable $e) {
91
            throw new ImageException(Craft::t(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
92
                'app',
93
                'The file “{path}” does not appear to be an image.',
94
                ['path' => $path]
95
            ), 0, $e);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
96
        }
97
98
        $extension = pathinfo($path, PATHINFO_EXTENSION);
99
100
        return $extension === 'gif' && $image->layers();
101
    }
102
}
103