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 API 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 API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class API extends \Object |
||
| 6 | { |
||
| 7 | private static $excluded_data_objects = [ |
||
|
|
|||
| 8 | 'Image_Cached', |
||
| 9 | 'PermissionRoleCode', |
||
| 10 | 'LoginAttempt', |
||
| 11 | 'MemberPassword', |
||
| 12 | 'MemberPassword', |
||
| 13 | 'SiteConfig' |
||
| 14 | ]; |
||
| 15 | |||
| 16 | private static $excluded_db_fields_types = [ |
||
| 17 | 'DBField', |
||
| 18 | 'Field', |
||
| 19 | 'DBLocale', |
||
| 20 | 'Locale', |
||
| 21 | 'StringField', |
||
| 22 | 'CompositeField', |
||
| 23 | 'PrimaryKey', |
||
| 24 | 'ForeignKey' |
||
| 25 | ]; |
||
| 26 | |||
| 27 | private static $additional_db_fields = [ |
||
| 28 | 'ID', |
||
| 29 | 'Created', |
||
| 30 | 'LastEdited', |
||
| 31 | 'ClassName' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | protected $rootBaseClass = 'DataObject'; |
||
| 35 | |||
| 36 | protected $myBaseClass = ''; |
||
| 37 | |||
| 38 | protected $_data = null; |
||
| 39 | |||
| 40 | private static $_my_singleton = []; |
||
| 41 | |||
| 42 | public static function inst($myBaseClass = 'DataObject', $data) |
||
| 43 | { |
||
| 44 | if (! isset(self::$_my_singleton[$myBaseClass])) { |
||
| 45 | self::$_my_singleton[$myBaseClass] = \Injector::inst()->get('SunnySideUp\BuildDataObject\API'); |
||
| 46 | } |
||
| 47 | self::$_my_singleton[$myBaseClass]->_data = $data; |
||
| 48 | self::$_my_singleton[$myBaseClass]->setBaseClass($myBaseClass); |
||
| 49 | |||
| 50 | return self::$_my_singleton[$myBaseClass]; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function setBaseClass($myBaseClass) |
||
| 54 | { |
||
| 55 | $this->myBaseClass = $myBaseClass; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | protected function retrieveDBFields($name) |
||
| 60 | { |
||
| 61 | $data = $this->_data; |
||
| 62 | $ar = []; |
||
| 63 | if (isset($data->$name)) { |
||
| 64 | foreach ($data->$name as $data) { |
||
| 65 | if ($data->Key && $data->Value) { |
||
| 66 | $ar[$data->Key] = $data->Key; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return $ar; |
||
| 72 | } |
||
| 73 | |||
| 74 | protected $_dbfieldCache = []; |
||
| 75 | |||
| 76 | public function DbFields() |
||
| 77 | { |
||
| 78 | if (count($this->_dbfieldCache) === 0) { |
||
| 79 | $list = \ClassInfo::subclassesFor('DbField'); |
||
| 80 | $newList = []; |
||
| 81 | foreach ($list as $class) { |
||
| 82 | if (substr($class, 0, 2) == 'DB') { |
||
| 83 | $class = substr($class, 2, strlen($class)); |
||
| 84 | } elseif (substr($class, 0, 3) == 'SS_') { |
||
| 85 | $class = substr($class, 3, strlen($class)); |
||
| 86 | } elseif ('Varchar' === $class) { |
||
| 87 | $class = 'Varchar'; |
||
| 88 | } elseif ('HTMLVarchar' === $class) { |
||
| 89 | $class = 'HTMLVarchar(255)'; |
||
| 90 | } elseif ('Enum' === $class) { |
||
| 91 | $class = 'Enum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
||
| 92 | } elseif ('MultiEnum' === $class) { |
||
| 93 | $class = 'MultiEnum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
||
| 94 | } |
||
| 95 | if ( |
||
| 96 | $class == 'DbField' || |
||
| 97 | is_subclass_of($class, 'TestOnly') || |
||
| 98 | in_array($class, $this->Config()->get('excluded_db_fields_types')) |
||
| 99 | ) { |
||
| 100 | //do nothing |
||
| 101 | } else { |
||
| 102 | $newList[$class] = $class; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | $this->_dbfieldCache = $newList; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->_dbfieldCache; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function MyDbFields() |
||
| 115 | |||
| 116 | public function MyDbFieldsWithDefaults() |
||
| 117 | { |
||
| 118 | $list = $this->retrieveDBFields('db'); |
||
| 119 | $toAdd = $this->Config()->get('additional_db_fields'); |
||
| 120 | $toAdd = array_combine($toAdd, $toAdd); |
||
| 121 | |||
| 122 | return $toAdd + $list; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function mydbfieldsfancywithbelongswithbasicfields() |
||
| 126 | { |
||
| 127 | return $this->MyDbFieldsFancyWithBelongs(); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function MyDbFieldsFancyWithBelongs() |
||
| 134 | |||
| 135 | public function MyDbFieldsFancyWithoutBelongs($includeBelongs = false) |
||
| 178 | |||
| 179 | public function MyDbFieldsAndHasOnes() |
||
| 185 | |||
| 186 | public function MyDbFieldsAndHasOnesWithIDs() |
||
| 197 | |||
| 198 | public function MyDbFieldsAndIndexes() |
||
| 208 | |||
| 209 | public function MyAllFieldsWithBelongs() |
||
| 213 | |||
| 214 | public function MyAllFieldsWithoutBelongs($includeBelongs = false) |
||
| 230 | |||
| 231 | |||
| 232 | public function IndexOptions() |
||
| 240 | |||
| 241 | public function RequiredOptions() |
||
| 248 | |||
| 249 | |||
| 250 | public function PossibleRelationsWithBaseClass($rootClass = '') |
||
| 251 | { |
||
| 264 | |||
| 265 | protected $_classesCache = []; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function PossibleRelations($rootClass = '') |
||
| 301 | |||
| 302 | protected $_filtersCache = []; |
||
| 303 | |||
| 304 | public function PossibleSearchFilters() |
||
| 319 | |||
| 320 | protected $_modelAdmins = []; |
||
| 321 | |||
| 322 | public function ModelAdminOptions() |
||
| 343 | |||
| 344 | |||
| 345 | public function SortOptions() |
||
| 352 | |||
| 353 | protected $_canOptions = null; |
||
| 354 | |||
| 355 | public function CanOptions() |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function SiteTreeList($rootClass = 'SiteTree') |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function AllowedChildrenOptions($rootClass = 'SiteTree') |
||
| 410 | |||
| 411 | /** |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | public function TrueOrFalseListWithIgnore() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function TrueOrFalseList() |
||
| 434 | } |
||
| 435 |
This check marks private properties in classes that are never used. Those properties can be removed.