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 |
||
10 | class PublishItemWorkflowAction extends WorkflowAction { |
||
|
|||
11 | |||
12 | private static $db = array( |
||
13 | 'PublishDelay' => 'Int' |
||
14 | ); |
||
15 | |||
16 | public static $icon = 'advancedworkflow/images/publish.png'; |
||
17 | |||
18 | public function execute(WorkflowInstance $workflow) { |
||
19 | if (!$target = $workflow->getTarget()) { |
||
20 | return true; |
||
21 | } |
||
22 | |||
23 | if (class_exists('AbstractQueuedJob') && $this->PublishDelay) { |
||
24 | $job = new WorkflowPublishTargetJob($target); |
||
25 | $days = $this->PublishDelay; |
||
26 | $after = date('Y-m-d H:i:s', strtotime("+$days days")); |
||
27 | singleton('QueuedJobService')->queueJob($job, $after); |
||
28 | } else if ($target->hasExtension('WorkflowEmbargoExpiryExtension')) { |
||
29 | // setting future date stuff if needbe |
||
30 | |||
31 | // set this value regardless |
||
32 | $target->UnPublishOnDate = $target->DesiredUnPublishDate; |
||
33 | $target->DesiredUnPublishDate = ''; |
||
34 | if ($target->DesiredPublishDate) { |
||
35 | $target->PublishOnDate = $target->DesiredPublishDate; |
||
36 | $target->DesiredPublishDate = ''; |
||
37 | $target->write(); |
||
38 | } else { |
||
39 | if ($target->hasMethod('doPublish')) { |
||
40 | $target->doPublish(); |
||
41 | } |
||
42 | } |
||
43 | } else { |
||
44 | if ($target->hasMethod('doPublish')) { |
||
45 | $target->doPublish(); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return true; |
||
50 | } |
||
51 | |||
52 | View Code Duplication | public function getCMSFields() { |
|
53 | $fields = parent::getCMSFields(); |
||
54 | |||
55 | if (class_exists('AbstractQueuedJob')) { |
||
56 | $before = _t('PublishItemWorkflowAction.DELAYPUBDAYSBEFORE', 'Delay publication '); |
||
57 | $after = _t('PublishItemWorkflowAction.DELAYPUBDAYSAFTER', ' days'); |
||
58 | |||
59 | $fields->addFieldToTab('Root.Main', new FieldGroup( |
||
60 | _t('PublishItemWorkflowAction.PUBLICATIONDELAY', 'Publication Delay'), |
||
61 | new LabelField('PublishDelayBefore', $before), |
||
62 | new NumericField('PublishDelay', ''), |
||
63 | new LabelField('PublishDelayAfter', $after) |
||
64 | )); |
||
65 | } |
||
66 | |||
67 | return $fields; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Publish action allows a user who is currently assigned at this point of the workflow to |
||
72 | * |
||
73 | * @param DataObject $target |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function canPublishTarget(DataObject $target) { |
||
79 | |||
80 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.