| Conditions | 22 |
| Paths | 9505 |
| Total Lines | 171 |
| Code Lines | 101 |
| Lines | 15 |
| Ratio | 8.77 % |
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 |
||
| 58 | function MakePie($filename, $pieWidth, $pieHeight, $ShadowDistance, $pieBackgroundColor, $EQpieData, $legend) |
||
| 59 | { |
||
| 60 | if (!function_exists("imagecreatetruecolor")) { |
||
| 61 | die("Error, GD Library 2 needed."); |
||
| 62 | } |
||
| 63 | |||
| 64 | //set some limitations |
||
| 65 | if ($pieWidth < 100 | $pieWidth > 500) { |
||
| 66 | $pieWidth = 100; |
||
| 67 | } |
||
| 68 | if ($pieHeight < 100 | $pieHeight > 500) { |
||
| 69 | $pieHeight = 100; |
||
| 70 | } |
||
| 71 | if ($ShadowDistance < 1 | $ShadowDistance > 50) { |
||
| 72 | $ShadowDistance = 10; |
||
| 73 | } |
||
| 74 | |||
| 75 | $pieWidth = $pieWidth * 3; |
||
| 76 | $pieHeight = $pieHeight * 3; |
||
| 77 | $ShadowDistance = $ShadowDistance * 3; |
||
| 78 | $pieBackgroundColor = $pieBackgroundColor; |
||
| 79 | |||
| 80 | $pie = @ImageCreateTrueColor($pieWidth, $pieHeight + $ShadowDistance); |
||
| 81 | |||
| 82 | $colR = hexdec(substr($pieBackgroundColor, 1, 2)); |
||
| 83 | $colG = hexdec(substr($pieBackgroundColor, 3, 2)); |
||
| 84 | $colB = hexdec(substr($pieBackgroundColor, 5, 2)); |
||
| 85 | $pieBG = ImageColorAllocate($pie, $colR, $colG, $colB); |
||
| 86 | ImageFill($pie, 0, 0, $pieBG); |
||
| 87 | |||
| 88 | // get the total value for percentage calculations |
||
| 89 | $this->total = 0; |
||
| 90 | |||
| 91 | $maxStringLenght = 0; |
||
| 92 | foreach ($EQpieData as $i => $value) { |
||
| 93 | $this->total += $value[1]; |
||
| 94 | if (strlen($value[0]) > $maxStringLenght) { |
||
| 95 | $maxStringLenght = strlen($value[0]); |
||
| 96 | } |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | $pieParts = $i + 1; |
||
| 101 | reset($EQpieData); |
||
| 102 | $legendWidth = (($legend > 0) ? ImageFontWidth(2) * ($maxStringLenght + 6) + 40 : 0); |
||
| 103 | |||
| 104 | // the first pie-part starts with offset in degrees up from horizantal right, looks better this way |
||
| 105 | $pieStart = 135; |
||
| 106 | |||
| 107 | foreach ($EQpieData as $i => $value) { |
||
| 108 | |||
| 109 | // the name for each part is $value[0] |
||
| 110 | // the value for each part is $value[1] |
||
| 111 | // the color for each part is $value[2] |
||
| 112 | |||
| 113 | $piePart = $value[1]; |
||
| 114 | View Code Duplication | if (isset($this->total) && $this->total > 0) { |
|
| 115 | $piePart100 = round(($piePart / $this->total * 100), 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
| 116 | } else { |
||
| 117 | $piePart100 = 0; |
||
| 118 | } |
||
| 119 | |||
| 120 | $piePart360 = $piePart100 * 3.6; // in degrees |
||
| 121 | |||
| 122 | $colR = hexdec(substr($value[2], 1, 2)); |
||
| 123 | $colG = hexdec(substr($value[2], 3, 2)); |
||
| 124 | $colB = hexdec(substr($value[2], 5, 2)); |
||
| 125 | $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB); |
||
| 126 | |||
| 127 | $ShadowColR = (($colR > 79) ? $colR - 80 : 0); |
||
| 128 | $ShadowColG = (($colG > 79) ? $colG - 80 : 0); |
||
| 129 | $ShadowColB = (($colB > 79) ? $colB - 80 : 0); |
||
| 130 | |||
| 131 | $ShadowColor = ImageColorAllocate($pie, $ShadowColR, $ShadowColG, $ShadowColB); |
||
| 132 | |||
| 133 | //Here we create the shadow down-worths |
||
| 134 | for ($i = 0; $i < $ShadowDistance; ++$i) { |
||
| 135 | ImageFilledArc($pie, $pieWidth / 2, ($pieHeight / 2 + $i), $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL); |
||
| 136 | } |
||
| 137 | |||
| 138 | $pieStart = $pieStart + $piePart360; |
||
| 139 | |||
| 140 | } |
||
| 141 | reset($EQpieData); |
||
| 142 | |||
| 143 | $pieStart = 135; |
||
| 144 | |||
| 145 | foreach ($EQpieData as $i => $value) { |
||
| 146 | |||
| 147 | $piePart = $value[1]; |
||
| 148 | View Code Duplication | if (isset($this->total) && $this->total > 0) { |
|
| 149 | $piePart100 = round(($piePart / $this->total * 100), 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
| 150 | } else { |
||
| 151 | $piePart100 = 0; |
||
| 152 | } |
||
| 153 | $piePart360 = $piePart100 * 3.6; // in degrees |
||
| 154 | |||
| 155 | $colR = hexdec(substr($value[2], 1, 2)); |
||
| 156 | $colG = hexdec(substr($value[2], 3, 2)); |
||
| 157 | $colB = hexdec(substr($value[2], 5, 2)); |
||
| 158 | $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB); |
||
| 159 | |||
| 160 | //Here we create the real pie chart |
||
| 161 | ImageFilledArc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $PartColor, IMG_ARC_PIE); |
||
| 162 | |||
| 163 | $pieStart = $pieStart + $piePart360; |
||
| 164 | |||
| 165 | } |
||
| 166 | reset($EQpieData); |
||
| 167 | |||
| 168 | // create final pie picture with proper background color |
||
| 169 | $finalPie = ImageCreateTrueColor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3); |
||
| 170 | ImageFill($finalPie, 0, 0, $pieBG); |
||
| 171 | |||
| 172 | // resample with pieGraph inside (3x smaller) |
||
| 173 | ImageCopyResampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, ($pieHeight + $ShadowDistance)); |
||
| 174 | |||
| 175 | // Create the ledgend ... |
||
| 176 | if ($legendWidth > 0) { |
||
| 177 | // Legend Box |
||
| 178 | $leg_width = $legendWidth - 10; |
||
| 179 | $leg_height = $pieParts * (ImageFontHeight(2) + 2) + 2; |
||
| 180 | $legendImage = ImageCreateTrueColor($leg_width, $leg_height); |
||
| 181 | //ImageFill($legendImage, 0, 0, $pieBG); |
||
| 182 | |||
| 183 | $borderColor = ImageColorAllocate($pie, '155', '155', '155'); |
||
| 184 | $boxColor = ImageColorAllocate($pie, '255', '255', '255'); |
||
| 185 | $textColor = ImageColorAllocate($pie, '55', '55', '55'); |
||
| 186 | |||
| 187 | ImageFilledRectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor); |
||
| 188 | ImageRectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor); |
||
| 189 | |||
| 190 | $box_width = ImageFontHeight(2) - 5; |
||
| 191 | $box_height = ImageFontHeight(2) - 5; |
||
| 192 | $yOffset = 2; |
||
| 193 | |||
| 194 | foreach ($EQpieData as $i => $value) { |
||
| 195 | |||
| 196 | $piePart = $value[1]; |
||
| 197 | View Code Duplication | if (isset($this->total) && $this->total > 0) { |
|
| 198 | $piePart100 = round(($piePart / $this->total * 100), 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
| 199 | } else { |
||
| 200 | $piePart100 = 0; |
||
| 201 | } |
||
| 202 | $colR = hexdec(substr($value[2], 1, 2)); |
||
| 203 | $colG = hexdec(substr($value[2], 3, 2)); |
||
| 204 | $colB = hexdec(substr($value[2], 5, 2)); |
||
| 205 | $PartColor = ImageColorAllocate($legendImage, $colR, $colG, $colB); |
||
| 206 | |||
| 207 | ImageFilledRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor); |
||
| 208 | ImageRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor); |
||
| 209 | |||
| 210 | $text = $value[0] . " " . $piePart100 . "%"; |
||
| 211 | ImageString($legendImage, 2, '20', $yOffset, $text, $textColor); |
||
| 212 | $yOffset = $yOffset + 15; |
||
| 213 | |||
| 214 | } |
||
| 215 | |||
| 216 | reset($EQpieData); // reset pointer in array to first |
||
| 217 | |||
| 218 | ImageCopyResampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height); |
||
| 219 | ImageDestroy($legendImage); |
||
| 220 | |||
| 221 | } |
||
| 222 | header('Content-type: image/png'); |
||
| 223 | imagepng($finalPie, $filename); |
||
| 224 | ImageDestroy($pie); |
||
| 225 | ImageDestroy($finalPie); |
||
| 226 | |||
| 227 | return; |
||
| 228 | } |
||
| 229 | |||
| 231 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.