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 |
||
| 14 | class QuickCreate implements Renderable |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var Grid |
||
| 18 | */ |
||
| 19 | protected $parent; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Collection |
||
| 23 | */ |
||
| 24 | protected $fields; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * QuickCreate constructor. |
||
| 28 | * |
||
| 29 | * @param Grid $grid |
||
| 30 | */ |
||
| 31 | public function __construct(Grid $grid) |
||
| 36 | |||
| 37 | protected function formatPlaceholder($placeholder) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $column |
||
| 44 | * @param string $placeholder |
||
| 45 | * |
||
| 46 | * @return Text |
||
| 47 | */ |
||
| 48 | public function text($column, $placeholder = '') |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $column |
||
| 59 | * @param string $placeholder |
||
| 60 | * |
||
| 61 | * @return Text |
||
| 62 | */ |
||
| 63 | public function email($column, $placeholder = '') |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $column |
||
| 71 | * @param string $placeholder |
||
| 72 | * |
||
| 73 | * @return Text |
||
| 74 | */ |
||
| 75 | public function ip($column, $placeholder = '') |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $column |
||
| 84 | * @param string $placeholder |
||
| 85 | * |
||
| 86 | * @return Text |
||
| 87 | */ |
||
| 88 | public function url($column, $placeholder = '') |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $column |
||
| 96 | * @param string $placeholder |
||
| 97 | * |
||
| 98 | * @return Text |
||
| 99 | */ |
||
| 100 | public function password($column, $placeholder = '') |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $column |
||
| 109 | * @param string $placeholder |
||
| 110 | * |
||
| 111 | * @return Text |
||
| 112 | */ |
||
| 113 | public function mobile($column, $placeholder = '') |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $column |
||
| 122 | * @param string $placeholder |
||
| 123 | * |
||
| 124 | * @return Text |
||
| 125 | */ |
||
| 126 | public function integer($column, $placeholder = '') |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $column |
||
| 135 | * @param string $placeholder |
||
| 136 | * |
||
| 137 | * @return Select |
||
| 138 | */ |
||
| 139 | public function select($column, $placeholder = '') |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $column |
||
| 150 | * @param string $placeholder |
||
| 151 | * |
||
| 152 | * @return MultipleSelect |
||
| 153 | */ |
||
| 154 | public function multipleSelect($column, $placeholder = '') |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $column |
||
| 165 | * @param string $placeholder |
||
| 166 | * |
||
| 167 | * @return Field\Date |
||
| 168 | */ |
||
| 169 | public function datetime($column, $placeholder = '') |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $column |
||
| 176 | * @param string $placeholder |
||
| 177 | * |
||
| 178 | * @return Field\Date |
||
| 179 | */ |
||
| 180 | public function time($column, $placeholder = '') |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $column |
||
| 187 | * @param string $placeholder |
||
| 188 | * |
||
| 189 | * @return Field\Date |
||
| 190 | */ |
||
| 191 | public function date($column, $placeholder = '') |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param Field $field |
||
| 202 | * |
||
| 203 | * @return Field |
||
| 204 | */ |
||
| 205 | protected function addField(Field $field) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $class |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | View Code Duplication | protected function resolveView($class) |
|
| 227 | |||
| 228 | protected function script() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param int $columnCount |
||
| 287 | * |
||
| 288 | * @return array|string |
||
| 289 | */ |
||
| 290 | public function render($columnCount = 0) |
||
| 305 | } |
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.