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 Images_Via_Imgix 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 Images_Via_Imgix, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Images_Via_Imgix { |
||
4 | |||
5 | /** |
||
6 | * The instance of the class. |
||
7 | * |
||
8 | * @var Images_Via_Imgix |
||
9 | */ |
||
10 | protected static $instance; |
||
11 | |||
12 | /** |
||
13 | * Plugin options |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $options = []; |
||
18 | |||
19 | /** |
||
20 | * Buffer is started by plugin and should be ended on shutdown. |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $buffer_started = false; |
||
25 | |||
26 | /** |
||
27 | * ImagesViaImgix constructor. |
||
28 | */ |
||
29 | public function __construct() { |
||
46 | |||
47 | /** |
||
48 | * Plugin loader instance. |
||
49 | * |
||
50 | * @return Images_Via_Imgix |
||
51 | */ |
||
52 | public static function instance() { |
||
59 | |||
60 | /** |
||
61 | * Set a single option. |
||
62 | * |
||
63 | * @param string $key |
||
64 | * @param mixed $value |
||
65 | */ |
||
66 | public function set_option( $key, $value ) { |
||
69 | |||
70 | /** |
||
71 | * Get a single option. |
||
72 | * |
||
73 | * @param string $key |
||
74 | * @param mixed $default |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function get_option( $key, $default = '' ) { |
||
80 | |||
81 | /** |
||
82 | * Override options from settings. |
||
83 | * Used in unit tests. |
||
84 | * |
||
85 | * @param array $options |
||
86 | */ |
||
87 | public function set_options( $options ) { |
||
90 | |||
91 | /** |
||
92 | * Find all img tags with sources matching "imgix.net" without the parameter |
||
93 | * "srcset" and add the "srcset" parameter to all those images, appending a new |
||
94 | * source using the "dpr=2" modifier. |
||
95 | * |
||
96 | * @param $content |
||
97 | * |
||
98 | * @return string Content with retina-enriched image tags. |
||
99 | */ |
||
100 | public function add_retina( $content ) { |
||
108 | |||
109 | /** |
||
110 | * Modify image urls for attachments to use imgix host. |
||
111 | * |
||
112 | * @param string $url |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function replace_image_url( $url ) { |
||
152 | |||
153 | /** |
||
154 | * Set params when running image_downsize |
||
155 | * |
||
156 | * @param false|array $return |
||
157 | * @param int $attachment_id |
||
158 | * @param string|array $size |
||
159 | * |
||
160 | * @return false|array |
||
161 | */ |
||
162 | public function image_downsize( $return, $attachment_id, $size ) { |
||
200 | |||
201 | /** |
||
202 | * Change url for images in srcset |
||
203 | * |
||
204 | * @param array $sources |
||
205 | * @param array $size_array |
||
206 | * @param string $image_src |
||
207 | * @param array $image_meta |
||
208 | * @param int $attachment_id |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public function calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) { |
||
228 | |||
229 | /** |
||
230 | * Modify image urls in content to use imgix host. |
||
231 | * |
||
232 | * @param $content |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function replace_images_in_content( $content ) { |
||
270 | |||
271 | /** |
||
272 | * Add tag to dns prefetch cdn host |
||
273 | */ |
||
274 | public function prefetch_cdn() { |
||
284 | |||
285 | /** |
||
286 | * Start output buffer if auto retina is enabled |
||
287 | */ |
||
288 | public function buffer_start_for_retina() { |
||
293 | |||
294 | /** |
||
295 | * Stop output buffer if it was enabled by the plugin |
||
296 | */ |
||
297 | public function buffer_end_for_retina() { |
||
302 | |||
303 | /** |
||
304 | * Returns a array of global parameters to be applied in all images, |
||
305 | * according to plugin's settings. |
||
306 | * |
||
307 | * @return array Global parameters to be appened at the end of each img URL. |
||
308 | */ |
||
309 | protected function get_global_params() { |
||
332 | |||
333 | /** |
||
334 | * Get all defined image sizes |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | protected function get_all_defined_sizes() { |
||
360 | } |
||
361 | |||
363 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.