Conditions | 13 |
Paths | 200 |
Total Lines | 77 |
Code Lines | 52 |
Lines | 28 |
Ratio | 36.36 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
12 | function resize($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight, $ratio = FALSE, $upsize = TRUE){ |
||
13 | |||
14 | // First, calculate the height. |
||
15 | $height = intval($newWidth / $imgWidth * $imgHeight); |
||
16 | |||
17 | // If the height is too large, set it to the maximum height and calculate the width. |
||
18 | View Code Duplication | if ($height > $newHeight) { |
|
|
|||
19 | |||
20 | $height = $newHeight; |
||
21 | $newWidth = intval($height / $imgHeight * $imgWidth); |
||
22 | } |
||
23 | |||
24 | // If we don't allow upsizing check if the new width or height are too big. |
||
25 | if (!$upsize) { |
||
26 | // If the given width is larger then the image height, then resize it. |
||
27 | View Code Duplication | if ($newWidth > $imgWidth) { |
|
28 | $newWidth = $imgWidth; |
||
29 | $height = intval($newWidth / $imgWidth * $imgHeight); |
||
30 | } |
||
31 | |||
32 | // If the given height is larger then the image height, then resize it. |
||
33 | View Code Duplication | if ($height > $imgHeight) { |
|
34 | $height = $imgHeight; |
||
35 | $newWidth = intval($height / $imgHeight * $imgWidth); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | if ($ratio == true) |
||
40 | { |
||
41 | $source_aspect_ratio = $imgWidth / $imgHeight; |
||
42 | $thumbnail_aspect_ratio = $newWidth / $newHeight; |
||
43 | if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) { |
||
44 | $newWidth = $imgWidth; |
||
45 | $newHeight = $imgHeight; |
||
46 | } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) { |
||
47 | $newWidth = (int) ($newHeight * $source_aspect_ratio); |
||
48 | $newHeight = $newHeight; |
||
49 | } else { |
||
50 | $newWidth = $newWidth; |
||
51 | $newHeight = (int) ($newWidth / $source_aspect_ratio); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $imgString = file_get_contents($image); |
||
56 | |||
57 | $imageFromString = imagecreatefromstring($imgString); |
||
58 | $tmp = imagecreatetruecolor($newWidth, $newHeight); |
||
59 | imagecopyresampled( |
||
60 | $tmp, |
||
61 | $imageFromString, |
||
62 | 0, |
||
63 | 0, |
||
64 | 0, |
||
65 | 0, |
||
66 | $newWidth, |
||
67 | $newHeight, |
||
68 | $imgWidth, |
||
69 | $imgHeight |
||
70 | ); |
||
71 | |||
72 | View Code Duplication | switch ($mimeType) { |
|
73 | case "jpeg": |
||
74 | case "jpg": |
||
75 | imagejpeg($tmp, $image, 90); |
||
76 | break; |
||
77 | case "png": |
||
78 | imagepng($tmp, $image, 0); |
||
79 | break; |
||
80 | case "gif": |
||
81 | imagegif($tmp, $image); |
||
82 | break; |
||
83 | default: |
||
84 | throw new \Exception(" Only jpg, jpeg, png and gif files can be resized "); |
||
85 | break; |
||
86 | } |
||
87 | |||
88 | } |
||
89 | |||
91 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.