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 |
||
| 8 | class DataProcessor |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var int |
||
| 12 | */ |
||
| 13 | protected $start; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Columns to escape value. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $escapeColumns = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Processed data output. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $output = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $appendColumns = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $editColumns = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $excessColumns = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var mixed |
||
| 46 | */ |
||
| 47 | protected $results; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $templates; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $includeIndex; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $rawColumns; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr']; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param mixed $results |
||
| 71 | * @param array $columnDef |
||
| 72 | * @param array $templates |
||
| 73 | * @param int $start |
||
| 74 | */ |
||
| 75 | public function __construct($results, array $columnDef, array $templates, $start) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Process data to output on browser. |
||
| 91 | * |
||
| 92 | * @param bool $object |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function process($object = false) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Process add columns. |
||
| 114 | * |
||
| 115 | * @param mixed $data |
||
| 116 | * @param mixed $row |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | View Code Duplication | protected function addColumns($data, $row) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Process add index column. |
||
| 131 | * |
||
| 132 | * @param mixed $data |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | protected function addIndexColumn($data) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Process edit columns. |
||
| 148 | * |
||
| 149 | * @param mixed $data |
||
| 150 | * @param mixed $row |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | View Code Duplication | protected function editColumns($data, $row) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Setup additional DT row variables. |
||
| 165 | * |
||
| 166 | * @param mixed $data |
||
| 167 | * @param mixed $row |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function setupRowVariables($data, $row) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get only needed columns. |
||
| 184 | * |
||
| 185 | * @param array $data |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function selectOnlyNeededColumns(array $data) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Remove declared hidden columns. |
||
| 199 | * |
||
| 200 | * @param array $data |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | protected function removeExcessColumns(array $data) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Flatten array with exceptions. |
||
| 214 | * |
||
| 215 | * @param array $array |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function flatten(array $array) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Escape all values of row. |
||
| 235 | * |
||
| 236 | * @param array $row |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | protected function escapeRow(array $row) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Whether to escape column or no. |
||
| 258 | * |
||
| 259 | * @param string $key |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | protected function shouldEscapeColumn($key) |
||
| 272 | } |
||
| 273 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: