Conditions | 31 |
Paths | 360 |
Total Lines | 172 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
96 | public static function resizeMulti(\Imagick $source, array $boxSizes, array $options = []) : array |
||
97 | { |
||
98 | //algorithm inspired from http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html |
||
99 | //use of 2x2 binning is arguably the best quality one will get downsizing and is what lots of hardware does in |
||
100 | //the photography field, while being reasonably fast. Upsizing is more subjective but you can't get much |
||
101 | //better than bicubic which is what is used here. |
||
102 | |||
103 | $options = $options + self::DEFAULT_OPTIONS; |
||
104 | $color = $options['color']; |
||
105 | Util::ensure(true, is_string($color), InvalidArgumentException::class, ['$options["color"] was not a string']); |
||
106 | |||
107 | $upsize = $options['upsize']; |
||
108 | Util::ensure(true, is_bool($upsize), InvalidArgumentException::class, ['$options["upsize"] was not a bool']); |
||
109 | |||
110 | $bestfit = $options['bestfit']; |
||
111 | Util::ensure(true, is_bool($bestfit), InvalidArgumentException::class, ['$options["bestfit"] was not a bool']); |
||
112 | |||
113 | $blurBackground = $options['blurBackground']; |
||
114 | Util::ensure( |
||
115 | true, |
||
116 | is_bool($blurBackground), |
||
117 | InvalidArgumentException::class, |
||
118 | ['$options["blurBackground"] was not a bool'] |
||
119 | ); |
||
120 | |||
121 | $blurValue = $options['blurValue']; |
||
122 | Util::ensure( |
||
123 | true, |
||
124 | is_float($blurValue), |
||
125 | InvalidArgumentException::class, |
||
126 | ['$options["blurValue"] was not a float'] |
||
127 | ); |
||
128 | $maxWidth = $options['maxWidth']; |
||
129 | Util::ensure(true, is_int($maxWidth), InvalidArgumentException::class, ['$options["maxWidth"] was not an int']); |
||
130 | |||
131 | $maxHeight = $options['maxHeight']; |
||
132 | Util::ensure( |
||
133 | true, |
||
134 | is_int($maxHeight), |
||
135 | InvalidArgumentException::class, |
||
136 | ['$options["maxHeight"] was not an int'] |
||
137 | ); |
||
138 | |||
139 | foreach ($boxSizes as $boxSizeKey => $boxSize) { |
||
140 | if (!isset($boxSize['width']) || !is_int($boxSize['width'])) { |
||
141 | throw new InvalidArgumentException('a width in a $boxSizes value was not an int'); |
||
142 | } |
||
143 | |||
144 | if (!isset($boxSize['height']) || !is_int($boxSize['height'])) { |
||
145 | throw new InvalidArgumentException('a height in a $boxSizes value was not an int'); |
||
146 | } |
||
147 | |||
148 | if ($boxSize['width'] > $maxWidth || $boxSize['width'] <= 0) { |
||
149 | throw new InvalidArgumentException('a $boxSizes width was not between 0 and $options["maxWidth"]'); |
||
150 | } |
||
151 | |||
152 | if ($boxSize['height'] > $maxHeight || $boxSize['height'] <= 0) { |
||
153 | throw new InvalidArgumentException('a $boxSizes height was not between 0 and $options["maxHeight"]'); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $results = []; |
||
158 | $cloneCache = []; |
||
159 | foreach ($boxSizes as $boxSizeKey => $boxSize) { |
||
160 | $boxWidth = $boxSize['width']; |
||
161 | $boxHeight = $boxSize['height']; |
||
162 | |||
163 | $clone = clone $source; |
||
164 | |||
165 | self::rotateImage($clone); |
||
166 | |||
167 | $width = $clone->getImageWidth(); |
||
168 | $height = $clone->getImageHeight(); |
||
169 | |||
170 | //ratio over 1 is horizontal, under 1 is vertical |
||
171 | $boxRatio = $boxWidth / $boxHeight; |
||
172 | //height should be positive since I didnt find a way you could get zero into imagick |
||
173 | $originalRatio = $width / $height; |
||
174 | |||
175 | $targetWidth = null; |
||
|
|||
176 | $targetHeight = null; |
||
177 | $targetX = null; |
||
178 | $targetY = null; |
||
179 | if ($width < $boxWidth && $height < $boxHeight && !$upsize) { |
||
180 | $targetWidth = $width; |
||
181 | $targetHeight = $height; |
||
182 | $targetX = ($boxWidth - $width) / 2; |
||
183 | $targetY = ($boxHeight - $height) / 2; |
||
184 | } else { |
||
185 | //if box is more vertical than original |
||
186 | if ($boxRatio < $originalRatio) { |
||
187 | $targetWidth = $boxWidth; |
||
188 | $targetHeight = (int)((double)$boxWidth / $originalRatio); |
||
189 | $targetX = 0; |
||
190 | $targetY = ($boxHeight - $targetHeight) / 2; |
||
191 | } else { |
||
192 | $targetWidth = (int)((double)$boxHeight * $originalRatio); |
||
193 | $targetHeight = $boxHeight; |
||
194 | $targetX = ($boxWidth - $targetWidth) / 2; |
||
195 | $targetY = 0; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | //do iterative downsize by halfs (2x2 binning is a common name) on dimensions that are bigger than target |
||
200 | //width and height |
||
201 | while (true) { |
||
202 | $widthReduced = false; |
||
203 | $widthIsHalf = false; |
||
204 | if ($width > $targetWidth) { |
||
205 | $width = (int)($width / 2); |
||
206 | $widthReduced = true; |
||
207 | $widthIsHalf = true; |
||
208 | if ($width < $targetWidth) { |
||
209 | $width = $targetWidth; |
||
210 | $widthIsHalf = false; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | $heightReduced = false; |
||
215 | $heightIsHalf = false; |
||
216 | if ($height > $targetHeight) { |
||
217 | $height = (int)($height / 2); |
||
218 | $heightReduced = true; |
||
219 | $heightIsHalf = true; |
||
220 | if ($height < $targetHeight) { |
||
221 | $height = $targetHeight; |
||
222 | $heightIsHalf = false; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | if (!$widthReduced && !$heightReduced) { |
||
227 | break; |
||
228 | } |
||
229 | |||
230 | $cacheKey = "{$width}x{$height}"; |
||
231 | if (isset($cloneCache[$cacheKey])) { |
||
232 | $clone = clone $cloneCache[$cacheKey]; |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | if ($clone->resizeImage($width, $height, \Imagick::FILTER_BOX, 1.0) !== true) { |
||
237 | //cumbersome to test |
||
238 | throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
||
239 | } |
||
240 | |||
241 | if ($widthIsHalf && $heightIsHalf) { |
||
242 | $cloneCache[$cacheKey] = clone $clone; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | if ($upsize && ($width < $targetWidth || $height < $targetHeight)) { |
||
247 | if ($clone->resizeImage($targetWidth, $targetHeight, \Imagick::FILTER_CUBIC, 1.0, $bestfit) !== true) { |
||
248 | //cumbersome to test |
||
249 | throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
||
250 | } |
||
251 | } |
||
252 | |||
253 | //put image in box |
||
254 | $canvas = self::getBackgroundCanvas($source, $color, $blurBackground, $blurValue, $boxWidth, $boxHeight); |
||
255 | if ($canvas->compositeImage($clone, \Imagick::COMPOSITE_ATOP, $targetX, $targetY) !== true) { |
||
256 | //cumbersome to test |
||
257 | throw new \Exception('Imagick::compositeImage() did not return true');//@codeCoverageIgnore |
||
258 | } |
||
259 | |||
260 | //reason we are not supporting the options in self::write() here is because format, and strip headers are |
||
261 | //only relevant once written Imagick::stripImage() doesnt even have an effect until written |
||
262 | //also the user can just call that function with the resultant $canvas |
||
263 | $results[$boxSizeKey] = $canvas; |
||
264 | } |
||
265 | |||
266 | return $results; |
||
267 | } |
||
268 | |||
437 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.