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 ChangeSetItem 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 ChangeSetItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ChangeSetItem extends DataObject implements Thumbnail { |
||
30 | |||
31 | const EXPLICITLY = 'explicitly'; |
||
32 | |||
33 | const IMPLICITLY = 'implicitly'; |
||
34 | |||
35 | /** Represents an object deleted */ |
||
36 | const CHANGE_DELETED = 'deleted'; |
||
37 | |||
38 | /** Represents an object which was modified */ |
||
39 | const CHANGE_MODIFIED = 'modified'; |
||
40 | |||
41 | /** Represents an object added */ |
||
42 | const CHANGE_CREATED = 'created'; |
||
43 | |||
44 | /** Represents an object which hasn't been changed directly, but owns a modified many_many relationship. */ |
||
45 | //const CHANGE_MANYMANY = 'manymany'; |
||
46 | |||
47 | private static $table_name = 'ChangeSetItem'; |
||
|
|||
48 | |||
49 | /** |
||
50 | * Represents that an object has not yet been changed, but |
||
51 | * should be included in this changeset as soon as any changes exist |
||
52 | */ |
||
53 | const CHANGE_NONE = 'none'; |
||
54 | |||
55 | private static $db = array( |
||
56 | 'VersionBefore' => 'Int', |
||
57 | 'VersionAfter' => 'Int', |
||
58 | 'Added' => "Enum('explicitly, implicitly', 'implicitly')" |
||
59 | ); |
||
60 | |||
61 | private static $has_one = array( |
||
62 | 'ChangeSet' => 'SilverStripe\ORM\Versioning\ChangeSet', |
||
63 | 'Object' => 'SilverStripe\ORM\DataObject', |
||
64 | ); |
||
65 | |||
66 | private static $many_many = array( |
||
67 | 'ReferencedBy' => 'SilverStripe\ORM\Versioning\ChangeSetItem' |
||
68 | ); |
||
69 | |||
70 | private static $belongs_many_many = array( |
||
71 | 'References' => 'ChangeSetItem.ReferencedBy' |
||
72 | ); |
||
73 | |||
74 | private static $indexes = array( |
||
75 | 'ObjectUniquePerChangeSet' => array( |
||
76 | 'type' => 'unique', |
||
77 | 'value' => '"ObjectID", "ObjectClass", "ChangeSetID"' |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | public function onBeforeWrite() { |
||
86 | |||
87 | public function getTitle() { |
||
95 | |||
96 | /** |
||
97 | * Get a thumbnail for this object |
||
98 | * |
||
99 | * @param int $width Preferred width of the thumbnail |
||
100 | * @param int $height Preferred height of the thumbnail |
||
101 | * @return string URL to the thumbnail, if available |
||
102 | */ |
||
103 | public function ThumbnailURL($width, $height) { |
||
110 | |||
111 | /** |
||
112 | * Get the type of change: none, created, deleted, modified, manymany |
||
113 | * @return string |
||
114 | * @throws UnexpectedDataException |
||
115 | */ |
||
116 | public function getChangeType() { |
||
145 | |||
146 | /** |
||
147 | * Find version of this object in the given stage |
||
148 | * |
||
149 | * @param string $stage |
||
150 | * @return DataObject|Versioned |
||
151 | * @throws UnexpectedDataException |
||
152 | */ |
||
153 | View Code Duplication | protected function getObjectInStage($stage) { |
|
160 | |||
161 | /** |
||
162 | * Find latest version of this object |
||
163 | * @return DataObject|Versioned |
||
164 | * @throws UnexpectedDataException |
||
165 | */ |
||
166 | View Code Duplication | protected function getObjectLatestVersion() { |
|
173 | |||
174 | /** |
||
175 | * Get all implicit objects for this change |
||
176 | * |
||
177 | * @return SS_List |
||
178 | */ |
||
179 | public function findReferenced() { |
||
199 | |||
200 | /** |
||
201 | * Publish this item, then close it. |
||
202 | * |
||
203 | * Note: Unlike Versioned::doPublish() and Versioned::doUnpublish, this action is not recursive. |
||
204 | */ |
||
205 | public function publish() { |
||
247 | |||
248 | /** |
||
249 | * Once this item (and all owned objects) are published, unlink |
||
250 | * all disowned objects |
||
251 | */ |
||
252 | public function unlinkDisownedObjects() { |
||
258 | |||
259 | /** Reverts this item, then close it. **/ |
||
260 | public function revert() { |
||
263 | |||
264 | public function canView($member = null) { |
||
267 | |||
268 | public function canEdit($member = null) { |
||
271 | |||
272 | public function canCreate($member = null, $context = array()) { |
||
275 | |||
276 | public function canDelete($member = null) { |
||
279 | |||
280 | /** |
||
281 | * Check if the BeforeVersion of this changeset can be restored to draft |
||
282 | * |
||
283 | * @param Member $member |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function canRevert($member) { |
||
306 | |||
307 | /** |
||
308 | * Check if this ChangeSetItem can be published |
||
309 | * |
||
310 | * @param Member $member |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function canPublish($member = null) { |
||
336 | |||
337 | /** |
||
338 | * Default permissions for this ChangeSetItem |
||
339 | * |
||
340 | * @param string $perm |
||
341 | * @param Member $member |
||
342 | * @param array $context |
||
343 | * @return bool |
||
344 | */ |
||
345 | View Code Duplication | public function can($perm, $member = null, $context = array()) { |
|
360 | |||
361 | /** |
||
362 | * Get the ChangeSetItems that reference a passed DataObject |
||
363 | * |
||
364 | * @param DataObject $object |
||
365 | * @return DataList |
||
366 | */ |
||
367 | public static function get_for_object($object) { |
||
373 | |||
374 | /** |
||
375 | * Get the ChangeSetItems that reference a passed DataObject |
||
376 | * |
||
377 | * @param int $objectID The ID of the object |
||
378 | * @param string $objectClass The class of the object (or any parent class) |
||
379 | * @return DataList |
||
380 | */ |
||
381 | public static function get_for_object_by_id($objectID, $objectClass) { |
||
387 | |||
388 | /** |
||
389 | * Gets the list of modes this record can be previewed in. |
||
390 | * |
||
391 | * {@link https://tools.ietf.org/html/draft-kelly-json-hal-07#section-5} |
||
392 | * |
||
393 | * @return array Map of links in acceptable HAL format |
||
394 | */ |
||
395 | public function getPreviewLinks() { |
||
418 | |||
419 | /** |
||
420 | * Get edit link for this item |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function CMSEditLink() |
||
432 | } |
||
433 |