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 |
||
| 23 | abstract class AssetFormFactory implements FormFactory |
||
| 24 | { |
||
| 25 | use Extensible; |
||
| 26 | use Injectable; |
||
| 27 | use Configurable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Insert into HTML content area |
||
| 31 | */ |
||
| 32 | const TYPE_INSERT = 'insert'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Select file by ID only |
||
| 36 | */ |
||
| 37 | const TYPE_SELECT = 'select'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Edit form: Default |
||
| 41 | */ |
||
| 42 | const TYPE_ADMIN = 'admin'; |
||
| 43 | |||
| 44 | public function __construct() |
||
| 48 | |||
| 49 | public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = []) |
||
| 50 | { |
||
| 51 | // Validate context |
||
| 52 | foreach ($this->getRequiredContext() as $required) { |
||
| 53 | if (!isset($context[$required])) { |
||
| 54 | throw new InvalidArgumentException("Missing required context $required"); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | $fields = $this->getFormFields($controller, $name, $context); |
||
| 59 | $actions = $this->getFormActions($controller, $name, $context); |
||
| 60 | $validator = $this->getValidator($controller, $name, $context); |
||
| 61 | $form = Form::create($controller, $name, $fields, $actions, $validator); |
||
| 62 | |||
| 63 | // Extend form |
||
| 64 | $this->invokeWithExtensions('updateForm', $form, $controller, $name, $context); |
||
| 65 | |||
| 66 | // Populate form from record |
||
| 67 | if (isset($context['Record'])) { |
||
| 68 | $form->loadDataFrom($context['Record']); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $form; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the validator for the form to be built |
||
| 76 | * |
||
| 77 | * @param $controller |
||
| 78 | * @param $name |
||
| 79 | * @param $context |
||
| 80 | * @return RequiredFields |
||
| 81 | */ |
||
| 82 | protected function getValidator(Controller $controller, $name, $context = []) { |
||
| 83 | $validator = new RequiredFields('Name'); |
||
| 84 | |||
| 85 | return $validator; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get form type from 'type' context |
||
| 90 | * |
||
| 91 | * @param array $context |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function getFormType($context) |
||
| 95 | { |
||
| 96 | return empty($context['Type']) ? static::TYPE_ADMIN : $context['Type']; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Gets the main tabs for the file edit form |
||
| 101 | * |
||
| 102 | * @param File $record |
||
| 103 | * @return TabSet |
||
| 104 | */ |
||
| 105 | protected function getFormFieldTabs($record, $context = []) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param File $record |
||
| 114 | * @return FormAction |
||
| 115 | */ |
||
| 116 | View Code Duplication | protected function getSaveAction($record) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get delete action, if this record is deletable |
||
| 127 | * |
||
| 128 | * @param File $record |
||
| 129 | * @return FormAction |
||
| 130 | */ |
||
| 131 | View Code Duplication | protected function getDeleteAction($record) |
|
| 141 | |||
| 142 | protected function getFormActions(Controller $controller, $name, $context = []) |
||
| 143 | { |
||
| 144 | $record = isset($context['Record']) ? $context['Record'] : null; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get fields for this form |
||
| 161 | * |
||
| 162 | * @param Controller $controller |
||
| 163 | * @param string $name |
||
| 164 | * @param array $context |
||
| 165 | * @return FieldList |
||
| 166 | */ |
||
| 167 | protected function getFormFields(Controller $controller, $name, $context = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Build popup menu |
||
| 194 | * |
||
| 195 | * @param File $record |
||
| 196 | * @return PopoverField |
||
| 197 | */ |
||
| 198 | protected function getPopoverMenu($record) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get actions that go into the Popover menu |
||
| 215 | * |
||
| 216 | * @param $record |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function getPopoverActions($record) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Build "details" formfield tab |
||
| 228 | * |
||
| 229 | * @param File $record |
||
| 230 | * @param array $context |
||
| 231 | * @return Tab |
||
| 232 | */ |
||
| 233 | protected function getFormFieldDetailsTab($record, $context = []) |
||
| 240 | |||
| 241 | public function getRequiredContext() |
||
| 245 | } |
||
| 246 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.