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 |
||
| 37 | abstract class Action implements Renderable |
||
| 38 | { |
||
| 39 | use Authorizable; |
||
| 40 | |||
| 41 | const INPUT_NAME = '_input'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Response |
||
| 45 | */ |
||
| 46 | protected $response; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $selector; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | public $event = 'click'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $method = 'POST'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $attributes = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $selectorPrefix = '.action-'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Interactor\Interactor |
||
| 75 | */ |
||
| 76 | protected $interactor; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected static $selectors = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | public $name; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Action constructor. |
||
| 90 | */ |
||
| 91 | public function __construct() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @throws \Exception |
||
| 98 | */ |
||
| 99 | protected function initInteractor() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get batch action title. |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function name() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $prefix |
||
| 126 | * @return mixed|string |
||
| 127 | */ |
||
| 128 | public function selector($prefix) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $class |
||
| 139 | * @param string $prefix |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public static function makeSelector($class, $prefix) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $name |
||
| 154 | * @param string $value |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function attribute($name, $value) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Format the field attributes. |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | View Code Duplication | protected function formatAttributes() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | protected function getElementClass() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return Response |
||
| 191 | */ |
||
| 192 | public function response() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getMethod() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | public function getCalledClass() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getHandleRoute() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | protected function getModelClass() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function parameters() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param Request $request |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function validate(Request $request) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | protected function addScript() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function actionScript() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function buildActionPromise() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function handleActionPromise() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $method |
||
| 384 | * @param array $arguments |
||
| 385 | * |
||
| 386 | * @return mixed |
||
| 387 | * |
||
| 388 | * @throws \Exception |
||
| 389 | */ |
||
| 390 | public function __call($method, $arguments = []) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function html() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function render() |
|
| 422 | } |
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.