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 ChangeSet 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 ChangeSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ChangeSet extends DataObject { |
||
30 | |||
31 | private static $singular_name = 'Campaign'; |
||
|
|||
32 | |||
33 | private static $plural_name = 'Campaigns'; |
||
34 | |||
35 | /** An active changeset */ |
||
36 | const STATE_OPEN = 'open'; |
||
37 | |||
38 | /** A changeset which is reverted and closed */ |
||
39 | const STATE_REVERTED = 'reverted'; |
||
40 | |||
41 | /** A changeset which is published and closed */ |
||
42 | const STATE_PUBLISHED = 'published'; |
||
43 | |||
44 | private static $table_name = 'ChangeSet'; |
||
45 | |||
46 | private static $db = array( |
||
47 | 'Name' => 'Varchar', |
||
48 | 'State' => "Enum('open,published,reverted','open')", |
||
49 | ); |
||
50 | |||
51 | private static $has_many = array( |
||
52 | 'Changes' => 'SilverStripe\ORM\Versioning\ChangeSetItem', |
||
53 | ); |
||
54 | |||
55 | private static $defaults = array( |
||
56 | 'State' => 'open' |
||
57 | ); |
||
58 | |||
59 | private static $has_one = array( |
||
60 | 'Owner' => 'SilverStripe\\Security\\Member', |
||
61 | ); |
||
62 | |||
63 | private static $casting = array( |
||
64 | 'Description' => 'Text', |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * List of classes to set apart in description |
||
69 | * |
||
70 | * @config |
||
71 | * @var array |
||
72 | */ |
||
73 | private static $important_classes = array( |
||
74 | 'SilverStripe\\CMS\\Model\\SiteTree', |
||
75 | 'SilverStripe\\Assets\\File', |
||
76 | ); |
||
77 | |||
78 | /** |
||
79 | * Default permission to require for publishers. |
||
80 | * Publishers must either be able to use the campaign admin, or have all admin access. |
||
81 | * |
||
82 | * Also used as default permission for ChangeSetItem default permission. |
||
83 | * |
||
84 | * @config |
||
85 | * @var array |
||
86 | */ |
||
87 | private static $required_permission = array('CMS_ACCESS_CampaignAdmin', 'CMS_ACCESS_LeftAndMain'); |
||
88 | |||
89 | /** |
||
90 | * Publish this changeset, then closes it. |
||
91 | * |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | public function publish() { |
||
120 | |||
121 | /** |
||
122 | * Add a new change to this changeset. Will automatically include all owned |
||
123 | * changes as those are dependencies of this item. |
||
124 | * |
||
125 | * @param DataObject $object |
||
126 | */ |
||
127 | public function addObject(DataObject $object) { |
||
153 | |||
154 | /** |
||
155 | * Remove an item from this changeset. Will automatically remove all changes |
||
156 | * which own (and thus depend on) the removed item. |
||
157 | * |
||
158 | * @param DataObject $object |
||
159 | */ |
||
160 | public function removeObject(DataObject $object) { |
||
175 | |||
176 | /** |
||
177 | * Build identifying string key for this object |
||
178 | * |
||
179 | * @param DataObject $item |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function implicitKey(DataObject $item) { |
||
188 | |||
189 | protected function calculateImplicit() { |
||
229 | |||
230 | /** |
||
231 | * Add implicit changes that should be included in this changeset |
||
232 | * |
||
233 | * When an item is created or changed, all it's owned items which have |
||
234 | * changes are implicitly added |
||
235 | * |
||
236 | * When an item is deleted, it's owner (even if that owner does not have changes) |
||
237 | * is implicitly added |
||
238 | */ |
||
239 | public function sync() { |
||
274 | |||
275 | /** Verify that any objects in this changeset include all owned changes */ |
||
276 | public function isSynced() { |
||
293 | |||
294 | public function canView($member = null) { |
||
297 | |||
298 | public function canEdit($member = null) { |
||
301 | |||
302 | public function canCreate($member = null, $context = array()) { |
||
305 | |||
306 | public function canDelete($member = null) { |
||
309 | |||
310 | /** |
||
311 | * Check if this item is allowed to be published |
||
312 | * |
||
313 | * @param Member $member |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function canPublish($member = null) { |
||
328 | |||
329 | /** |
||
330 | * Check if this changeset (if published) can be reverted |
||
331 | * |
||
332 | * @param Member $member |
||
333 | * @return bool |
||
334 | */ |
||
335 | public function canRevert($member = null) { |
||
347 | |||
348 | /** |
||
349 | * Default permissions for this changeset |
||
350 | * |
||
351 | * @param string $perm |
||
352 | * @param Member $member |
||
353 | * @param array $context |
||
354 | * @return bool |
||
355 | */ |
||
356 | View Code Duplication | public function can($perm, $member = null, $context = array()) { |
|
371 | |||
372 | public function getCMSFields() { |
||
381 | |||
382 | /** |
||
383 | * Gets summary of items in changeset |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | public function getDescription() { |
||
494 | |||
495 | public function fieldLabels($includerelations = true) { |
||
502 | } |
||
503 |