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 PageRating 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 PageRating, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PageRating extends DataObject |
||
|
|
|||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var bool |
||
| 13 | */ |
||
| 14 | private static $admin_can_create = true; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | private static $admin_can_edit = true; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private static $admin_can_delete = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var boolean |
||
| 28 | */ |
||
| 29 | private static $round_rating = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private static $stars = array( |
||
| 35 | '1' => array("Code" => 'OneStar', "Title" => "One Star"), |
||
| 36 | '2' => array("Code" => 'TwoStar', "Title" => "Two Stars"), |
||
| 37 | '3' => array("Code" => 'ThreeStar', "Title" => "Three Stars"), |
||
| 38 | '4' => array("Code" => 'FourStar', "Title" => "Four Stars"), |
||
| 39 | '5' => array("Code" => 'FiveStar', "Title" => "Five Stars") |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | protected static function get_stars() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * returns something like OneStar |
||
| 53 | * @param int $value |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | View Code Duplication | public static function get_star_entry_code($value) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * returns something like One Star |
||
| 68 | * @param int $value |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | View Code Duplication | public static function get_star_entry_name($value) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * returns something like |
||
| 83 | * |
||
| 84 | * 1 => One Star, |
||
| 85 | * 2 => Two Star |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public static function get_star_dropdowndown() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * return int |
||
| 103 | */ |
||
| 104 | public static function get_number_of_stars() |
||
| 108 | |||
| 109 | private static $_star_details_as_array_data = array(); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | * |
||
| 114 | * @return ArrayData |
||
| 115 | */ |
||
| 116 | public static function get_star_details_as_array_data($score, $parentID, $method = "unkown") |
||
| 148 | |||
| 149 | private static $db = array( |
||
| 150 | "Rating" => "Int", |
||
| 151 | "Name" => "Varchar(100)", |
||
| 152 | "Title" => "Varchar(100)", |
||
| 153 | "Comment" => "Text", |
||
| 154 | "IsApproved" => "Boolean", |
||
| 155 | "IsDefault" => "Boolean" |
||
| 156 | ); |
||
| 157 | |||
| 158 | private static $has_one = array( |
||
| 159 | "Parent" => "Page" |
||
| 160 | ); |
||
| 161 | |||
| 162 | private static $summary_fields = array( |
||
| 163 | "Created" => "When", |
||
| 164 | "Rating" => "Score", |
||
| 165 | "Parent.Title" => "Page", |
||
| 166 | "Comment" => "Comment", |
||
| 167 | "IsApproved.Nice" => "Approved" |
||
| 168 | ); |
||
| 169 | |||
| 170 | private static $field_labels = array( |
||
| 171 | "Rating" => "Score", |
||
| 172 | "Parent.Title" => "Page", |
||
| 173 | "IsDefault" => "Default Entry Only", |
||
| 174 | "Parent" => "Rating for", |
||
| 175 | "IsApproved" => "Approved" |
||
| 176 | ); |
||
| 177 | |||
| 178 | private static $casting = array( |
||
| 179 | 'Method' => "Varchar", |
||
| 180 | 'Stars' => "Float", |
||
| 181 | 'Percentage' => "Float", |
||
| 182 | 'RoundedPercentage' => "Int", |
||
| 183 | 'ReversePercentage' => "Float", |
||
| 184 | 'ReverseRoundedPercentage' => "Int", |
||
| 185 | 'StarClass' => "Varchar" |
||
| 186 | ); |
||
| 187 | |||
| 188 | private static $default_sort = "Created DESC"; |
||
| 189 | |||
| 190 | private static $singular_name = 'Page Rating'; |
||
| 191 | |||
| 192 | private static $plural_name = 'Page Ratings'; |
||
| 193 | |||
| 194 | |||
| 195 | public function getCMSFields() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * updates Page Rating for a Page and saves it with the Page. |
||
| 267 | * Updates SiteTree.PageRating field ... |
||
| 268 | * |
||
| 269 | * if $siteTreeID is 0 then ALL pages are updated ... |
||
| 270 | * |
||
| 271 | * @param integer $siteTreeID |
||
| 272 | */ |
||
| 273 | public static function update_ratings($siteTreeID = 0) |
||
| 301 | |||
| 302 | public function onBeforeWrite() |
||
| 309 | |||
| 310 | public function requireDefaultRecords() |
||
| 319 | |||
| 320 | public function canCreate($member = null) |
||
| 327 | |||
| 328 | public function canDelete($member = null) |
||
| 335 | |||
| 336 | public function canEdit($member = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @alias |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function Method() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * casted variable |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getMethod() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @alias |
||
| 367 | * |
||
| 368 | * @return int | float |
||
| 369 | */ |
||
| 370 | public function Stars() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * casted variable |
||
| 377 | * |
||
| 378 | * @return int | float |
||
| 379 | */ |
||
| 380 | public function getStars() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @alias |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function Percentage() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * casted variable |
||
| 398 | * |
||
| 399 | * @return float |
||
| 400 | */ |
||
| 401 | public function getPercentage() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @alias |
||
| 409 | * |
||
| 410 | * @return int |
||
| 411 | */ |
||
| 412 | public function RoundedPercentage() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * casted variable |
||
| 419 | * |
||
| 420 | * @return int |
||
| 421 | */ |
||
| 422 | public function getRoundedPercentage() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * casted alias |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function ReversePercentage() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * casted variable |
||
| 440 | * |
||
| 441 | * @return float |
||
| 442 | */ |
||
| 443 | public function getReversePercentage() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @alias |
||
| 451 | * |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function ReverseRoundedPercentage() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * casted variable |
||
| 461 | * |
||
| 462 | * @return int |
||
| 463 | */ |
||
| 464 | public function getReverseRoundedPercentage() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @alias |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function StarClass() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * casted variable |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | public function getStarClass() |
||
| 490 | } |
||
| 491 |
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.