Complex classes like VideoPress_Video 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 VideoPress_Video, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class VideoPress_Video { |
||
| 8 | public $version = 3; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Manifest version returned by remote service. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | * @since 1.3 |
||
| 15 | */ |
||
| 16 | const manifest_version = '1.5'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Expiration of the video expressed in Unix time |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | * @since 1.3 |
||
| 23 | */ |
||
| 24 | public $expires; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * VideoPress unique identifier |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | * @since 1.3 |
||
| 31 | */ |
||
| 32 | public $guid; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * WordPress.com blog identifier |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | * @since 1.5 |
||
| 39 | */ |
||
| 40 | public $blog_id; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Remote blog attachment identifier |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | * @since 1.5 |
||
| 47 | */ |
||
| 48 | public $post_id; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Maximum desired width. |
||
| 52 | * |
||
| 53 | * @var int |
||
| 54 | * @since 1.3 |
||
| 55 | */ |
||
| 56 | public $maxwidth; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Video width calculated based on original video dimensions and the requested maxwidth |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | * @since 1.3 |
||
| 63 | */ |
||
| 64 | public $calculated_width; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Video height calculated based on original video dimensions and the requested maxwidth |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | * @since 1.3 |
||
| 71 | */ |
||
| 72 | public $calculated_height; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Video title |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | * @since 1.3 |
||
| 79 | */ |
||
| 80 | public $title; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Video description |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | * @since 4.4 |
||
| 87 | */ |
||
| 88 | public $description; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Directionality of title text. ltr or rtl |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | * @since 1.3 |
||
| 95 | */ |
||
| 96 | public $text_direction; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Text and audio language as ISO 639-2 language code |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | * @since 1.3 |
||
| 103 | */ |
||
| 104 | public $language; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Video duration in whole seconds |
||
| 108 | * |
||
| 109 | * @var int |
||
| 110 | * @since 1.3 |
||
| 111 | */ |
||
| 112 | public $duration; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Recommended minimum age of the viewer. |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | * @since 1.3 |
||
| 119 | */ |
||
| 120 | public $age_rating; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Video author has restricted video embedding or sharing |
||
| 124 | * |
||
| 125 | * @var bool |
||
| 126 | * @since 1.3 |
||
| 127 | */ |
||
| 128 | public $restricted_embed; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Poster frame image URI for the given video guid and calculated dimensions. |
||
| 132 | * |
||
| 133 | * @var string |
||
| 134 | * @since 1.3 |
||
| 135 | */ |
||
| 136 | public $poster_frame_uri; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Video files associated with the given guid for the calculated dimensions. |
||
| 140 | * |
||
| 141 | * @var stdClass |
||
| 142 | * @since 1.3 |
||
| 143 | */ |
||
| 144 | public $videos; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Video player information |
||
| 148 | * |
||
| 149 | * @var stdClass |
||
| 150 | * @since 1.3 |
||
| 151 | */ |
||
| 152 | public $players; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Video player skinning preferences including background color and watermark |
||
| 156 | * |
||
| 157 | * @var array |
||
| 158 | * @since 1.5 |
||
| 159 | */ |
||
| 160 | public $skin; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI |
||
| 164 | * |
||
| 165 | * @var array |
||
| 166 | * @since 1.5 |
||
| 167 | */ |
||
| 168 | public $captions; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Setup the object. |
||
| 172 | * Request video information from VideoPress servers and process the response. |
||
| 173 | * |
||
| 174 | * @since 1.3 |
||
| 175 | * @var string $guid VideoPress unique identifier |
||
| 176 | * @var int $maxwidth maximum requested video width. final width and height are calculated on VideoPress servers based on the aspect ratio of the original video upload. |
||
| 177 | */ |
||
| 178 | public function __construct( $guid, $maxwidth = 640 ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Convert an Expires HTTP header value into Unix time for use in WP Cache |
||
| 282 | * |
||
| 283 | * @since 1.3 |
||
| 284 | * @var string $expires_header |
||
| 285 | * @return int|bool Unix time or false |
||
| 286 | */ |
||
| 287 | public static function calculate_expiration( $expires_header ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds. |
||
| 311 | * |
||
| 312 | * @since 1.2 |
||
| 313 | * @param string $url absolute URL |
||
| 314 | * @return bool|string host component of the URL, or false if none found |
||
| 315 | */ |
||
| 316 | public static function hostname( $url ) { |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname. |
||
| 323 | * |
||
| 324 | * @since 1.3 |
||
| 325 | * @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful |
||
| 326 | */ |
||
| 327 | private function get_data() { |
||
| 375 | } |
||
| 376 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: