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 |
||
| 30 | class UserFormFieldEditorExtension extends DataExtension |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private static $has_many = array( |
||
|
|
|||
| 36 | 'Fields' => EditableFormField::class |
||
| 37 | ); |
||
| 38 | |||
| 39 | private static $owns = [ |
||
| 40 | 'Fields' |
||
| 41 | ]; |
||
| 42 | |||
| 43 | private static $cascade_deletes = [ |
||
| 44 | 'Fields' |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Adds the field editor to the page. |
||
| 49 | * |
||
| 50 | * @return FieldList |
||
| 51 | */ |
||
| 52 | public function updateCMSFields(FieldList $fields) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Gets the field editor, for adding and removing EditableFormFields. |
||
| 64 | * |
||
| 65 | * @return GridField |
||
| 66 | */ |
||
| 67 | public function getFieldEditorGrid() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * A UserForm must have at least one step. |
||
| 121 | * If no steps exist, create an initial step, and put all fields inside it. |
||
| 122 | * |
||
| 123 | * @param bool $force |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function createInitialFormStep($force = false) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Ensure that at least one page exists at the start |
||
| 163 | */ |
||
| 164 | public function onAfterWrite() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Remove any orphaned child records on publish |
||
| 171 | */ |
||
| 172 | public function onAfterPublish() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Remove all fields from the live stage when unpublishing the page |
||
| 204 | */ |
||
| 205 | public function onAfterUnpublish() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * When duplicating a UserDefinedForm, duplicate all of its fields and display rules |
||
| 214 | * |
||
| 215 | * @see \SilverStripe\ORM\DataObject::duplicate |
||
| 216 | * @param \SilverStripe\ORM\DataObject $oldPage |
||
| 217 | * @param bool $doWrite |
||
| 218 | * @param string $manyMany |
||
| 219 | * @return \SilverStripe\ORM\DataObject |
||
| 220 | */ |
||
| 221 | public function onAfterDuplicate($oldPage, $doWrite, $manyMany) |
||
| 222 | { |
||
| 223 | // List of EditableFieldGroups, where the key of the array is the ID of the old end group |
||
| 224 | $fieldGroups = []; |
||
| 225 | foreach ($oldPage->Fields() as $field) { |
||
| 226 | $newField = $field->duplicate(false); |
||
| 227 | $newField->ParentID = $this->owner->ID; |
||
| 228 | $newField->ParentClass = $this->owner->ClassName; |
||
| 229 | $newField->Version = 0; |
||
| 230 | $newField->write(); |
||
| 231 | |||
| 232 | // If we encounter a group start, record it for later use |
||
| 233 | if ($field instanceof EditableFieldGroup) { |
||
| 234 | $fieldGroups[$field->EndID] = $newField; |
||
| 235 | } |
||
| 236 | |||
| 237 | // If we encounter an end group, link it back to the group start |
||
| 238 | if ($field instanceof EditableFieldGroupEnd && isset($fieldGroups[$field->ID])) { |
||
| 239 | $groupStart = $fieldGroups[$field->ID]; |
||
| 240 | $groupStart->EndID = $newField->ID; |
||
| 241 | $groupStart->write(); |
||
| 242 | } |
||
| 243 | |||
| 244 | View Code Duplication | foreach ($field->DisplayRules() as $customRule) { |
|
| 245 | $newRule = $customRule->duplicate(false); |
||
| 246 | $newRule->ParentID = $newField->ID; |
||
| 247 | $newRule->Version = 0; |
||
| 248 | $newRule->write(); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Checks child fields to see if any are modified in draft as well. The owner of this extension will still |
||
| 255 | * use the Versioned method to determine its own status. |
||
| 256 | * |
||
| 257 | * @see Versioned::isModifiedOnDraft |
||
| 258 | * |
||
| 259 | * @return boolean|null |
||
| 260 | */ |
||
| 261 | public function isModifiedOnDraft() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @see Versioned::doRevertToLive |
||
| 272 | */ |
||
| 273 | public function onAfterRevertToLive() |
||
| 280 | } |
||
| 281 |
This check marks private properties in classes that are never used. Those properties can be removed.