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 |
||
| 19 | class ObjectFormWidget extends FormWidget implements ObjectContainerInterface |
||
| 20 | { |
||
| 21 | use ObjectContainerTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $formIdent; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Container $container The DI container. |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function setDependencies(Container $container) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function widgetType() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param array|ArrayInterface $data The widget data. |
||
| 50 | * @return ObjectForm Chainable |
||
| 51 | */ |
||
| 52 | public function setData($data) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $url The next URL. |
||
| 70 | * @throws InvalidArgumentException If argument is not a string. |
||
| 71 | * @return ActionInterface Chainable |
||
| 72 | */ |
||
| 73 | public function setNextUrl($url) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Form action (target URL) |
||
| 92 | * |
||
| 93 | * @return string Relative URL |
||
| 94 | */ |
||
| 95 | public function action() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $formIdent The form ident. |
||
| 113 | * @throws InvalidArgumentException If the argument is not a string. |
||
| 114 | * @return ObjectForm Chainable |
||
| 115 | */ |
||
| 116 | public function setFormIdent($formIdent) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function formIdent() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Set the data from an object. |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function dataFromObject() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * FormProperty Generator |
||
| 156 | * |
||
| 157 | * @param array $group An optional group to use. |
||
| 158 | * @throws Exception If a property data is invalid. |
||
| 159 | * @return void This is a generator. |
||
| 160 | */ |
||
| 161 | public function formProperties(array $group = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function formData() |
||
| 199 | } |
||
| 200 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: