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) |
||
52 | |||
53 | public function setBaseClass($myBaseClass) |
||
57 | |||
58 | |||
59 | protected function retrieveDBFields($name) |
||
73 | |||
74 | protected $_dbfieldCache = []; |
||
75 | |||
76 | public function DbFields() |
||
116 | |||
117 | public function MyDbFields() |
||
121 | |||
122 | public function MyDbFieldsWithDefaults() |
||
130 | |||
131 | public function mydbfieldsfancywithbelongswithbasicfields() |
||
135 | |||
136 | public function MyDbFieldsFancyWithBelongs() |
||
140 | |||
141 | public function MyDbFieldsFancyWithoutBelongs($includeBelongs = false) |
||
184 | |||
185 | public function MyDbFieldsAndHasOnes() |
||
191 | |||
192 | public function MyDbFieldsAndHasOnesWithIDs() |
||
203 | |||
204 | public function MyDbFieldsAndIndexes() |
||
214 | |||
215 | public function MyAllFieldsWithBelongs() |
||
219 | |||
220 | public function MyAllFieldsWithoutBelongs($includeBelongs = false) |
||
236 | |||
237 | |||
238 | public function IndexOptions() |
||
246 | |||
247 | public function RequiredOptions() |
||
254 | |||
255 | |||
256 | public function PossibleRelationsWithBaseClass($rootClass = '') |
||
267 | |||
268 | protected $_classesCache = []; |
||
269 | |||
270 | /** |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function PossibleRelations($rootClass = '') |
||
304 | |||
305 | protected $_filtersCache = []; |
||
306 | |||
307 | public function PossibleSearchFilters() |
||
322 | |||
323 | protected $_modelAdmins = []; |
||
324 | |||
325 | public function ModelAdminOptions() |
||
346 | |||
347 | |||
348 | public function SortOptions() |
||
355 | |||
356 | protected $_canOptions = null; |
||
357 | |||
358 | public function CanOptions() |
||
374 | |||
375 | |||
376 | |||
377 | /** |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | public function SiteTreeList($rootClass = 'SiteTree') |
||
402 | |||
403 | |||
404 | /** |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | public function AllowedChildrenOptions($rootClass = 'SiteTree') |
||
412 | |||
413 | /** |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | public function TrueOrFalseListWithIgnore() |
||
424 | |||
425 | /** |
||
426 | * |
||
427 | * @return array |
||
428 | */ |
||
429 | public function TrueOrFalseList() |
||
436 | |||
437 | } |
||
438 |
This check marks private properties in classes that are never used. Those properties can be removed.