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 |
||
| 28 | class ChangeSetItem extends DataObject implements Thumbnail { |
||
| 29 | |||
| 30 | const EXPLICITLY = 'explicitly'; |
||
| 31 | |||
| 32 | const IMPLICITLY = 'implicitly'; |
||
| 33 | |||
| 34 | /** Represents an object deleted */ |
||
| 35 | const CHANGE_DELETED = 'deleted'; |
||
| 36 | |||
| 37 | /** Represents an object which was modified */ |
||
| 38 | const CHANGE_MODIFIED = 'modified'; |
||
| 39 | |||
| 40 | /** Represents an object added */ |
||
| 41 | const CHANGE_CREATED = 'created'; |
||
| 42 | |||
| 43 | /** Represents an object which hasn't been changed directly, but owns a modified many_many relationship. */ |
||
| 44 | //const CHANGE_MANYMANY = 'manymany'; |
||
| 45 | |||
| 46 | private static $table_name = 'ChangeSetItem'; |
||
|
|
|||
| 47 | |||
| 48 | /** |
||
| 49 | * Represents that an object has not yet been changed, but |
||
| 50 | * should be included in this changeset as soon as any changes exist |
||
| 51 | */ |
||
| 52 | const CHANGE_NONE = 'none'; |
||
| 53 | |||
| 54 | private static $db = array( |
||
| 55 | 'VersionBefore' => 'Int', |
||
| 56 | 'VersionAfter' => 'Int', |
||
| 57 | 'Added' => "Enum('explicitly, implicitly', 'implicitly')" |
||
| 58 | ); |
||
| 59 | |||
| 60 | private static $has_one = array( |
||
| 61 | 'ChangeSet' => 'SilverStripe\ORM\Versioning\ChangeSet', |
||
| 62 | 'Object' => 'SilverStripe\ORM\DataObject', |
||
| 63 | ); |
||
| 64 | |||
| 65 | private static $many_many = array( |
||
| 66 | 'ReferencedBy' => 'SilverStripe\ORM\Versioning\ChangeSetItem' |
||
| 67 | ); |
||
| 68 | |||
| 69 | private static $belongs_many_many = array( |
||
| 70 | 'References' => 'ChangeSetItem.ReferencedBy' |
||
| 71 | ); |
||
| 72 | |||
| 73 | private static $indexes = array( |
||
| 74 | 'ObjectUniquePerChangeSet' => array( |
||
| 75 | 'type' => 'unique', |
||
| 76 | 'value' => '"ObjectID", "ObjectClass", "ChangeSetID"' |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | |||
| 80 | public function onBeforeWrite() { |
||
| 85 | |||
| 86 | public function getTitle() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a thumbnail for this object |
||
| 97 | * |
||
| 98 | * @param int $width Preferred width of the thumbnail |
||
| 99 | * @param int $height Preferred height of the thumbnail |
||
| 100 | * @return string URL to the thumbnail, if available |
||
| 101 | */ |
||
| 102 | public function ThumbnailURL($width, $height) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the type of change: none, created, deleted, modified, manymany |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getChangeType() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Find version of this object in the given stage |
||
| 147 | * |
||
| 148 | * @param string $stage |
||
| 149 | * @return Versioned|DataObject |
||
| 150 | */ |
||
| 151 | View Code Duplication | protected function getObjectInStage($stage) { |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Find latest version of this object |
||
| 161 | * |
||
| 162 | * @return Versioned|DataObject |
||
| 163 | */ |
||
| 164 | View Code Duplication | protected function getObjectLatestVersion() { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get all implicit objects for this change |
||
| 174 | * |
||
| 175 | * @return SS_List |
||
| 176 | */ |
||
| 177 | public function findReferenced() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Publish this item, then close it. |
||
| 192 | * |
||
| 193 | * Note: Unlike Versioned::doPublish() and Versioned::doUnpublish, this action is not recursive. |
||
| 194 | */ |
||
| 195 | public function publish() { |
||
| 237 | |||
| 238 | /** Reverts this item, then close it. **/ |
||
| 239 | public function revert() { |
||
| 242 | |||
| 243 | public function canView($member = null) { |
||
| 246 | |||
| 247 | public function canEdit($member = null) { |
||
| 250 | |||
| 251 | public function canCreate($member = null, $context = array()) { |
||
| 254 | |||
| 255 | public function canDelete($member = null) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Check if the BeforeVersion of this changeset can be restored to draft |
||
| 261 | * |
||
| 262 | * @param Member $member |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function canRevert($member) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Check if this ChangeSetItem can be published |
||
| 297 | * |
||
| 298 | * @param Member $member |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function canPublish($member = null) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Default permissions for this ChangeSetItem |
||
| 328 | * |
||
| 329 | * @param string $perm |
||
| 330 | * @param Member $member |
||
| 331 | * @param array $context |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function can($perm, $member = null, $context = array()) { |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get the ChangeSetItems that reference a passed DataObject |
||
| 352 | * |
||
| 353 | * @param DataObject $object |
||
| 354 | * @return DataList |
||
| 355 | */ |
||
| 356 | public static function get_for_object($object) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the ChangeSetItems that reference a passed DataObject |
||
| 365 | * |
||
| 366 | * @param int $objectID The ID of the object |
||
| 367 | * @param string $objectClass The class of the object (or any parent class) |
||
| 368 | * @return DataList |
||
| 369 | */ |
||
| 370 | public static function get_for_object_by_id($objectID, $objectClass) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Gets the list of modes this record can be previewed in. |
||
| 379 | * |
||
| 380 | * {@link https://tools.ietf.org/html/draft-kelly-json-hal-07#section-5} |
||
| 381 | * |
||
| 382 | * @return array Map of links in acceptable HAL format |
||
| 383 | */ |
||
| 384 | public function getPreviewLinks() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get edit link for this item |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function CMSEditLink() |
||
| 421 | } |
||
| 422 |