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 UserDefinedForm_EmailRecipient 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 UserDefinedForm_EmailRecipient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class UserDefinedForm_EmailRecipient extends DataObject |
||
|
|||
11 | { |
||
12 | |||
13 | private static $db = array( |
||
14 | 'EmailAddress' => 'Varchar(200)', |
||
15 | 'EmailSubject' => 'Varchar(200)', |
||
16 | 'EmailFrom' => 'Varchar(200)', |
||
17 | 'EmailReplyTo' => 'Varchar(200)', |
||
18 | 'EmailBody' => 'Text', |
||
19 | 'EmailBodyHtml' => 'HTMLText', |
||
20 | 'EmailTemplate' => 'Varchar', |
||
21 | 'SendPlain' => 'Boolean', |
||
22 | 'HideFormData' => 'Boolean', |
||
23 | 'CustomRulesCondition' => 'Enum("And,Or")' |
||
24 | ); |
||
25 | |||
26 | private static $has_one = array( |
||
27 | 'Form' => 'UserDefinedForm', |
||
28 | 'SendEmailFromField' => 'EditableFormField', |
||
29 | 'SendEmailToField' => 'EditableFormField', |
||
30 | 'SendEmailSubjectField' => 'EditableFormField' |
||
31 | ); |
||
32 | |||
33 | private static $has_many = array( |
||
34 | 'CustomRules' => 'UserDefinedForm_EmailRecipientCondition' |
||
35 | ); |
||
36 | |||
37 | private static $summary_fields = array( |
||
38 | 'EmailAddress', |
||
39 | 'EmailSubject', |
||
40 | 'EmailFrom' |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Setting this to true will allow you to select "risky" fields as |
||
45 | * email recipient, such as free-text entry fields. |
||
46 | * |
||
47 | * It's advisable to leave this off. |
||
48 | * |
||
49 | * @config |
||
50 | * @var bool |
||
51 | */ |
||
52 | private static $allow_unbound_recipient_fields = false; |
||
53 | |||
54 | 1 | public function summaryFields() |
|
55 | 1 | { |
|
56 | $fields = parent::summaryFields(); |
||
57 | 1 | if (isset($fields['EmailAddress'])) { |
|
58 | $fields['EmailAddress'] = _t('UserDefinedForm.EMAILADDRESS', 'Email'); |
||
59 | 1 | } |
|
60 | if (isset($fields['EmailSubject'])) { |
||
61 | $fields['EmailSubject'] = _t('UserDefinedForm.EMAILSUBJECT', 'Subject'); |
||
62 | } |
||
63 | if (isset($fields['EmailFrom'])) { |
||
64 | $fields['EmailFrom'] = _t('UserDefinedForm.EMAILFROM', 'From'); |
||
65 | } |
||
66 | return $fields; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get instance of UserDefinedForm when editing in getCMSFields |
||
71 | * |
||
72 | * @return UserDefinedFrom |
||
73 | */ |
||
74 | 1 | protected function getFormParent() |
|
81 | |||
82 | 1 | public function getTitle() |
|
83 | { |
||
84 | 1 | if ($this->EmailAddress) { |
|
85 | return $this->EmailAddress; |
||
86 | } |
||
87 | if ($this->EmailSubject) { |
||
88 | return $this->EmailSubject; |
||
89 | } |
||
90 | return parent::getTitle(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Generate a gridfield config for editing filter rules |
||
95 | * |
||
96 | * @return GridFieldConfig |
||
97 | */ |
||
98 | 1 | protected function getRulesConfig() |
|
126 | |||
127 | /** |
||
128 | * @return FieldList |
||
129 | */ |
||
130 | 1 | public function getCMSFields() |
|
300 | |||
301 | /** |
||
302 | * Return whether a user can create an object of this type |
||
303 | * |
||
304 | * @param Member $member |
||
305 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
306 | * @return bool |
||
307 | */ |
||
308 | View Code Duplication | public function canCreate($member = null) |
|
319 | |||
320 | /** |
||
321 | * Helper method to check the parent for this object |
||
322 | * |
||
323 | * @param array $args List of arguments passed to canCreate |
||
324 | * @return SiteTree Parent page instance |
||
325 | */ |
||
326 | View Code Duplication | protected function getCanCreateContext($args) |
|
340 | |||
341 | /** |
||
342 | * @param Member |
||
343 | * |
||
344 | * @return boolean |
||
345 | */ |
||
346 | public function canView($member = null) |
||
350 | |||
351 | /** |
||
352 | * @param Member |
||
353 | 1 | * |
|
354 | * @return boolean |
||
355 | 1 | */ |
|
356 | public function canEdit($member = null) |
||
360 | |||
361 | /** |
||
362 | * @param Member |
||
363 | 1 | * |
|
364 | * @return boolean |
||
365 | 1 | */ |
|
366 | public function canDelete($member = null) |
||
370 | |||
371 | /* |
||
372 | * Determine if this recipient may receive notifications for this submission |
||
373 | * |
||
374 | * @param array $data |
||
375 | 2 | * @param Form $form |
|
376 | * @return bool |
||
377 | */ |
||
378 | 2 | public function canSend($data, $form) |
|
402 | |||
403 | /** |
||
404 | * Make sure the email template saved against the recipient exists on the file system. |
||
405 | * |
||
406 | * @param string |
||
407 | 2 | * |
|
408 | * @return boolean |
||
409 | 2 | */ |
|
410 | public function emailTemplateExists($template = '') |
||
416 | |||
417 | /** |
||
418 | * Get the email body for the current email format |
||
419 | 2 | * |
|
420 | * @return string |
||
421 | 2 | */ |
|
422 | public function getEmailBodyContent() |
||
426 | |||
427 | /** |
||
428 | * Gets a list of email templates suitable for populating the email template dropdown. |
||
429 | 4 | * |
|
430 | * @return array |
||
431 | 4 | */ |
|
432 | public function getEmailTemplateDropdownValues() |
||
449 | |||
450 | /** |
||
451 | * Validate that valid email addresses are being used |
||
452 | * |
||
453 | * @return ValidationResult |
||
454 | */ |
||
455 | public function validate() { |
||
478 | } |
||
479 |
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.