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