Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Image_Transform_Driver_Imagick3 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image_Transform_Driver_Imagick3, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Image_Transform_Driver_Imagick3 extends Image_Transform |
||
47 | { |
||
48 | /** |
||
49 | * Instance of imagick |
||
50 | * @var Imagick |
||
51 | */ |
||
52 | var $imagick = null; |
||
53 | |||
54 | /** |
||
55 | * Handler of the image resource before |
||
56 | * the last transformation |
||
57 | * @var array |
||
58 | */ |
||
59 | var $oldImage; |
||
60 | |||
61 | /** |
||
62 | * @see __construct() |
||
63 | */ |
||
64 | function Image_Transform_Driver_Imagick3() |
||
68 | |||
69 | /** |
||
70 | * @see http://www.imagemagick.org/www/formats.html |
||
71 | */ |
||
72 | function __construct() |
||
81 | |||
82 | /** |
||
83 | * Loads an image |
||
84 | * |
||
85 | * @param string $image filename |
||
86 | * |
||
87 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
88 | * @access public |
||
89 | */ |
||
90 | function load($image) |
||
111 | |||
112 | /** |
||
113 | * Resizes the image |
||
114 | * |
||
115 | * @param integer $new_x New width |
||
116 | * @param integer $new_y New height |
||
117 | * @param mixed $options Optional parameters |
||
118 | * <ul> |
||
119 | * <li>'scaleMethod': "pixel" or "smooth"</li> |
||
120 | * </ul> |
||
121 | * |
||
122 | * @return bool|PEAR_Error TRUE or PEAR_Error object on error |
||
123 | * @access protected |
||
124 | */ |
||
125 | function _resize($new_x, $new_y, $options = null) |
||
143 | |||
144 | /** |
||
145 | * Rotates the current image |
||
146 | * |
||
147 | * @param float $angle Rotation angle in degree |
||
148 | * @param array $options Supported options: |
||
149 | * <ul> |
||
150 | * <li>'canvasColor' : array(r ,g, b), named color or #rrggbb</li> |
||
151 | * </ul> |
||
152 | * |
||
153 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
154 | * @access public |
||
155 | */ |
||
156 | function rotate($angle, $options = null) |
||
179 | |||
180 | /** |
||
181 | * Adds text to the image |
||
182 | * |
||
183 | * @param array $params Array contains options: |
||
184 | * <ul> |
||
185 | * <li>'text' (string) The string to draw</li> |
||
186 | * <li>'x' (integer) Horizontal position</li> |
||
187 | * <li>'y' (integer) Vertical Position</li> |
||
188 | * <li>'Color' (mixed) Font color</li> |
||
189 | * <li>'font' (string) Font to be used</li> |
||
190 | * <li>'size' (integer) Size of the fonts in pixel</li> |
||
191 | * </ul> |
||
192 | * |
||
193 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
194 | * @access public |
||
195 | */ |
||
196 | function addText($params) |
||
228 | |||
229 | |||
230 | /** |
||
231 | * Saves the image to a file |
||
232 | * |
||
233 | * @param $filename string the name of the file to write to |
||
234 | * |
||
235 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
236 | * @access public |
||
237 | */ |
||
238 | View Code Duplication | function save($filename, $type = '', $quality = null) |
|
272 | |||
273 | /** |
||
274 | * Displays image without saving and lose changes |
||
275 | * |
||
276 | * This method adds the Content-type HTTP header |
||
277 | * |
||
278 | * @param string type (JPG,PNG...); |
||
279 | * @param int quality 75 |
||
280 | * |
||
281 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
282 | * @access public |
||
283 | */ |
||
284 | View Code Duplication | function display($type = '', $quality = null) |
|
314 | |||
315 | /** |
||
316 | * Adjusts the image gamma |
||
317 | * |
||
318 | * @param float $outputgamma |
||
319 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
320 | * @access public |
||
321 | */ |
||
322 | function gamma($outputgamma = 1.0) { |
||
328 | |||
329 | /** |
||
330 | * Crops the image |
||
331 | * |
||
332 | * @param integer $width Cropped image width |
||
333 | * @param integer $height Cropped image height |
||
334 | * @param integer $x X-coordinate to crop at |
||
335 | * @param integer $y Y-coordinate to crop at |
||
336 | * |
||
337 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
338 | * @access public |
||
339 | */ |
||
340 | View Code Duplication | function crop($width, $height, $x = 0, $y = 0) |
|
361 | |||
362 | /** |
||
363 | * Converts the image to greyscale |
||
364 | * |
||
365 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
366 | * @access public |
||
367 | */ |
||
368 | function greyscale() { |
||
376 | |||
377 | /** |
||
378 | * Horizontal mirroring |
||
379 | * |
||
380 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
381 | * @access public |
||
382 | */ |
||
383 | function mirror() |
||
393 | |||
394 | /** |
||
395 | * Vertical mirroring |
||
396 | * |
||
397 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
398 | * @access public |
||
399 | */ |
||
400 | function flip() |
||
411 | |||
412 | /** |
||
413 | * Destroy image handle |
||
414 | * |
||
415 | * @access public |
||
416 | */ |
||
417 | function free() |
||
424 | |||
425 | /** |
||
426 | * RaiseError Method - shows imagick Raw errors. |
||
427 | * |
||
428 | * @param string $message message = prefixed message.. |
||
429 | * @param int $code error code |
||
430 | * @return PEAR error object |
||
431 | * @access protected |
||
432 | */ |
||
433 | function raiseError($message, $code = 0) |
||
437 | } |
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.