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 VimeoDataObject 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 VimeoDataObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class VimeoDataObject extends DataObject |
||
|
|
|||
| 10 | { |
||
| 11 | private static $db = array( |
||
| 12 | "Title" => "Varchar(100)", |
||
| 13 | "VimeoCode" => "Int", |
||
| 14 | "HTMLSnippet" => "HTMLText", |
||
| 15 | "Data" => "Text" |
||
| 16 | ); |
||
| 17 | |||
| 18 | private static $casting = array( |
||
| 19 | "FullName" => "Text", |
||
| 20 | "Icon" => "HTMLText", |
||
| 21 | "IconLink" => "Varchar", |
||
| 22 | "FullImage" => "HTMLText", |
||
| 23 | "FullImageLink" => "Varchar" |
||
| 24 | ); |
||
| 25 | |||
| 26 | private static $searchable_fields = array( |
||
| 27 | "Title" => "PartialMatchFilter", |
||
| 28 | "VimeoCode" |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $summary_fields = array( |
||
| 32 | "Icon" => "Icon", |
||
| 33 | "Title" => "Title", |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $singular_name = "Vimeo Video"; |
||
| 37 | |||
| 38 | private static $plural_name = "Vimeo Videos"; |
||
| 39 | |||
| 40 | private static $default_sort = "Title ASC"; |
||
| 41 | |||
| 42 | private static $vimeo_base_url = "http://vimeo.com/api/oembed.xml?url=http%3A//vimeo.com/";//The exact width of the video. Defaults to original size. |
||
| 43 | |||
| 44 | private static $width = null;//The exact width of the video. Defaults to original size. |
||
| 45 | |||
| 46 | private static $maxwidth = null;////Same as width, but video will not exceed original size. |
||
| 47 | |||
| 48 | private static $height = null;//The exact height of the video. Defaults to original size. |
||
| 49 | |||
| 50 | private static $maxheight = null;//Same as height, but video will not exceed original size. |
||
| 51 | |||
| 52 | private static $byline = null;//Show the byline on the video. Defaults to true. |
||
| 53 | |||
| 54 | private static $title = null;//Show the title on the video. Defaults to true. |
||
| 55 | |||
| 56 | private static $portrait = null;//// Show the user's portrait on the video. Defaults to true. |
||
| 57 | |||
| 58 | private static $color = null;// Specify the color of the video controls. |
||
| 59 | |||
| 60 | private static $callback = null;//When returning JSON, wrap in this function. |
||
| 61 | |||
| 62 | private static $autoplay = null;//Automatically start playback of the video. Defaults to false. |
||
| 63 | |||
| 64 | private static $xhtml = null;// Make the embed code XHTML compliant. Defaults to true. |
||
| 65 | |||
| 66 | private static $api = null;// Enable the Javascript API for Moogaloop. Defaults to false. |
||
| 67 | |||
| 68 | private static $wmode = null;//add the "wmode" parameter. Can be either transparent or opaque. |
||
| 69 | |||
| 70 | private static $iframe;// Use our new embed code. Defaults to true. NEW! |
||
| 71 | |||
| 72 | protected $dataAsArray = array(); |
||
| 73 | |||
| 74 | protected $variables = array( |
||
| 75 | "type", |
||
| 76 | "version", |
||
| 77 | "provider_name", |
||
| 78 | "provider_url", |
||
| 79 | "title", |
||
| 80 | "author_name", |
||
| 81 | "author_url", |
||
| 82 | "is_plus", |
||
| 83 | "html", |
||
| 84 | "width", |
||
| 85 | "height", |
||
| 86 | "duration", |
||
| 87 | "description", |
||
| 88 | "thumbnail_url", |
||
| 89 | "thumbnail_width", |
||
| 90 | "thumbnail_height", |
||
| 91 | "video_id" |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * do not retrieve data from vimeo server ... |
||
| 96 | * for internal use only |
||
| 97 | * @var Boolean |
||
| 98 | */ |
||
| 99 | private $doNotRetrieveData = false; |
||
| 100 | |||
| 101 | public function getCMSFields() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * casted variable |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getFullName() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * alias for getVariable |
||
| 127 | * @param String $name |
||
| 128 | * @return Varchar |
||
| 129 | */ |
||
| 130 | public function MetaDataVariable($name) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * |
||
| 137 | * @param String - name of variable |
||
| 138 | * |
||
| 139 | * @return Varchar Object |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function getMetaDataVariable($name) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * return icon as <img tag> |
||
| 152 | * @return String |
||
| 153 | */ |
||
| 154 | public function getIcon() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * returns icon as myimage.png |
||
| 171 | * @return String |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function getIconLink() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * return icon as <img tag> |
||
| 184 | * @return String |
||
| 185 | */ |
||
| 186 | public function getFullImage() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * returns icon as myimage.png |
||
| 204 | * @return String |
||
| 205 | */ |
||
| 206 | public function getFullImageLink() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * returns the HTML Embed code |
||
| 218 | * @return String |
||
| 219 | */ |
||
| 220 | public function HTML($noCaching = false) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * turns the saved serialized data into an array to return |
||
| 231 | * if there is no data then it will try to retrieve and save it |
||
| 232 | * then return it. |
||
| 233 | * @return Array |
||
| 234 | */ |
||
| 235 | protected function getDataAsArray() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * retrieves data from Vimeo Site |
||
| 249 | * |
||
| 250 | * @return Array |
||
| 251 | */ |
||
| 252 | protected function updateData($writeToDatabase = true) |
||
| 330 | |||
| 331 | |||
| 332 | public function onBeforeWrite() |
||
| 338 | |||
| 339 | |||
| 340 | //SOURCE: http://php.net/manual/en/function.xml-parse.php |
||
| 341 | private function my_xml2array($contents) |
||
| 394 | |||
| 395 | // |
||
| 396 | // use this to get node of tree by path with '/' terminator |
||
| 397 | // |
||
| 398 | //SOURCE: http://php.net/manual/en/function.xml-parse.php |
||
| 399 | private function get_value_by_path($__xml_tree, $__tag_path) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * |
||
| 421 | * @param String $serializedData |
||
| 422 | * |
||
| 423 | * @return Array |
||
| 424 | */ |
||
| 425 | public function safelyUnserialize($serializedData) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * |
||
| 450 | * @param Array $dataAsArray |
||
| 451 | * |
||
| 452 | * @return String |
||
| 453 | * |
||
| 454 | */ |
||
| 455 | public function safelySerialize($dataAsArray) |
||
| 459 | } |
||
| 460 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.