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 |
||
| 45 | class Form implements Renderable |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $id = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $action = '/'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $method = 'POST'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var []Field |
||
| 64 | */ |
||
| 65 | protected $fields = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $data = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Form constructor. |
||
| 74 | * |
||
| 75 | * @param array $data |
||
| 76 | */ |
||
| 77 | public function __construct($data = []) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Action uri of the form. |
||
| 92 | * |
||
| 93 | * @param string $action |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function action($action) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Method of the form. |
||
| 105 | * |
||
| 106 | * @param string $method |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function method($method = 'POST') |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Find field class with given name. |
||
| 118 | * |
||
| 119 | * @param string $method |
||
| 120 | * @return bool|string |
||
| 121 | */ |
||
| 122 | View Code Duplication | public static function findFieldClass($method) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Add a form field to form. |
||
| 139 | * |
||
| 140 | * @param Field $field |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | protected function pushField(Field &$field) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get variables for render form. |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | protected function getVariables() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Generate a Field object and add to form builder if Field exists. |
||
| 170 | * |
||
| 171 | * @param string $method |
||
| 172 | * @param array $arguments |
||
| 173 | * |
||
| 174 | * @return Field|null |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function __call($method, $arguments) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Render the form. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function render() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Output as string. |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function __toString() |
||
| 210 | } |
||
| 211 |
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.