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:
| 1 | <?php |
||
| 15 | class ChangeSetItem extends DataObject { |
||
| 16 | |||
| 17 | const EXPLICITLY = 'explicitly'; |
||
| 18 | |||
| 19 | const IMPLICITLY = 'implicitly'; |
||
| 20 | |||
| 21 | /** Represents an object deleted */ |
||
| 22 | const CHANGE_DELETED = 'deleted'; |
||
| 23 | |||
| 24 | /** Represents an object which was modified */ |
||
| 25 | const CHANGE_MODIFIED = 'modified'; |
||
| 26 | |||
| 27 | /** Represents an object added */ |
||
| 28 | const CHANGE_CREATED = 'created'; |
||
| 29 | |||
| 30 | /** Represents an object which hasn't been changed directly, but owns a modified many_many relationship. */ |
||
| 31 | //const CHANGE_MANYMANY = 'manymany'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Represents that an object has not yet been changed, but |
||
| 35 | * should be included in this changeset as soon as any changes exist |
||
| 36 | */ |
||
| 37 | const CHANGE_NONE = 'none'; |
||
| 38 | |||
| 39 | private static $db = array( |
||
|
|
|||
| 40 | 'VersionBefore' => 'Int', |
||
| 41 | 'VersionAfter' => 'Int', |
||
| 42 | 'Added' => "Enum('explicitly, implicitly', 'implicitly')" |
||
| 43 | ); |
||
| 44 | |||
| 45 | private static $has_one = array( |
||
| 46 | 'ChangeSet' => 'ChangeSet', |
||
| 47 | 'Object' => 'DataObject', |
||
| 48 | ); |
||
| 49 | |||
| 50 | private static $many_many = array( |
||
| 51 | 'ReferencedBy' => 'ChangeSetItem' |
||
| 52 | ); |
||
| 53 | |||
| 54 | private static $belongs_many_many = array( |
||
| 55 | 'References' => 'ChangeSetItem.ReferencedBy' |
||
| 56 | ); |
||
| 57 | |||
| 58 | private static $indexes = array( |
||
| 59 | 'ObjectUniquePerChangeSet' => array( |
||
| 60 | 'type' => 'unique', |
||
| 61 | 'value' => '"ObjectID", "ObjectClass", "ChangeSetID"' |
||
| 62 | ) |
||
| 63 | ); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the type of change: none, created, deleted, modified, manymany |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getChangeType() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Find version of this object in the given stage |
||
| 98 | * |
||
| 99 | * @param string $stage |
||
| 100 | * @return Versioned|DataObject |
||
| 101 | */ |
||
| 102 | private function getObjectInStage($stage) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get all implicit objects for this change |
||
| 108 | * |
||
| 109 | * @return SS_List |
||
| 110 | */ |
||
| 111 | public function findReferenced() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Publish this item, then close it. |
||
| 126 | * |
||
| 127 | * Note: Unlike Versioned::doPublish() and Versioned::doUnpublish, this action is not recursive. |
||
| 128 | */ |
||
| 129 | public function publish() { |
||
| 167 | |||
| 168 | /** Reverts this item, then close it. **/ |
||
| 169 | public function revert() { |
||
| 172 | |||
| 173 | public function canView($member = null) { |
||
| 176 | |||
| 177 | public function canEdit($member = null) { |
||
| 180 | |||
| 181 | public function canCreate($member = null, $context = array()) { |
||
| 184 | |||
| 185 | public function canDelete($member = null) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Check if the BeforeVersion of this changeset can be restored to draft |
||
| 191 | * |
||
| 192 | * @param Member $member |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function canRevert($member) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Check if this ChangeSetItem can be published |
||
| 227 | * |
||
| 228 | * @param Member $member |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function canPublish($member = null) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Default permissions for this ChangeSetItem |
||
| 258 | * |
||
| 259 | * @param string $perm |
||
| 260 | * @param Member $member |
||
| 261 | * @param array $context |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function can($perm, $member = null, $context = array()) { |
|
| 279 | |||
| 280 | } |
||
| 281 |