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:
1 | <?php |
||
35 | class Image_Transform_Driver_Cairowrapper extends Image_Transform |
||
36 | { |
||
37 | var $surface = null; |
||
38 | |||
39 | /** |
||
40 | * Supported image types |
||
41 | * |
||
42 | * @var array |
||
43 | * @access protected |
||
44 | */ |
||
45 | var $_supported_image_types = array( |
||
46 | 'png' => 'rw' |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * Check settings |
||
51 | */ |
||
52 | function Image_Transform_Driver_Cairowrapper() |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Create object and check if cairo_wrapper is loaded |
||
61 | */ |
||
62 | function __construct() |
||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * Loads an image from file |
||
78 | * |
||
79 | * @param string $image filename |
||
80 | * |
||
81 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
82 | * |
||
83 | * @access public |
||
84 | */ |
||
85 | function load($image) |
||
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * Resize the image |
||
113 | * |
||
114 | * @param int $new_x New width |
||
115 | * @param int $new_y New height |
||
116 | * @param array $options Optional parameters |
||
117 | * |
||
118 | * @return bool|PEAR_Error TRUE on success or PEAR_Error object on error |
||
119 | * |
||
120 | * @access protected |
||
121 | */ |
||
122 | function _resize($new_x, $new_y, $options = null) |
||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * Saves the scaled image into a file. |
||
164 | * |
||
165 | * @param string $filename The filename to save to |
||
166 | * @param mixed $type ignored |
||
167 | * @param mixed $quality ignored |
||
168 | * |
||
169 | * @return bool|PEAR_Error TRUE on success or PEAR_Error object on error |
||
170 | * |
||
171 | * @access public |
||
172 | */ |
||
173 | function save($filename, $type = null, $quality = null) |
||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * Returns the surface of the image so it can be modified further |
||
184 | * |
||
185 | * @return resource |
||
186 | * |
||
187 | * @access public |
||
188 | */ |
||
189 | function getHandle() |
||
193 | |||
194 | |||
195 | |||
196 | /** |
||
197 | * Frees cairo handles |
||
198 | * |
||
199 | * @return void |
||
200 | * |
||
201 | * @access public |
||
202 | */ |
||
203 | function free() |
||
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * Mirrors the image vertically |
||
216 | * Uses an affine transformation matrix to flip the image. |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | View Code Duplication | function flip() |
|
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * Mirrors the image horizontally. |
||
243 | * Uses an affine transformation matrix to mirror the image. |
||
244 | * |
||
245 | * 123 -> 321 |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | View Code Duplication | function mirror() |
|
267 | |||
268 | }//class Image_Transform_Driver_Cairowrapper extends Image_Transform |
||
269 | ?> |
||
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.