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 PodsField_OEmbed 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 PodsField_OEmbed, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PodsField_OEmbed extends PodsField { |
||
6 | |||
7 | /** |
||
8 | * Field Type Group |
||
9 | * |
||
10 | * @var string |
||
11 | * @since 2.0 |
||
12 | */ |
||
13 | public static $group = 'Relationships / Media'; |
||
14 | |||
15 | /** |
||
16 | * Field Type Identifier |
||
17 | * |
||
18 | * @var string |
||
19 | * @since 2.0 |
||
20 | */ |
||
21 | public static $type = 'oembed'; |
||
22 | |||
23 | /** |
||
24 | * Field Type Label |
||
25 | * |
||
26 | * @var string |
||
27 | * @since 2.0 |
||
28 | */ |
||
29 | public static $label = 'oEmbed'; |
||
30 | |||
31 | /** |
||
32 | * Field Type Preparation |
||
33 | * |
||
34 | * @var string |
||
35 | * @since 2.0 |
||
36 | */ |
||
37 | public static $prepare = '%s'; |
||
38 | |||
39 | /** |
||
40 | * Available oEmbed providers |
||
41 | * |
||
42 | * @var array |
||
43 | * @since 2.7 |
||
44 | */ |
||
45 | private $providers = array(); |
||
46 | |||
47 | /** |
||
48 | * Current embed width |
||
49 | * |
||
50 | * @var int |
||
51 | * @since 2.7 |
||
52 | */ |
||
53 | private $width = 0; |
||
54 | |||
55 | /** |
||
56 | * Current embed height |
||
57 | * |
||
58 | * @var int |
||
59 | * @since 2.7 |
||
60 | */ |
||
61 | private $height = 0; |
||
62 | |||
63 | /** |
||
64 | * Do things like register/enqueue scripts and stylesheets |
||
65 | * |
||
66 | * @since 2.0 |
||
67 | */ |
||
68 | public function __construct () { |
||
71 | |||
72 | /** |
||
73 | * Add admin_init actions |
||
74 | * |
||
75 | * @since 2.3 |
||
76 | */ |
||
77 | public function admin_init() { |
||
81 | |||
82 | /** |
||
83 | * Add options and set defaults to |
||
84 | * |
||
85 | * @return array |
||
86 | * @since 2.0 |
||
87 | */ |
||
88 | public function options () { |
||
154 | |||
155 | /** |
||
156 | * Define the current field's schema for DB table storage |
||
157 | * |
||
158 | * @param array $options |
||
159 | * |
||
160 | * @return array |
||
161 | * @since 2.0 |
||
162 | */ |
||
163 | public function schema ( $options = null ) { |
||
168 | |||
169 | /** |
||
170 | * Change the way the value of the field is displayed with Pods::get |
||
171 | * |
||
172 | * @param mixed $value |
||
173 | * @param string $name |
||
174 | * @param array $options |
||
175 | * @param array $fields |
||
|
|||
176 | * @param array $pod |
||
177 | * @param int $id |
||
178 | * |
||
179 | * @since 2.0 |
||
180 | */ |
||
181 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
216 | |||
217 | /** |
||
218 | * Customize output of the form field |
||
219 | * |
||
220 | * @param string $name |
||
221 | * @param mixed $value |
||
222 | * @param array $options |
||
223 | * @param array $pod |
||
224 | * @param int $id |
||
225 | * |
||
226 | * @since 2.0 |
||
227 | */ |
||
228 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
246 | |||
247 | /** |
||
248 | * Validate a value before it's saved |
||
249 | * |
||
250 | * @param mixed $value |
||
251 | * @param string $name |
||
252 | * @param array $options |
||
253 | * @param array $fields |
||
254 | * @param array $pod |
||
255 | * @param int $id |
||
256 | * |
||
257 | * @param null $params |
||
258 | * @return array|bool |
||
259 | * @since 2.0 |
||
260 | */ |
||
261 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
280 | |||
281 | /** |
||
282 | * Change the value or perform actions after validation but before saving to the DB |
||
283 | * |
||
284 | * @param mixed $value |
||
285 | * @param int $id |
||
286 | * @param string $name |
||
287 | * @param array $options |
||
288 | * @param array $fields |
||
289 | * @param array $pod |
||
290 | * @param object $params |
||
291 | * |
||
292 | * @return mixed|string |
||
293 | * @since 2.0 |
||
294 | */ |
||
295 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
311 | |||
312 | /** |
||
313 | * Customize the Pods UI manage table column output |
||
314 | * |
||
315 | * @param int $id |
||
316 | * @param mixed $value |
||
317 | * @param string $name |
||
318 | * @param array $options |
||
319 | * @param array $fields |
||
320 | * @param array $pod |
||
321 | * |
||
322 | * @return mixed|string |
||
323 | * @since 2.0 |
||
324 | */ |
||
325 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
330 | |||
331 | /** |
||
332 | * Strip HTML based on options |
||
333 | * |
||
334 | * @param string $value |
||
335 | * @param array $options |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function strip_html ( $value, $options = null ) { |
||
353 | |||
354 | /** |
||
355 | * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
||
356 | * |
||
357 | * @see WP_Embed::autoembed() |
||
358 | * @see WP_Embed::autoembed_callback() |
||
359 | * |
||
360 | * @uses PodsField_OEmbed::autoembed_callback() |
||
361 | * |
||
362 | * @param string $content The content to be searched. |
||
363 | * @return string Potentially modified $content. |
||
364 | * |
||
365 | * @since 2.7 |
||
366 | */ |
||
367 | public function autoembed( $content ) { |
||
379 | |||
380 | /** |
||
381 | * Callback function for {@link WP_Embed::autoembed()}. |
||
382 | * |
||
383 | * @param array $match A regex match array. |
||
384 | * @return string The embed shortcode |
||
385 | * |
||
386 | * @since 2.7 |
||
387 | */ |
||
388 | public function autoembed_callback( $match ) { |
||
395 | |||
396 | /** |
||
397 | * Get a list of available providers from the WP_oEmbed class |
||
398 | * |
||
399 | * @see wp-includes/class-oembed.php |
||
400 | * @return array $providers { |
||
401 | * Array of provider data with regex as key |
||
402 | * |
||
403 | * @type string URL for this provider |
||
404 | * @type int |
||
405 | * @type string Hostname for this provider |
||
406 | * } |
||
407 | * |
||
408 | * @since 2.7 |
||
409 | */ |
||
410 | public function get_providers() { |
||
447 | |||
448 | /** |
||
449 | * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one. |
||
450 | * This function is ripped from WP since Pods has support from 3.8 and in the WP core this function is 4.0+ |
||
451 | * We've stripped the autodiscover part from this function to keep it basic |
||
452 | * |
||
453 | * @since 2.7 |
||
454 | * @access public |
||
455 | * |
||
456 | * @see WP_oEmbed::get_provider() |
||
457 | * |
||
458 | * @param string $url The URL to the content. |
||
459 | * @return false|string False on failure, otherwise the oEmbed provider URL. |
||
460 | */ |
||
461 | public function get_provider( $url ) { |
||
490 | |||
491 | /** |
||
492 | * Validate a value with the enabled oEmbed providers (if required) |
||
493 | * |
||
494 | * @since 2.7 |
||
495 | * @param string $value |
||
496 | * @param array $options Field options |
||
497 | * @return bool |
||
498 | */ |
||
499 | public function validate_provider( $value, $options ) { |
||
536 | |||
537 | /** |
||
538 | * Handle update preview AJAX |
||
539 | * |
||
540 | * @since 2.7 |
||
541 | */ |
||
542 | public function admin_ajax_oembed_update_preview() { |
||
577 | |||
578 | } |
||
579 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.