| Conditions | 14 |
| Paths | 196 |
| Total Lines | 88 |
| Code Lines | 52 |
| Lines | 12 |
| Ratio | 13.64 % |
| 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 |
||
| 86 | public static function imagecreatefromstring($bin_data) |
||
| 87 | { |
||
| 88 | if (!trim($bin_data)) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | $bin_pos = 0; |
||
| 93 | $header = substr($bin_data, $bin_pos, 18); |
||
| 94 | $bin_pos += 18; |
||
| 95 | $header = unpack( "cimage_id_len/ccolor_map_type/cimage_type/vcolor_map_origin/vcolor_map_len/" . |
||
| 96 | "ccolor_map_entry_size/vx_origin/vy_origin/vwidth/vheight/" . |
||
| 97 | "cpixel_size/cdescriptor", $header); |
||
| 98 | |||
| 99 | switch ($header['image_type']) { |
||
| 100 | case 2: //no palette, uncompressed |
||
| 101 | case 10: //no palette, rle |
||
| 102 | break; |
||
| 103 | default: return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($header['pixel_size'] != 24) { |
||
| 107 | return false; |
||
| 108 | //die('Unsupported TGA color depth'); |
||
| 109 | } |
||
| 110 | |||
| 111 | $bytes = $header['pixel_size'] / 8; |
||
| 112 | |||
| 113 | if ($header['image_id_len'] > 0) { |
||
| 114 | $header['image_id'] = substr($bin_data, $bin_pos, $header['image_id_len']); |
||
| 115 | $bin_pos += $header['image_id_len']; |
||
| 116 | } else { |
||
| 117 | $header['image_id'] = ''; |
||
| 118 | } |
||
| 119 | |||
| 120 | $im = imagecreatetruecolor($header['width'], $header['height']); |
||
| 121 | |||
| 122 | $size = $header['width'] * $header['height'] * 3; |
||
| 123 | |||
| 124 | //-- check whether this is NEW TGA or not |
||
| 125 | $pos = $bin_pos; |
||
| 126 | $bin_pos = strlen($bin_data) - 26; |
||
| 127 | $newtga = substr($bin_data, $bin_pos, 26); |
||
| 128 | |||
| 129 | if (substr($newtga, 8, 16) != 'TRUEVISION-XFILE') { |
||
| 130 | $newtga = false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $bin_pos = strlen($bin_data); |
||
| 134 | $datasize = $bin_pos - $pos; |
||
| 135 | |||
| 136 | if ($newtga) { |
||
| 137 | $datasize -= 26; |
||
| 138 | } |
||
| 139 | |||
| 140 | $bin_pos = $pos; |
||
| 141 | |||
| 142 | //-- end of check |
||
| 143 | $data = substr($bin_data, $bin_pos, $datasize); |
||
| 144 | $bin_pos += $datasize; |
||
| 145 | |||
| 146 | if ($header['image_type'] == 10) { |
||
| 147 | $data = self::rle_decode($data, $size); |
||
| 148 | } |
||
| 149 | |||
| 150 | $reverse = self::bit5($header['descriptor']) == 1; |
||
| 151 | $pixels = str_split($data, 3); |
||
| 152 | |||
| 153 | $i = 0; |
||
| 154 | |||
| 155 | //read pixels |
||
| 156 | if ($reverse) { |
||
| 157 | View Code Duplication | for ($y = 0; $y < $header['height']; $y++) { |
|
| 158 | for ($x = 0; $x < $header['width']; $x++) { |
||
| 159 | imagesetpixel($im, $x, $y, self::dwordize($pixels[$i])); |
||
| 160 | $i++; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | View Code Duplication | for ($y = $header['height']-1; $y >= 0; $y--) { |
|
| 165 | for ($x=0; $x<$header['width']; $x++) { |
||
| 166 | imagesetpixel($im, $x, $y, self::dwordize($pixels[$i])); |
||
| 167 | $i++; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return $im; |
||
| 173 | } |
||
| 174 | |||
| 194 |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.