Conditions | 12 |
Paths | 28 |
Total Lines | 58 |
Code Lines | 36 |
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 |
||
39 | public static function getIsAnimatedGif(string $path): bool |
||
40 | { |
||
41 | $generalConfig = Craft::$app->getConfig()->getGeneral(); |
||
42 | |||
43 | $extension = strtolower($generalConfig->imageDriver); |
||
44 | |||
45 | // If it's explicitly set, take their word for it. |
||
46 | if ($extension === 'gd') { |
||
47 | $instance = new GdImagine(); |
||
48 | } elseif ($extension === 'imagick') { |
||
49 | $instance = new ImagickImagine(); |
||
50 | } elseif (Craft::$app->getImages()->getIsGd()) { |
||
51 | $instance = new GdImagine(); |
||
52 | } else { |
||
53 | $instance = new ImagickImagine(); |
||
54 | } |
||
55 | |||
56 | $imageService = Craft::$app->getImages(); |
||
57 | if ($imageService->getIsGd()) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | if (!is_file($path)) { |
||
62 | Craft::error('Tried to load an image at ' . $path . ', but the file does not exist.', __METHOD__); |
||
63 | throw new ImageException(Craft::t('app', 'No file exists at the given path.')); |
||
64 | } |
||
65 | |||
66 | if (!$imageService->checkMemoryForImage($path)) { |
||
67 | throw new ImageException(Craft::t( |
||
68 | 'app', |
||
69 | 'Not enough memory available to perform this image operation.' |
||
70 | )); |
||
71 | } |
||
72 | |||
73 | // Make sure the image says it's an image |
||
74 | $mimeType = FileHelper::getMimeType($path, null, false); |
||
75 | |||
76 | if ($mimeType !== null && !str_starts_with($mimeType, 'image/') && !str_starts_with($mimeType, 'application/pdf')) { |
||
77 | throw new ImageException(Craft::t( |
||
78 | 'app', |
||
79 | 'The file “{name}” does not appear to be an image.', |
||
80 | ['name' => pathinfo($path, PATHINFO_BASENAME)] |
||
81 | )); |
||
82 | } |
||
83 | |||
84 | try { |
||
85 | $image = $instance->open($path); |
||
86 | } catch (Throwable $e) { |
||
87 | throw new ImageException(Craft::t( |
||
88 | 'app', |
||
89 | 'The file “{path}” does not appear to be an image.', |
||
90 | ['path' => $path] |
||
91 | ), 0, $e); |
||
92 | } |
||
93 | |||
94 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
||
95 | |||
96 | return $extension === 'gif' && $image->layers()->count() > 1; |
||
97 | } |
||
99 |