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