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 () { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add admin_init actions |
||
| 73 | * |
||
| 74 | * @since 2.3 |
||
| 75 | */ |
||
| 76 | public function admin_init() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Add options and set defaults to |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | * @since 2.0 |
||
| 86 | */ |
||
| 87 | public function options () { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Define the current field's schema for DB table storage |
||
| 156 | * |
||
| 157 | * @param array $options |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | * @since 2.0 |
||
| 161 | */ |
||
| 162 | public function schema ( $options = null ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Change the way the value of the field is displayed with Pods::get |
||
| 170 | * |
||
| 171 | * @param mixed $value |
||
| 172 | * @param string $name |
||
| 173 | * @param array $options |
||
| 174 | * @param array $fields |
||
|
|
|||
| 175 | * @param array $pod |
||
| 176 | * @param int $id |
||
| 177 | * |
||
| 178 | * @since 2.0 |
||
| 179 | */ |
||
| 180 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Customize output of the form field |
||
| 218 | * |
||
| 219 | * @param string $name |
||
| 220 | * @param mixed $value |
||
| 221 | * @param array $options |
||
| 222 | * @param array $pod |
||
| 223 | * @param int $id |
||
| 224 | * |
||
| 225 | * @since 2.0 |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Validate a value before it's saved |
||
| 248 | * |
||
| 249 | * @param mixed $value |
||
| 250 | * @param string $name |
||
| 251 | * @param array $options |
||
| 252 | * @param array $fields |
||
| 253 | * @param array $pod |
||
| 254 | * @param int $id |
||
| 255 | * |
||
| 256 | * @param null $params |
||
| 257 | * @return array|bool |
||
| 258 | * @since 2.0 |
||
| 259 | */ |
||
| 260 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Change the value or perform actions after validation but before saving to the DB |
||
| 282 | * |
||
| 283 | * @param mixed $value |
||
| 284 | * @param int $id |
||
| 285 | * @param string $name |
||
| 286 | * @param array $options |
||
| 287 | * @param array $fields |
||
| 288 | * @param array $pod |
||
| 289 | * @param object $params |
||
| 290 | * |
||
| 291 | * @return mixed|string |
||
| 292 | * @since 2.0 |
||
| 293 | */ |
||
| 294 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Customize the Pods UI manage table column output |
||
| 313 | * |
||
| 314 | * @param int $id |
||
| 315 | * @param mixed $value |
||
| 316 | * @param string $name |
||
| 317 | * @param array $options |
||
| 318 | * @param array $fields |
||
| 319 | * @param array $pod |
||
| 320 | * |
||
| 321 | * @return mixed|string |
||
| 322 | * @since 2.0 |
||
| 323 | */ |
||
| 324 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Strip HTML based on options |
||
| 332 | * |
||
| 333 | * @param string $value |
||
| 334 | * @param array $options |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function strip_html ( $value, $options = null ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
||
| 355 | * |
||
| 356 | * @see WP_Embed::autoembed() |
||
| 357 | * @see WP_Embed::autoembed_callback() |
||
| 358 | * |
||
| 359 | * @uses PodsField_OEmbed::autoembed_callback() |
||
| 360 | * |
||
| 361 | * @param string $content The content to be searched. |
||
| 362 | * @return string Potentially modified $content. |
||
| 363 | * |
||
| 364 | * @since 2.7 |
||
| 365 | */ |
||
| 366 | public function autoembed( $content ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Callback function for {@link WP_Embed::autoembed()}. |
||
| 381 | * |
||
| 382 | * @param array $match A regex match array. |
||
| 383 | * @return string The embed shortcode |
||
| 384 | * |
||
| 385 | * @since 2.7 |
||
| 386 | */ |
||
| 387 | public function autoembed_callback( $match ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get a list of available providers from the WP_oEmbed class |
||
| 397 | * |
||
| 398 | * @see wp-includes/class-oembed.php |
||
| 399 | * @return array $providers { |
||
| 400 | * Array of provider data with regex as key |
||
| 401 | * |
||
| 402 | * @type string URL for this provider |
||
| 403 | * @type int |
||
| 404 | * @type string Hostname for this provider |
||
| 405 | * } |
||
| 406 | * |
||
| 407 | * @since 2.7 |
||
| 408 | */ |
||
| 409 | public function get_providers() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one. |
||
| 449 | * This function is ripped from WP since Pods has support from 3.8 and in the WP core this function is 4.0+ |
||
| 450 | * We've stripped the autodiscover part from this function to keep it basic |
||
| 451 | * |
||
| 452 | * @since 2.7 |
||
| 453 | * @access public |
||
| 454 | * |
||
| 455 | * @see WP_oEmbed::get_provider() |
||
| 456 | * |
||
| 457 | * @param string $url The URL to the content. |
||
| 458 | * @return false|string False on failure, otherwise the oEmbed provider URL. |
||
| 459 | */ |
||
| 460 | public function get_provider( $url ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Validate a value with the enabled oEmbed providers (if required) |
||
| 492 | * |
||
| 493 | * @since 2.7 |
||
| 494 | * @param string $value |
||
| 495 | * @param array $options Field options |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | public function validate_provider( $value, $options ) { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Handle update preview AJAX |
||
| 538 | * |
||
| 539 | * @since 2.7 |
||
| 540 | */ |
||
| 541 | public function admin_ajax_oembed_update_preview() { |
||
| 576 | |||
| 577 | } |
||
| 578 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.