Conditions | 12 |
Paths | 28 |
Total Lines | 63 |
Code Lines | 38 |
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 |
||
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( |
||
72 | 'app', |
||
73 | 'Not enough memory available to perform this image operation.' |
||
74 | )); |
||
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( |
||
82 | 'app', |
||
83 | 'The file “{name}” does not appear to be an image.', |
||
84 | ['name' => pathinfo($path, PATHINFO_BASENAME)] |
||
85 | )); |
||
86 | } |
||
87 | |||
88 | try { |
||
89 | $image = $instance->open($path); |
||
90 | } catch (\Throwable $e) { |
||
91 | throw new ImageException(Craft::t( |
||
92 | 'app', |
||
93 | 'The file “{path}” does not appear to be an image.', |
||
94 | ['path' => $path] |
||
95 | ), 0, $e); |
||
96 | } |
||
97 | |||
98 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
||
99 | |||
100 | return $extension === 'gif' && $image->layers(); |
||
101 | } |
||
103 |