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 |
||
| 21 | abstract class AbstractForm implements FormInterface, Iterator |
||
| 22 | { |
||
| 23 | /** @var NestedElementInterface */ |
||
| 24 | protected $form; |
||
| 25 | /** @var string */ |
||
| 26 | protected $name; |
||
| 27 | /** @var string */ |
||
| 28 | protected $salt; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | protected $uniqueFormNamePostfix = ''; |
||
| 32 | |||
| 33 | // The implementation of the Iterator interface. |
||
| 34 | use IteratorTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * AbstractForm constructor. |
||
| 38 | * |
||
| 39 | * @param string $name |
||
| 40 | * @param string $action |
||
| 41 | * @param string $method |
||
| 42 | */ |
||
| 43 | final public function __construct($name, $action = '', $method = 'POST') |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the form container element. E.g.: for HTML forms it is the <form> tag. |
||
| 65 | * |
||
| 66 | * @return NestedElementInterface |
||
| 67 | */ |
||
| 68 | abstract protected function getFormContainer(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Initialize form. |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | abstract protected function initForm(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Gets form name. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getName() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set unique identifier for the form. |
||
| 89 | * |
||
| 90 | * @param string $salt |
||
| 91 | * @return AbstractForm |
||
| 92 | */ |
||
| 93 | public function setNameSalt($salt) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Sets form action. |
||
| 102 | * |
||
| 103 | * @param string $action |
||
| 104 | * @return AbstractForm |
||
| 105 | */ |
||
| 106 | View Code Duplication | protected function setAction($action) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Sets form submit. |
||
| 118 | * |
||
| 119 | * @param string $method |
||
| 120 | * @return AbstractForm |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function setMethod($method = 'POST') |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Sets form auto-complete option. |
||
| 134 | * |
||
| 135 | * @param bool $autoComplete |
||
| 136 | * @return AbstractForm |
||
| 137 | */ |
||
| 138 | public function setAutoComplete($autoComplete = true) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Sets form encoding type. |
||
| 165 | * |
||
| 166 | * @param string $encodingType |
||
| 167 | * @return AbstractForm |
||
| 168 | */ |
||
| 169 | protected function setEnctype($encodingType = 'application/x-www-form-urlencoded') |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Adds a form element to the form. |
||
| 181 | * |
||
| 182 | * @param array<FormElementInterface> $nodes |
||
| 183 | * @return AbstractForm |
||
| 184 | */ |
||
| 185 | protected function setNodes(array $nodes) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the form elements. |
||
| 194 | * |
||
| 195 | * @return array<FormElementInterface> |
||
| 196 | */ |
||
| 197 | public function getNodes() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Validates the form. |
||
| 204 | * |
||
| 205 | * @return boolean |
||
| 206 | */ |
||
| 207 | public function isValid() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sets form data. |
||
| 214 | * |
||
| 215 | * @param array $data |
||
| 216 | * @return FormInterface |
||
| 217 | */ |
||
| 218 | public function setData(array $data) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the form data. |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function getData() |
||
| 240 | } |
||
| 241 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.