| Conditions | 37 |
| Paths | 15360 |
| Total Lines | 164 |
| 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 |
||
| 34 | function imagebmp($img, $file = '', $RLE = 0) |
||
| 35 | { |
||
| 36 | $ColorCount = imagecolorstotal($img); |
||
| 37 | |||
| 38 | $Transparent = imagecolortransparent($img); |
||
| 39 | $IsTransparent = $Transparent !== -1; |
||
| 40 | |||
| 41 | |||
| 42 | if ($IsTransparent) { |
||
| 43 | $ColorCount--; |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($ColorCount == 0) { |
||
| 47 | $ColorCount = 0; |
||
| 48 | $BitCount = 24; |
||
| 49 | }; |
||
| 50 | if (($ColorCount > 0) and ($ColorCount <= 2)) { |
||
| 51 | $ColorCount = 2; |
||
| 52 | $BitCount = 1; |
||
| 53 | }; |
||
| 54 | if (($ColorCount > 2) and ($ColorCount <= 16)) { |
||
| 55 | $ColorCount = 16; |
||
| 56 | $BitCount = 4; |
||
| 57 | }; |
||
| 58 | if (($ColorCount > 16) and ($ColorCount <= 256)) { |
||
| 59 | $ColorCount = 0; |
||
| 60 | $BitCount = 8; |
||
| 61 | }; |
||
| 62 | |||
| 63 | |||
| 64 | $Width = imagesx($img); |
||
| 65 | $Height = imagesy($img); |
||
| 66 | |||
| 67 | $Zbytek = (4 - ($Width / (8 / $BitCount)) % 4) % 4; |
||
|
|
|||
| 68 | |||
| 69 | if ($BitCount < 24) { |
||
| 70 | $palsize = pow(2, $BitCount) * 4; |
||
| 71 | } |
||
| 72 | |||
| 73 | $size = (floor($Width / (8 / $BitCount)) + $Zbytek) * $Height + 54; |
||
| 74 | $size += $palsize; |
||
| 75 | $offset = 54 + $palsize; |
||
| 76 | |||
| 77 | // Bitmap File Header |
||
| 78 | $ret = 'BM'; // header (2b) |
||
| 79 | $ret .= int_to_dword($size); // size of file (4b) |
||
| 80 | $ret .= int_to_dword(0); // reserved (4b) |
||
| 81 | $ret .= int_to_dword($offset); // byte location in the file which is first byte of IMAGE (4b) |
||
| 82 | // Bitmap Info Header |
||
| 83 | $ret .= int_to_dword(40); // Size of BITMAPINFOHEADER (4b) |
||
| 84 | $ret .= int_to_dword($Width); // width of bitmap (4b) |
||
| 85 | $ret .= int_to_dword($Height); // height of bitmap (4b) |
||
| 86 | $ret .= int_to_word(1); // biPlanes = 1 (2b) |
||
| 87 | $ret .= int_to_word($BitCount); // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b) |
||
| 88 | $ret .= int_to_dword($RLE); // RLE COMPRESSION (4b) |
||
| 89 | $ret .= int_to_dword(0); // width x height (4b) |
||
| 90 | $ret .= int_to_dword(0); // biXPelsPerMeter (4b) |
||
| 91 | $ret .= int_to_dword(0); // biYPelsPerMeter (4b) |
||
| 92 | $ret .= int_to_dword(0); // Number of palettes used (4b) |
||
| 93 | $ret .= int_to_dword(0); // Number of important colour (4b) |
||
| 94 | // image data |
||
| 95 | |||
| 96 | $CC = $ColorCount; |
||
| 97 | $sl1 = strlen($ret); |
||
| 98 | if ($CC == 0) { |
||
| 99 | $CC = 256; |
||
| 100 | } |
||
| 101 | if ($BitCount < 24) { |
||
| 102 | $ColorTotal = imagecolorstotal($img); |
||
| 103 | if ($IsTransparent) { |
||
| 104 | $ColorTotal--; |
||
| 105 | } |
||
| 106 | |||
| 107 | for ($p = 0; $p < $ColorTotal; $p++) { |
||
| 108 | $color = imagecolorsforindex($img, $p); |
||
| 109 | $ret .= inttobyte($color['blue']); |
||
| 110 | $ret .= inttobyte($color['green']); |
||
| 111 | $ret .= inttobyte($color['red']); |
||
| 112 | $ret .= inttobyte(0); //RESERVED |
||
| 113 | } |
||
| 114 | |||
| 115 | $CT = $ColorTotal; |
||
| 116 | for ($p = $ColorTotal; $p < $CC; $p++) { |
||
| 117 | $ret .= inttobyte(0); |
||
| 118 | $ret .= inttobyte(0); |
||
| 119 | $ret .= inttobyte(0); |
||
| 120 | $ret .= inttobyte(0); //RESERVED |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | if ($BitCount <= 8) { |
||
| 126 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
| 127 | $bWrite = ''; |
||
| 128 | for ($x = 0; $x < $Width; $x++) { |
||
| 129 | $color = imagecolorat($img, $x, $y); |
||
| 130 | $bWrite .= decbinx($color, $BitCount); |
||
| 131 | if (strlen($bWrite) == 8) { |
||
| 132 | $retd .= inttobyte(bindec($bWrite)); |
||
| 133 | $bWrite = ''; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if ((strlen($bWrite) < 8) && (strlen($bWrite) != 0)) { |
||
| 138 | $sl = strlen($bWrite); |
||
| 139 | for ($t = 0; $t < 8 - $sl; $t++) { |
||
| 140 | $sl .= '0'; |
||
| 141 | } |
||
| 142 | $retd .= inttobyte(bindec($bWrite)); |
||
| 143 | }; |
||
| 144 | for ($z = 0; $z < $Zbytek; $z++) { |
||
| 145 | $retd .= inttobyte(0); |
||
| 146 | } |
||
| 147 | }; |
||
| 148 | }; |
||
| 149 | |||
| 150 | if (($RLE == 1) && ($BitCount == 8)) { |
||
| 151 | for ($t = 0; $t < strlen($retd); $t += 4) { |
||
| 152 | if ($t != 0) { |
||
| 153 | if (($t) % $Width == 0) { |
||
| 154 | $ret .= chr(0) . chr(0); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if (($t + 5) % $Width == 0) { |
||
| 159 | $ret .= chr(0) . chr(5) . substr($retd, $t, 5) . chr(0); |
||
| 160 | $t += 1; |
||
| 161 | } |
||
| 162 | if (($t + 6) % $Width == 0) { |
||
| 163 | $ret .= chr(0) . chr(6) . substr($retd, $t, 6); |
||
| 164 | $t += 2; |
||
| 165 | } else { |
||
| 166 | $ret .= chr(0) . chr(4) . substr($retd, $t, 4); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | $ret .= chr(0) . chr(1); |
||
| 170 | } else { |
||
| 171 | $ret .= $retd; |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | if ($BitCount == 24) { |
||
| 176 | for ($z = 0; $z < $Zbytek; $z++) { |
||
| 177 | $Dopl .= chr(0); |
||
| 178 | } |
||
| 179 | |||
| 180 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
| 181 | for ($x = 0; $x < $Width; $x++) { |
||
| 182 | $color = imagecolorsforindex($img, imagecolorat($img, $x, $y)); |
||
| 183 | $ret .= chr($color['blue']) . chr($color['green']) . chr($color['red']); |
||
| 184 | } |
||
| 185 | $ret .= $Dopl; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($file != '') { |
||
| 190 | $r = ($f = fopen($file, 'wb')); |
||
| 191 | $r = $r and fwrite($f, $ret); |
||
| 192 | $r = $r and fclose($f); |
||
| 193 | |||
| 194 | return $r; |
||
| 195 | } |
||
| 196 | echo $ret; |
||
| 197 | } |
||
| 198 | |||
| 604 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: