| Conditions | 24 |
| Paths | 146 |
| Total Lines | 117 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 41 | public static function get_image_link($image, string $name, ?bool $useRetina = null, ?bool $forMobile = null, ?int $resizeToWidth = 0): string |
||
| 42 | { |
||
| 43 | $cacheKey = |
||
| 44 | implode( |
||
| 45 | '_', |
||
| 46 | array_filter( |
||
| 47 | [ |
||
| 48 | $image->ID, |
||
| 49 | $name, |
||
| 50 | ($useRetina ? 'Y' : 'N'), |
||
| 51 | ($forMobile ? 'MY' : 'MN'), |
||
| 52 | $resizeToWidth |
||
| 53 | ] |
||
| 54 | ) |
||
| 55 | ); |
||
| 56 | if (empty(self::$imageLinkCache[$cacheKey])) { |
||
| 57 | $item = DataObject::get_one(PerfectCMSImageCache::class, ['Code' => $cacheKey, 'ImageID' => $image->ID]); |
||
| 58 | if ($item) { |
||
| 59 | return $item->Link; |
||
|
|
|||
| 60 | } |
||
| 61 | $link = ''; |
||
| 62 | //work out perfect width and height |
||
| 63 | if (null === $useRetina) { |
||
| 64 | $useRetina = PerfectCMSImages::use_retina($name); |
||
| 65 | } |
||
| 66 | |||
| 67 | $crop = PerfectCMSImages::is_crop($name); |
||
| 68 | |||
| 69 | $multiplier = PerfectCMSImages::get_multiplier($useRetina); |
||
| 70 | $perfectWidth = (int) PerfectCMSImages::get_width($name, true); |
||
| 71 | $perfectHeight = (int) PerfectCMSImages::get_height($name, true); |
||
| 72 | |||
| 73 | if ($forMobile) { |
||
| 74 | $perfectWidth = (int) PerfectCMSImages::get_mobile_width($name, true); |
||
| 75 | $perfectHeight = (int) PerfectCMSImages::get_mobile_height($name, true); |
||
| 76 | } |
||
| 77 | |||
| 78 | $perfectWidth *= $multiplier; |
||
| 79 | $perfectHeight *= $multiplier; |
||
| 80 | |||
| 81 | //get current width and height |
||
| 82 | $myWidth = $image->getWidth(); |
||
| 83 | $myHeight = $image->getHeight(); |
||
| 84 | //if we are trying to resize to a width that is small than the perfect width |
||
| 85 | //and the resize width is small than the current width, then lets resize... |
||
| 86 | if (0 !== (int) $resizeToWidth) { |
||
| 87 | if ($resizeToWidth < $perfectWidth && $resizeToWidth < $myWidth) { |
||
| 88 | $perfectWidth = $resizeToWidth; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $link = null; |
||
| 93 | if ($perfectWidth && $perfectHeight) { |
||
| 94 | //if the height or the width are already perfect then we can not do anything about it. |
||
| 95 | if ($myWidth === $perfectWidth && $myHeight === $perfectHeight) { |
||
| 96 | $link = $image->getURL(); |
||
| 97 | } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
||
| 98 | $link = $image->getImageLinkCachedIfExists( |
||
| 99 | 'Pad', |
||
| 100 | $perfectWidth, |
||
| 101 | $perfectHeight, |
||
| 102 | PerfectCMSImages::get_padding_bg_colour($name) |
||
| 103 | ); |
||
| 104 | } elseif ($crop) { |
||
| 105 | $link = $image->getImageLinkCachedIfExists( |
||
| 106 | 'Fill', |
||
| 107 | $perfectWidth, |
||
| 108 | $perfectHeight |
||
| 109 | ); |
||
| 110 | } else { |
||
| 111 | $link = $image->getImageLinkCachedIfExists( |
||
| 112 | 'FitMax', |
||
| 113 | $perfectWidth, |
||
| 114 | $perfectHeight |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | } elseif ($perfectWidth) { |
||
| 118 | if ($myWidth === $perfectWidth) { |
||
| 119 | $link = $image->getURL(); |
||
| 120 | } elseif ($crop) { |
||
| 121 | $link = $image->getImageLinkCachedIfExists( |
||
| 122 | 'Fill', |
||
| 123 | $perfectWidth, |
||
| 124 | $myHeight |
||
| 125 | ); |
||
| 126 | } else { |
||
| 127 | $link = $image->getImageLinkCachedIfExists( |
||
| 128 | 'ScaleWidth', |
||
| 129 | $perfectWidth |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | } elseif ($perfectHeight) { |
||
| 133 | if ($myHeight === $perfectHeight) { |
||
| 134 | $link = $image->getUrl(); |
||
| 135 | } elseif ($crop) { |
||
| 136 | $link = $image->getImageLinkCachedIfExists( |
||
| 137 | 'Fill', |
||
| 138 | $myWidth, |
||
| 139 | $perfectHeight |
||
| 140 | ); |
||
| 141 | } else { |
||
| 142 | $link = $image->getImageLinkCachedIfExists( |
||
| 143 | 'ScaleHeight', |
||
| 144 | $perfectHeight |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | } elseif ($forMobile) { |
||
| 148 | // basically, it is for mobile and there is not perfect height nor perfect width |
||
| 149 | $link = ''; |
||
| 150 | } else { |
||
| 151 | // not for mobile, we definitely want to have some sort of link! |
||
| 152 | $link = $image->getUrl(); |
||
| 153 | } |
||
| 154 | self::$imageLinkCache[$cacheKey] = (string) $link; |
||
| 155 | } |
||
| 156 | |||
| 157 | return self::$imageLinkCache[$cacheKey]; |
||
| 158 | } |
||
| 294 |