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 | class DataCellWidths |
||
| 10 | { |
||
| 11 | protected $widths; |
||
| 12 | |||
| 13 | public function __consturct() |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Calculate the longest cell data from any row of each of the cells. |
||
| 20 | */ |
||
| 21 | View Code Duplication | public function calculateLongestCell($rows) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Calculate the longest word and longest line in the provided data. |
||
| 39 | */ |
||
| 40 | View Code Duplication | public function calculateLongestWord($rows) |
|
| 55 | |||
| 56 | public function paddingSpace( |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Find all columns that are shorter than the specified threshold width. |
||
| 66 | * These are removed from this object, and returned as the result of |
||
| 67 | * this method. |
||
| 68 | */ |
||
| 69 | public function removeShortColumns($thresholdWidth) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Find all of the columns that are shorter than the specified threshold. |
||
| 78 | */ |
||
| 79 | public function findShortColumns($thresholdWidth) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Remove all of the specified columns from this data structure. |
||
| 94 | */ |
||
| 95 | public function removeColumns($columnKeys) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return proportional weights |
||
| 104 | */ |
||
| 105 | public function distribute($availableWidth) |
||
| 122 | |||
| 123 | public function lastColumn() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return the available keys (column identifiers) from the calculated |
||
| 131 | * data set. |
||
| 132 | */ |
||
| 133 | public function keys() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Return the length of the specified column. |
||
| 140 | */ |
||
| 141 | public function width($key) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return all of the lengths |
||
| 148 | */ |
||
| 149 | public function widths() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the sum of the lengths of the provided widths. |
||
| 156 | */ |
||
| 157 | public function totalWidth() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return the sum of the lengths of the provided widths. |
||
| 164 | */ |
||
| 165 | public static function sumWidth($widths) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the length of the longest word in the string. |
||
| 177 | * @param string $str |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | View Code Duplication | protected static function longestWordLength($str) |
|
| 188 | } |
||
| 189 |
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.