Conditions | 31 |
Paths | 2304 |
Total Lines | 162 |
Code Lines | 110 |
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 |
||
88 | public function rotate($angle, $options = null) |
||
89 | { |
||
90 | if (null == $options) { |
||
91 | $autoresize = true; |
||
92 | $color_mask = array(255, 255, 0); |
||
93 | } else { |
||
94 | extract($options); |
||
95 | } |
||
96 | |||
97 | while ($angle <= -45) { |
||
98 | $angle += 360; |
||
99 | } |
||
100 | while ($angle > 270) { |
||
101 | $angle -= 360; |
||
102 | } |
||
103 | |||
104 | $t = deg2rad($angle); |
||
105 | |||
106 | if (!is_array($color_mask)) { |
||
107 | // Not already in numberical format, so we convert it. |
||
108 | if ('#' == $color_mask{0}) { |
||
109 | $color_mask = $this->colorhex2colorarray($color_mask); |
||
110 | } else { |
||
111 | require_once 'Image/Transform/Driver/ColorsDefs.php'; |
||
112 | $color_mask = isset($colornames[$color_mask]) ? $colornames[$color_mask] : false; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | // Do not round it, too much lost of quality |
||
117 | $cosT = cos($t); |
||
118 | $sinT = sin($t); |
||
119 | |||
120 | $img =& $this->imageHandle; |
||
121 | |||
122 | $width = $max_x = $this->img_x; |
||
123 | $height = $max_y = $this->img_y; |
||
124 | $min_y = 0; |
||
125 | $min_x = 0; |
||
126 | |||
127 | $x1 = round($max_x / 2, 0); |
||
128 | $y1 = round($max_y / 2, 0); |
||
129 | |||
130 | if ($autoresize) { |
||
131 | $t = abs($t); |
||
132 | $a = round($angle, 0); |
||
133 | switch ((int)($angle)) { |
||
134 | case 0: |
||
135 | $width2 = $width; |
||
136 | $height2 = $height; |
||
137 | break; |
||
138 | case 90: |
||
139 | $width2 = $height; |
||
140 | $height2 = $width; |
||
141 | break; |
||
142 | case 180: |
||
143 | $width2 = $width; |
||
144 | $height2 = $height; |
||
145 | break; |
||
146 | case 270: |
||
147 | $width2 = $height; |
||
148 | $height2 = $width; |
||
149 | break; |
||
150 | default: |
||
151 | $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); |
||
152 | $height2 = (int)(abs(cos($t) * $height + sin($t) * $width)); |
||
153 | } |
||
154 | |||
155 | $width2 -= $width2 % 2; |
||
156 | $height2 -= $height2 % 2; |
||
157 | |||
158 | $d_width = abs($width - $width2); |
||
159 | $d_height = abs($height - $height2); |
||
160 | $x_offset = $d_width / 2; |
||
161 | $y_offset = $d_height / 2; |
||
162 | $min_x2 = -abs($x_offset); |
||
163 | $min_y2 = -abs($y_offset); |
||
164 | $max_x2 = $width2; |
||
165 | $max_y2 = $height2; |
||
166 | } |
||
167 | |||
168 | $img2 = @imagecreateTrueColor($width2, $height2); |
||
169 | |||
170 | if (!is_resource($img2)) { |
||
171 | return PEAR::raiseError('Cannot create buffer for the rotataion.', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); |
||
172 | } |
||
173 | |||
174 | $this->img_x = $width2; |
||
175 | $this->img_y = $height2; |
||
176 | |||
177 | imagepalettecopy($img2, $img); |
||
178 | |||
179 | $mask = imagecolorresolve($img2, $color_mask[0], $color_mask[1], $color_mask[2]); |
||
180 | |||
181 | // use simple lines copy for axes angles |
||
182 | switch ((int)($angle)) { |
||
183 | case 0: |
||
184 | imagefill($img2, 0, 0, $mask); |
||
185 | for ($y = 0; $y < $max_y; $y++) { |
||
186 | for ($x = $min_x; $x < $max_x; $x++) { |
||
187 | $c = @imagecolorat($img, $x, $y); |
||
188 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
189 | } |
||
190 | } |
||
191 | break; |
||
192 | case 90: |
||
193 | imagefill($img2, 0, 0, $mask); |
||
194 | for ($x = $min_x; $x < $max_x; $x++) { |
||
195 | for ($y = $min_y; $y < $max_y; $y++) { |
||
196 | $c = imagecolorat($img, $x, $y); |
||
197 | imagesetpixel($img2, $max_y - $y - 1, $x, $c); |
||
198 | } |
||
199 | } |
||
200 | break; |
||
201 | case 180: |
||
202 | imagefill($img2, 0, 0, $mask); |
||
203 | for ($y = 0; $y < $max_y; $y++) { |
||
204 | for ($x = $min_x; $x < $max_x; $x++) { |
||
205 | $c = @imagecolorat($img, $x, $y); |
||
206 | imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c); |
||
207 | } |
||
208 | } |
||
209 | break; |
||
210 | case 270: |
||
211 | imagefill($img2, 0, 0, $mask); |
||
212 | for ($y = 0; $y < $max_y; $y++) { |
||
213 | for ($x = $max_x; $x >= $min_x; $x--) { |
||
214 | $c = @imagecolorat($img, $x, $y); |
||
215 | imagesetpixel($img2, $y, $max_x - $x - 1, $c); |
||
216 | } |
||
217 | } |
||
218 | break; |
||
219 | // simple reverse rotation algo |
||
220 | default: |
||
221 | $i = 0; |
||
222 | for ($y = $min_y2; $y < $max_y2; $y++) { |
||
223 | |||
224 | // Algebra :) |
||
225 | $x2 = round((($min_x2 - $x1) * $cosT) + (($y - $y1) * $sinT + $x1), 0); |
||
226 | $y2 = round((($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1), 0); |
||
227 | |||
228 | for ($x = $min_x2; $x < $max_x2; $x++) { |
||
229 | |||
230 | // Check if we are out of original bounces, if we are |
||
231 | // use the default color mask |
||
232 | if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) { |
||
233 | $c = imagecolorat($img, $x2, $y2); |
||
234 | } else { |
||
235 | $c = $mask; |
||
236 | } |
||
237 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
238 | |||
239 | // round verboten! |
||
240 | $x2 += $cosT; |
||
241 | $y2 -= $sinT; |
||
242 | } |
||
243 | } |
||
244 | break; |
||
245 | } |
||
246 | |||
247 | $this->imageHandle = $img2; |
||
248 | |||
249 | return true; |
||
250 | } |
||
252 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.