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 __construct($widths = [])  | 
            ||
| 14 |     { | 
            ||
| 15 | $this->widths = $widths;  | 
            ||
| 16 | }  | 
            ||
| 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(  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * Find all columns that are shorter than the specified threshold width.  | 
            ||
| 67 | * These are removed from this object, and returned as the result of  | 
            ||
| 68 | * this method.  | 
            ||
| 69 | */  | 
            ||
| 70 | public function removeShortColumns($thresholdWidth)  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * Find all of the columns that are shorter than the specified threshold.  | 
            ||
| 79 | */  | 
            ||
| 80 | public function findShortColumns($thresholdWidth)  | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Remove all of the specified columns from this data structure.  | 
            ||
| 95 | */  | 
            ||
| 96 | public function removeColumns($columnKeys)  | 
            ||
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * Need to think about the name of this function a bit.  | 
            ||
| 105 | * Maybe second parameter is just a column count.  | 
            ||
| 106 | */  | 
            ||
| 107 | public function adjustMinimumWidths($availableWidth, DataCellWidths $dataCellWidths)  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Return proportional weights  | 
            ||
| 113 | */  | 
            ||
| 114 | public function distribute($availableWidth)  | 
            ||
| 134 | |||
| 135 | public function lastColumn()  | 
            ||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Return the available keys (column identifiers) from the calculated  | 
            ||
| 143 | * data set.  | 
            ||
| 144 | */  | 
            ||
| 145 | public function keys()  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Set the length of the specified column.  | 
            ||
| 152 | */  | 
            ||
| 153 | public function setWidth($key, $width)  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * Return the length of the specified column.  | 
            ||
| 160 | */  | 
            ||
| 161 | public function width($key)  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Return all of the lengths  | 
            ||
| 168 | */  | 
            ||
| 169 | public function widths()  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Return true if there is no data in this object  | 
            ||
| 176 | */  | 
            ||
| 177 | public function isEmpty()  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Return the sum of the lengths of the provided widths.  | 
            ||
| 184 | */  | 
            ||
| 185 | public function totalWidth()  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Return the sum of the lengths of the provided widths.  | 
            ||
| 192 | */  | 
            ||
| 193 | public static function sumWidth($widths)  | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Ensure that every item in $widths that has a corresponding entry  | 
            ||
| 205 | * in $minimumWidths is as least as large as the minimum value held there.  | 
            ||
| 206 | */  | 
            ||
| 207 | public function enforceMinimums($minimumWidths)  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Combine this set of widths with another set, and return  | 
            ||
| 224 | * a new set that contains the entries from both.  | 
            ||
| 225 | */  | 
            ||
| 226 | public function combine(DataCellWidths $combineWith)  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * Return the length of the longest word in the string.  | 
            ||
| 235 | * @param string $str  | 
            ||
| 236 | * @return int  | 
            ||
| 237 | */  | 
            ||
| 238 | protected static function longestWordLength($str)  | 
            ||
| 246 | }  | 
            ||
| 247 | 
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.