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 |
||
| 9 | abstract class BaseColumn |
||
| 10 | { |
||
| 11 | use Configurable; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | public $title = ''; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string|mixed |
||
| 20 | */ |
||
| 21 | public $value = ''; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $headerHtmlOptions = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public $contentHtmlOptions = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string - allowed: raw, url, email, text, image |
||
| 35 | */ |
||
| 36 | public $contentFormat = 'text'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Value when |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $emptyValue = '-'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * BaseColumn constructor. |
||
| 46 | * @param array $config |
||
| 47 | * @throws \Woo\GridView\Exceptions\GridViewConfigException |
||
| 48 | */ |
||
| 49 | public function __construct(array $config) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | protected function configTests(): array |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Formatted header html options |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function headerHtmlOptions() : string |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Formatted content html options |
||
| 80 | * @param array $context |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function contentHtmlOptions(array $context) : string |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Render column value for row |
||
| 90 | * @param array|object $row |
||
| 91 | * @return string|mixed |
||
| 92 | */ |
||
| 93 | protected abstract function _renderValue($row); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Renders column content |
||
| 97 | * @param $row |
||
| 98 | * @return string |
||
| 99 | * @throws GridViewConfigException |
||
| 100 | */ |
||
| 101 | public function renderValue($row) |
||
| 125 | |||
| 126 | } |
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.