| Conditions | 23 |
| Paths | 580 |
| Total Lines | 73 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 34 | public static function get_image_link($image, string $name, ?bool $useRetina = null, ?bool $forMobile = null, ?int $resizeToWidth = 0): string |
||
| 35 | { |
||
| 36 | $cacheKey = $image->ClassName . '_' . $image->ID . '_' . $name . '_' . ($useRetina ? 'Y' : 'N') . '_' . ($forMobile ? 'MY' : 'MN'); |
||
| 37 | if (empty(self::$imageLinkCache[$cacheKey])) { |
||
| 38 | //work out perfect width and height |
||
| 39 | if (null === $useRetina) { |
||
| 40 | $useRetina = PerfectCMSImages::use_retina($name); |
||
| 41 | } |
||
| 42 | $crop = PerfectCMSImages::is_crop($name); |
||
| 43 | |||
| 44 | $multiplier = PerfectCMSImages::get_multiplier($useRetina); |
||
| 45 | $perfectWidth = PerfectCMSImages::get_width($name, true); |
||
| 46 | $perfectHeight = PerfectCMSImages::get_height($name, true); |
||
| 47 | |||
| 48 | if ($forMobile) { |
||
| 49 | $perfectWidth = PerfectCMSImages::get_mobile_width($name, true); |
||
| 50 | $perfectHeight = PerfectCMSImages::get_mobile_height($name, true); |
||
| 51 | } |
||
| 52 | |||
| 53 | $perfectWidth *= $multiplier; |
||
| 54 | $perfectHeight *= $multiplier; |
||
| 55 | |||
| 56 | //get current width and height |
||
| 57 | $myWidth = $image->getWidth(); |
||
| 58 | $myHeight = $image->getHeight(); |
||
| 59 | |||
| 60 | //if we are trying to resize to a width that is small than the perfect width |
||
| 61 | //and the resize width is small than the current width, then lets resize... |
||
| 62 | if (0 !== (int) $resizeToWidth) { |
||
| 63 | if ($resizeToWidth < $perfectWidth && $resizeToWidth < $myWidth) { |
||
| 64 | $perfectWidth = $resizeToWidth; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | if ($perfectWidth && $perfectHeight) { |
||
|
|
|||
| 68 | //if the height or the width are already perfect then we can not do anything about it. |
||
| 69 | if ($myWidth === $perfectWidth && $myHeight === $perfectHeight) { |
||
| 70 | $link = $image->Link(); |
||
| 71 | } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
||
| 72 | $link = $image->Pad( |
||
| 73 | $perfectWidth, |
||
| 74 | $perfectHeight, |
||
| 75 | PerfectCMSImages::get_padding_bg_colour($name) |
||
| 76 | )->Link(); |
||
| 77 | } elseif ($crop) { |
||
| 78 | $link = $image->Fill($perfectWidth, $perfectHeight)->Link(); |
||
| 79 | } else { |
||
| 80 | $link = $image->FitMax($perfectWidth, $perfectHeight)->Link(); |
||
| 81 | } |
||
| 82 | } elseif ($perfectWidth) { |
||
| 83 | if ($myWidth === $perfectWidth) { |
||
| 84 | $link = $image->Link(); |
||
| 85 | } elseif ($crop) { |
||
| 86 | $link = $image->Fill($perfectWidth, $myHeight)->Link(); |
||
| 87 | } else { |
||
| 88 | $link = $image->ScaleWidth($perfectWidth)->Link(); |
||
| 89 | } |
||
| 90 | } elseif ($perfectHeight) { |
||
| 91 | if ($myHeight === $perfectHeight) { |
||
| 92 | $link = $image->Link(); |
||
| 93 | } elseif ($crop) { |
||
| 94 | $link = $image->Fill($myWidth, $perfectHeight)->Link(); |
||
| 95 | } else { |
||
| 96 | $link = $image->ScaleHeight($perfectHeight)->Link(); |
||
| 97 | } |
||
| 98 | } elseif ($forMobile) { |
||
| 99 | $link = ''; |
||
| 100 | } else { |
||
| 101 | $link = $image->Link(); |
||
| 102 | } |
||
| 103 | self::$imageLinkCache[$cacheKey] = $link; |
||
| 104 | } |
||
| 105 | |||
| 106 | return self::$imageLinkCache[$cacheKey]; |
||
| 107 | } |
||
| 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: