| Total Complexity | 53 |
| Total Lines | 211 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like ImageManipulations often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageManipulations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ImageManipulations |
||
| 13 | { |
||
| 14 | private static $webp_enabled = true; |
||
| 15 | |||
| 16 | private static $webp_quality = 77; |
||
| 17 | |||
| 18 | private static $imageLinkCache = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * work out the best image link. |
||
| 22 | * |
||
| 23 | * There are basically three options: |
||
| 24 | * a. if the height and/or width matches or is smaller than it should be |
||
| 25 | * then just return natural image |
||
| 26 | * b. if crop is set to true then Fill |
||
| 27 | * c. otherwise resize Height/Width/Both |
||
| 28 | * |
||
| 29 | * @param Image $image |
||
| 30 | * @param null|bool $useRetina optional |
||
| 31 | * @param null|bool $forMobile optional |
||
| 32 | * @param null|int $resizeToWidth optional |
||
| 33 | */ |
||
| 34 | public static function get_image_link($image, string $name, ?bool $useRetina = null, ?bool $forMobile = null, ?int $resizeToWidth = 0): string |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * back-up image. |
||
| 111 | * |
||
| 112 | * @return null|Image |
||
| 113 | */ |
||
| 114 | public static function get_backup_image(string $name) |
||
| 115 | { |
||
| 116 | $image = null; |
||
| 117 | $backupObject = SiteConfig::current_site_config(); |
||
| 118 | if ($backupObject->hasMethod($name)) { |
||
| 119 | $image = $backupObject->{$name}(); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $image; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * placeholder image. |
||
| 127 | */ |
||
| 128 | public static function get_placeholder_image_tag(string $name): string |
||
| 148 | } |
||
| 149 | |||
| 150 | public static function web_p_link(string $link): string |
||
| 151 | { |
||
| 152 | if (self::web_p_enabled() && $link) { |
||
| 153 | $fileNameWithBaseFolder = Director::baseFolder() .'/public' . $link; |
||
| 154 | $arrayOfLink = explode('.', $link); |
||
| 155 | $extension = array_pop($arrayOfLink); |
||
| 156 | $pathWithoutExtension = rtrim($link, '.' . $extension); |
||
| 157 | $webPFileName = $pathWithoutExtension . '_' . $extension . '.webp'; |
||
| 158 | $webPFileNameWithBaseFolder = Director::baseFolder() .'/public' . $webPFileName; |
||
| 159 | if (file_exists($fileNameWithBaseFolder)) { |
||
| 160 | if (isset($_GET['flush']) && file_exists($webPFileNameWithBaseFolder)) { |
||
| 161 | unlink($webPFileNameWithBaseFolder); |
||
| 162 | } |
||
| 163 | if (file_exists($webPFileNameWithBaseFolder)) { |
||
| 164 | //todo: check that image is the same ... |
||
| 165 | } else { |
||
| 166 | list($width, $height, $type) = getimagesize($fileNameWithBaseFolder); |
||
| 167 | $img = null; |
||
| 168 | if ($width && $height) { |
||
| 169 | if (2 === $type) { |
||
| 170 | $img = imagecreatefromjpeg($fileNameWithBaseFolder); |
||
| 171 | } elseif (3 === $type) { |
||
| 172 | $img = imagecreatefrompng($fileNameWithBaseFolder); |
||
| 173 | imagesavealpha($img, true); |
||
| 174 | } |
||
| 175 | if (null !== $img) { |
||
| 176 | $quality = Config::inst()->get(ImageManipulations::class, 'webp_quality'); |
||
| 177 | imagewebp($img, $webPFileNameWithBaseFolder, $quality); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return $webPFileName; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | return $link; |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function add_fake_parts($image, string $link): string |
||
| 190 | { |
||
| 191 | if (class_exists('HashPathExtension')) { |
||
| 192 | /** @var null|Controller $curr */ |
||
| 193 | $curr = Controller::curr(); |
||
| 194 | if ($curr) { |
||
| 195 | if ($curr->hasMethod('HashPath')) { |
||
| 196 | $link = $curr->HashPath($link, false); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | if ($image->Title) { |
||
| 201 | $imageClasses = Config::inst()->get(PerfectCMSImages::class, 'perfect_cms_images_append_title_to_image_links_classes'); |
||
| 202 | if (in_array($image->ClassName, $imageClasses, true)) { |
||
| 203 | $link .= '?title=' . urlencode(Convert::raw2att($image->Title)); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return $link; |
||
| 208 | } |
||
| 209 | |||
| 210 | public static function web_p_enabled(): bool |
||
| 223 | } |
||
| 224 | } |
||
| 225 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: