| Total Complexity | 47 |
| Total Lines | 329 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PerfectCmsImageDataExtension 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 PerfectCmsImageDataExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class PerfectCmsImageDataExtension extends DataExtension |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * background image for padded images... |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private static $perfect_cms_images_background_padding_color = '#cccccc'; |
||
| 29 | |||
| 30 | /* |
||
| 31 | * details of the images |
||
| 32 | * - width: 3200 |
||
| 33 | * - height: 3200 |
||
| 34 | * - folder: "myfolder" |
||
| 35 | * - filetype: "try jpg" |
||
| 36 | * - enforce_size: false |
||
| 37 | * - folder: my-image-folder-a |
||
| 38 | * - filetype: "jpg or a png with a transparant background" |
||
| 39 | * - use_retina: true |
||
| 40 | * - padding_bg_colour: '#dddddd' |
||
| 41 | * - crop: true |
||
| 42 | * - move_to_right_folder: true |
||
| 43 | * - loading_style: 'eager' |
||
| 44 | * - used_by: |
||
| 45 | * - MyClass.MyHasOne |
||
| 46 | * - MyOtherClass.MyHasManyMethod |
||
| 47 | * - MyOtherClass.MyManyManyRel |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $perfect_cms_images_image_definitions = []; |
||
| 51 | |||
| 52 | private static $casting = [ |
||
| 53 | 'PerfectCMSImageTag' => 'HTMLText', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $name PerfectCMSImages name |
||
| 58 | * @param bool $inline for use within existing image tag - optional |
||
| 59 | * @param string $alt alt tag for image -optional |
||
| 60 | * @param string $attributes additional attributes |
||
| 61 | * |
||
| 62 | * @return string (HTML) |
||
| 63 | */ |
||
| 64 | public function getPerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $name PerfectCMSImages name |
||
| 71 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 72 | * @param string $alt alt tag for image -optional |
||
| 73 | * @param string $attributes additional attributes |
||
| 74 | * |
||
| 75 | * @return string (HTML) |
||
| 76 | */ |
||
| 77 | public function PerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 78 | { |
||
| 79 | $arrayData = $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); |
||
| 80 | $template = 'Includes/PerfectCMSImageTag'; |
||
| 81 | if (true === $inline || 1 === (int) $inline || 'true' === strtolower($inline)) { |
||
| 82 | $template .= 'Inline'; |
||
| 83 | } |
||
| 84 | |||
| 85 | return DBField::create_field('HTMLText', $arrayData->renderWith($template)); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $name PerfectCMSImages name |
||
| 90 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 91 | * @param string $alt alt tag for image -optional |
||
| 92 | * @param string $attributes additional attributes |
||
| 93 | * |
||
| 94 | * @return ArrayData |
||
| 95 | */ |
||
| 96 | public function PerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $name PerfectCMSImages name |
||
| 103 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 104 | * @param string $alt alt tag for image -optional |
||
| 105 | * @param string $attributes additional attributes |
||
| 106 | * |
||
| 107 | * @return ArrayData |
||
| 108 | */ |
||
| 109 | public function getPerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 110 | { |
||
| 111 | $retinaLink = $this->PerfectCMSImageLinkRetina($name); |
||
| 112 | $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name); |
||
| 113 | |||
| 114 | $width = PerfectCMSImages::get_width($name, true); |
||
| 115 | $height = PerfectCMSImages::get_height($name, true); |
||
| 116 | |||
| 117 | $hasWebP = (bool) Config::inst()->get(ImageManipulations::class, 'webp_enabled'); |
||
| 118 | $hasMobile = PerfectCMSImages::has_mobile($name); |
||
| 119 | $mobileRetinaLink = ''; |
||
| 120 | $mobileNonRetinaLink = ''; |
||
| 121 | $mobileMediaWidth = ''; |
||
| 122 | |||
| 123 | $retinaLink = $this->PerfectCMSImageLinkRetina($name); |
||
| 124 | $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name); |
||
| 125 | if ($hasMobile) { |
||
| 126 | $mobileRetinaLink = $this->PerfectCMSImageLinkRetinaForMobile($name); |
||
| 127 | $mobileNonRetinaLink = $this->PerfectCMSImageLinkNonRetinaForMobile($name); |
||
| 128 | } |
||
| 129 | if ($hasWebP) { |
||
| 130 | $retinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebP($name); |
||
| 131 | $nonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebP($name); |
||
| 132 | if ($hasMobile) { |
||
| 133 | $mobileRetinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebPForMobile($name); |
||
| 134 | $mobileNonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebPForMobile($name); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($hasMobile) { |
||
| 139 | $mobileMediaWidth = PerfectCMSImages::get_mobile_media_width($name); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (! $alt) { |
||
| 143 | $alt = $this->getOwner()->Title; |
||
| 144 | } |
||
| 145 | |||
| 146 | return ArrayData::create( |
||
| 147 | [ |
||
| 148 | 'MobileMediaWidth' => $mobileMediaWidth, |
||
| 149 | 'Width' => $width, |
||
| 150 | 'Height' => $height, |
||
| 151 | 'Alt' => Convert::raw2att($alt), |
||
| 152 | 'MobileRetinaLink' => $mobileRetinaLink, |
||
| 153 | 'MobileNonRetinaLink' => $mobileNonRetinaLink, |
||
| 154 | 'RetinaLink' => $retinaLink, |
||
| 155 | 'NonRetinaLink' => $nonRetinaLink, |
||
| 156 | 'MobileRetinaLinkWebP' => $mobileRetinaLinkWebP, |
||
| 157 | 'MobileNonRetinaLinkWebP' => $mobileNonRetinaLinkWebP, |
||
| 158 | 'RetinaLinkWebP' => $retinaLinkWebP, |
||
| 159 | 'NonRetinaLinkWebP' => $nonRetinaLinkWebP, |
||
| 160 | 'Type' => $this->owner->getMimeType(), |
||
| 161 | 'HasWebP' => $hasWebP, |
||
| 162 | 'Attributes' => DBField::create_field('HTMLText', $attributes), |
||
| 163 | ] |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $name of Image Field template |
||
| 169 | * |
||
| 170 | * @return string (link) |
||
| 171 | */ |
||
| 172 | public function PerfectCMSImageLinkNonRetina(string $name): string |
||
| 173 | { |
||
| 174 | return $this->PerfectCMSImageLink($name); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $name of Image Field template |
||
| 179 | * |
||
| 180 | * @return string (link) |
||
| 181 | */ |
||
| 182 | public function PerfectCMSImageLinkRetina(string $name): string |
||
| 183 | { |
||
| 184 | return $this->PerfectCMSImageLink($name, true); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $name of Image Field template |
||
| 189 | * |
||
| 190 | * @return string (link) |
||
| 191 | */ |
||
| 192 | public function PerfectCMSImageLinkNonRetinaWebP(string $name): string |
||
| 193 | { |
||
| 194 | return $this->PerfectCMSImageLink($name, false, true); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $name of Image Field template |
||
| 199 | * |
||
| 200 | * @return string (link) |
||
| 201 | */ |
||
| 202 | public function PerfectCMSImageLinkRetinaWebP(string $name): string |
||
| 203 | { |
||
| 204 | return $this->PerfectCMSImageLink($name, true, true); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $name of Image Field template |
||
| 209 | * |
||
| 210 | * @return string (link) |
||
| 211 | */ |
||
| 212 | public function PerfectCMSImageLinkNonRetinaForMobile(string $name): string |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $name of Image Field template |
||
| 219 | * |
||
| 220 | * @return string (link) |
||
| 221 | */ |
||
| 222 | public function PerfectCMSImageLinkRetinaForMobile(string $name): string |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $name of Image Field template |
||
| 229 | * |
||
| 230 | * @return string (link) |
||
| 231 | */ |
||
| 232 | public function PerfectCMSImageLinkNonRetinaWebPForMobile(string $name): string |
||
| 233 | { |
||
| 234 | return $this->PerfectCMSImageLink($name, false, true, true); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $name of Image Field template |
||
| 239 | * |
||
| 240 | * @return string (link) |
||
| 241 | */ |
||
| 242 | public function PerfectCMSImageLinkRetinaWebPForMobile(string $name): string |
||
| 243 | { |
||
| 244 | return $this->PerfectCMSImageLink($name, true, true, true); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string (link) |
||
| 249 | */ |
||
| 250 | public function getPerfectCMSImageAbsoluteLink(string $link): string |
||
| 251 | { |
||
| 252 | return Director::absoluteURL($link); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * returns image link (if any). |
||
| 257 | */ |
||
| 258 | public function PerfectCMSImageLink(string $name, ?bool $useRetina = false, ?bool $isWebP = false, ?bool $forMobile = false): string |
||
| 259 | { |
||
| 260 | /** @var null|Image $image */ |
||
| 261 | $image = $this->owner; |
||
| 262 | $allOk = false; |
||
| 263 | if ($image && $image->exists() && $image instanceof Image) { |
||
| 264 | $allOk = true; |
||
| 265 | //we are all good ... |
||
| 266 | } else { |
||
| 267 | $image = ImageManipulations::get_backup_image($name); |
||
| 268 | if ($image && $image->exists() && $image instanceof Image) { |
||
| 269 | $allOk = true; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if ($allOk) { |
||
| 274 | // $backEndString = Image::get_backend(); |
||
| 275 | // $backend = Injector::inst()->get($backEndString); |
||
| 276 | $link = ImageManipulations::get_image_link($image, $name, $useRetina, $forMobile); |
||
| 277 | |||
| 278 | if ($isWebP) { |
||
| 279 | $link = ImageManipulations::web_p_link($link); |
||
| 280 | } |
||
| 281 | |||
| 282 | return $link ? ImageManipulations::add_fake_parts($image, $link) : ''; |
||
| 283 | } else { |
||
| 284 | // no image -> provide placeholder if in DEV MODE only!!! |
||
| 285 | if (Director::isDev()) { |
||
| 286 | return ImageManipulations::get_placeholder_image_tag($name); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | // no image -> provide placeholder if in DEV MODE only!!! |
||
| 291 | if (Director::isDev()) { |
||
| 292 | return ImageManipulations::get_placeholder_image_tag($name); |
||
| 293 | } |
||
| 294 | |||
| 295 | return ''; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function PerfectCMSImageFixFolder($name, ?string $folderName = ''): ?Folder |
||
| 299 | { |
||
| 300 | $folder = null; |
||
| 301 | if (PerfectCMSImages::move_to_right_folder($name) || $folderName) { |
||
| 302 | $image = $this->getOwner(); |
||
| 303 | if ($image) { |
||
| 304 | if (! $folderName) { |
||
| 305 | $folderName = PerfectCMSImages::get_folder($name); |
||
| 306 | } |
||
| 307 | $folder = Folder::find_or_make($folderName); |
||
| 308 | if (! $folder->ID) { |
||
| 309 | $folder->write(); |
||
| 310 | } |
||
| 311 | if ($image->ParentID !== $folder->ID) { |
||
| 312 | $wasPublished = $image->isPublished() && ! $image->isModifiedOnDraft(); |
||
| 313 | $image->ParentID = $folder->ID; |
||
| 314 | $image->write(); |
||
| 315 | if ($wasPublished) { |
||
| 316 | $image->publishRecursive(); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | // user_error('could not find image'); |
||
| 321 | } |
||
| 322 | |||
| 323 | return $folder; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function getThumbnail() |
||
| 327 | { |
||
| 328 | if ($this->owner->ID) { |
||
| 329 | if ('svg' === $this->owner->getExtension()) { |
||
| 330 | $obj = DBHTMLText::create(); |
||
| 331 | $obj->setValue(file_get_contents(BASE_PATH . $this->owner->Link())); |
||
| 332 | |||
| 333 | return $obj; |
||
| 334 | } |
||
| 335 | |||
| 336 | return $this->owner->CMSThumbnail(); |
||
| 337 | } |
||
| 338 | |||
| 339 | return $this->owner->CMSThumbnail(); |
||
| 340 | } |
||
| 341 | |||
| 342 | public function updatePreviewLink(&$link, $action) |
||
| 350 | } |
||
| 351 | } |
||
| 352 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths