Complex classes like Table often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Table extends AbstractRendering |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Table settings |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $tableSettings = array( |
||
| 25 | 'default' => array( |
||
| 26 | 'join' => '+', |
||
| 27 | 'xChar' => '-', |
||
| 28 | 'xCharHeader' => '-', |
||
| 29 | 'yChar' => '|', |
||
| 30 | 'yCharHeader' => '|', |
||
| 31 | 'xSpace' => 1, |
||
| 32 | 'ySpace' => 0, |
||
| 33 | 'separateData' => false, |
||
| 34 | 'separateHeader' => true, |
||
| 35 | 'outerTop' => true, |
||
| 36 | 'outerBottom' => true, |
||
| 37 | 'outerLeft' => true, |
||
| 38 | 'outerRight' => true, |
||
| 39 | ), |
||
| 40 | 'ascii_old' => array( |
||
| 41 | 'join' => '+', |
||
| 42 | 'xChar' => '-', |
||
| 43 | 'xCharHeader' => '=', |
||
| 44 | 'yChar' => '|', |
||
| 45 | 'yCharHeader' => '|', |
||
| 46 | 'xSpace' => 1, |
||
| 47 | 'ySpace' => 0, |
||
| 48 | 'separateData' => true, |
||
| 49 | 'separateHeader' => true, |
||
| 50 | 'outerTop' => true, |
||
| 51 | 'outerBottom' => true, |
||
| 52 | 'outerLeft' => true, |
||
| 53 | 'outerRight' => true, |
||
| 54 | ), |
||
| 55 | 'ascii_compact' => array( |
||
| 56 | 'join' => ' ', |
||
| 57 | 'xChar' => ' ', |
||
| 58 | 'xCharHeader' => '=', |
||
| 59 | 'yChar' => ' ', |
||
| 60 | 'yCharHeader' => ' ', |
||
| 61 | 'xSpace' => 0, |
||
| 62 | 'ySpace' => 0, |
||
| 63 | 'separateData' => false, |
||
| 64 | 'separateHeader' => true, |
||
| 65 | 'outerTop' => false, |
||
| 66 | 'outerBottom' => false, |
||
| 67 | 'outerLeft' => false, |
||
| 68 | 'outerRight' => false, |
||
| 69 | ), |
||
| 70 | 'unicode' => array( |
||
| 71 | 'join' => '╬', |
||
| 72 | 'xChar' => '═', |
||
| 73 | 'xCharHeader' => '═', |
||
| 74 | 'yChar' => '║', |
||
| 75 | 'yCharHeader' => '║', |
||
| 76 | 'xSpace' => 1, |
||
| 77 | 'ySpace' => 0, |
||
| 78 | 'separateData' => false, |
||
| 79 | 'separateHeader' => true, |
||
| 80 | 'outerTop' => true, |
||
| 81 | 'outerBottom' => true, |
||
| 82 | 'outerLeft' => true, |
||
| 83 | 'outerRight' => true, |
||
| 84 | ), |
||
| 85 | 'markdown' => array( |
||
| 86 | 'join' => ' ', |
||
| 87 | 'xChar' => ' ', |
||
| 88 | 'xCharHeader' => '-', |
||
| 89 | 'yChar' => '|', |
||
| 90 | 'yCharHeader' => '|', |
||
| 91 | 'xSpace' => 1, |
||
| 92 | 'ySpace' => 0, |
||
| 93 | 'separateData' => false, |
||
| 94 | 'separateHeader' => true, |
||
| 95 | 'outerTop' => false, |
||
| 96 | 'outerBottom' => false, |
||
| 97 | 'outerLeft' => true, |
||
| 98 | 'outerRight' => true, |
||
| 99 | ), |
||
| 100 | ); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Render mode |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $renderMode = 'markdown'; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function renderInternal() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $html |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function parseHtmlTable($html) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the table |
||
| 179 | * |
||
| 180 | * @param $table |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getTable($table) |
||
| 185 | { |
||
| 186 | $lines = array(); |
||
| 187 | $columnsHeaders = $this->columnsHeaders($table); |
||
| 188 | $columns_lengths = $this->columnsLengths($table, $columnsHeaders); |
||
| 189 | |||
| 190 | $topLine = $this->renderLine($columns_lengths, 'top', 'CharHeader'); |
||
| 191 | if ($topLine) { |
||
| 192 | $lines[] = $topLine; |
||
| 193 | } |
||
| 194 | $lines[] = $this->renderHeader($columns_lengths, $columnsHeaders); |
||
| 195 | if ($this->tableSettings[$this->renderMode]['separateHeader']) { |
||
| 196 | $lines[] = $this->renderLine($columns_lengths, 'default', 'CharHeader'); |
||
| 197 | } |
||
| 198 | foreach ($table as $row_cells) { |
||
| 199 | $lines[] = $this->renderCell($row_cells, $columnsHeaders, $columns_lengths); |
||
| 200 | if ($this->tableSettings[$this->renderMode]['separateData']) { |
||
| 201 | $lines[] = $this->renderLine($columns_lengths); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | $bottomLine = $this->renderLine($columns_lengths, 'bottom'); |
||
| 206 | if ($bottomLine) { |
||
| 207 | $lines[] = $bottomLine; |
||
| 208 | } |
||
| 209 | return $lines; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Render a separation line |
||
| 214 | * |
||
| 215 | * @param $columnsLengths |
||
| 216 | * @param string $specialLine |
||
| 217 | * @param string $charMode |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | protected function renderLine($columnsLengths, $specialLine = 'default', $charMode = 'Char') |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $columnsLengths |
||
| 241 | * @param $columnsHeaders |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | protected function renderHeader($columnsLengths, $columnsHeaders) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $row_cells |
||
| 262 | * @param $columns_headers |
||
| 263 | * @param $columns_lengths |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function renderCell($row_cells, $columns_headers, $columns_lengths) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $table |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public function columnsHeaders($table) |
||
| 294 | { |
||
| 295 | return array_keys(reset($table)); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $table |
||
| 300 | * @param $columns_headers |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function columnsLengths($table, $columns_headers) |
||
| 305 | { |
||
| 306 | $lengths = array(); |
||
| 307 | foreach ($columns_headers as $header) { |
||
| 308 | $header_length = mb_strlen($header); |
||
| 309 | $max = $header_length; |
||
| 310 | foreach ($table as $row) { |
||
| 311 | $length = mb_strlen($row[$header]); |
||
| 312 | if ($length > $max) { |
||
| 313 | $max = $length; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | if (($max % 2) != ($header_length % 2)) { |
||
| 318 | $max += 1; |
||
| 319 | } |
||
| 320 | |||
| 321 | $lengths[$header] = $max; |
||
| 322 | } |
||
| 323 | |||
| 324 | return $this->increaseLengthToo100Percent($lengths); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $lengths |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | protected function increaseLengthToo100Percent($lengths) |
||
| 369 | } |
||
| 370 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.