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:
Complex classes like CHTMLTable 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 CHTMLTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CHTMLTable |
||
| 18 | { |
||
| 19 | const FOOTER = 'footer'; |
||
| 20 | |||
| 21 | private $tableSpec; |
||
| 22 | private $tableHead; |
||
| 23 | private $tableBody; |
||
| 24 | private $tableFoot; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor |
||
| 28 | * |
||
| 29 | * Creates a table with table head, table body and if specified, a table |
||
| 30 | * footer. It is possible to specify the table and the tabel cells settings |
||
| 31 | * per column. |
||
| 32 | * |
||
| 33 | * @param string[] $tableSpecs table settings. |
||
| 34 | * @param mixed[] $data table cell data. |
||
| 35 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 36 | */ |
||
| 37 | 11 | public function __construct($tableSpecs = [], $data = [], $columnSpecs = []) |
|
| 38 | { |
||
| 39 | 11 | $this->create($tableSpecs, $data, $columnSpecs); |
|
| 40 | 11 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Creates a table with cell data. |
||
| 44 | * |
||
| 45 | * Creates a table with table head, table body with table data and if |
||
| 46 | * specified, a table footer. It is possible to specify the table and the |
||
| 47 | * tabel cells settings per column. |
||
| 48 | * |
||
| 49 | * @param string[] $tableSpecs table settings. |
||
| 50 | * @param mixed[] $data table cell data. |
||
| 51 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 52 | * |
||
| 53 | * @return object the html table object. |
||
| 54 | */ |
||
| 55 | 11 | public function create($tableSpecs = [], $data = [], $columnSpecs = []) |
|
| 56 | { |
||
| 57 | 11 | $this->resetTableTags(); |
|
| 58 | 11 | $this->setTableSpecifications($tableSpecs); |
|
| 59 | |||
| 60 | 11 | $this->createTableHead($data, $columnSpecs); |
|
| 61 | 11 | $this->createTableBody($data, $columnSpecs); |
|
| 62 | 11 | $this->createTableFooter($columnSpecs); |
|
| 63 | |||
| 64 | 11 | return $this; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Helper method to reset main parts of table tags. |
||
| 69 | * |
||
| 70 | * Sets the table head, table body and table foot tag to null. |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | 11 | private function resetTableTags() |
|
| 75 | { |
||
| 76 | 11 | $this->tableHead = null; |
|
| 77 | 11 | $this->tableBody = null; |
|
| 78 | 11 | $this->tableFoot = null; |
|
| 79 | 11 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Helper method to set the table specifications. |
||
| 83 | * |
||
| 84 | * Merges the table specifications with the default specifications. |
||
| 85 | * Default table CSS id is html-table. |
||
| 86 | * |
||
| 87 | * @param string[] $tableSpec the table specification. |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 11 | private function setTableSpecifications($tableSpec) |
|
| 92 | { |
||
| 93 | $defaults = [ |
||
| 94 | // Always have a id for the form |
||
| 95 | 11 | 'id' => 'html-table', |
|
| 96 | 11 | ]; |
|
| 97 | |||
| 98 | 11 | if ($this->isClassPresent($tableSpec)) { |
|
| 99 | 1 | $tableSpec = $this->removeId($tableSpec); |
|
| 100 | 1 | } |
|
| 101 | |||
| 102 | 11 | $this->tableSpec = array_merge($defaults, $tableSpec); |
|
| 103 | 11 | } |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Helper method to check if a CSS class tag is present |
||
| 107 | * |
||
| 108 | * Checks if a CSS class tag is present in the table specification. |
||
| 109 | * |
||
| 110 | * @param string[] $tableSpec the table specification. |
||
| 111 | * |
||
| 112 | * @return boolean true if class is present in the table specification, |
||
| 113 | * false otherwise. |
||
| 114 | */ |
||
| 115 | 11 | private function isClassPresent($tableSpec) |
|
| 116 | { |
||
| 117 | 11 | return isset($tableSpec['class']) ? true : false; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Helper method to reset the id tag. |
||
| 122 | * |
||
| 123 | * Sets the CSS id tag to null. |
||
| 124 | * |
||
| 125 | * @param string[] $tableSpec the table specification. |
||
| 126 | * |
||
| 127 | * @return string[] the table specification without the CSS id tag. |
||
| 128 | */ |
||
| 129 | 1 | private function removeId($tableSpec) { |
|
| 130 | 1 | $tableSpec['id'] = null; |
|
| 131 | |||
| 132 | 1 | return $tableSpec; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Helper method to create the table head. |
||
| 137 | * |
||
| 138 | * Creates the table head. The title of the columns are set according to |
||
| 139 | * the table tag in the column specifications. Otherwise, the title is set |
||
| 140 | * to the keys name in the table cell data array. |
||
| 141 | * |
||
| 142 | * @param mixed[] $data table cell data. |
||
| 143 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 144 | * |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | 11 | private function createTableHead($data, $columnSpecs) |
|
| 148 | { |
||
| 149 | 11 | $this->tableHead = "\n<thead>"; |
|
| 150 | 11 | $this->tableHead .= "\n<tr>"; |
|
| 151 | |||
| 152 | 11 | if (empty($columnSpecs)) |
|
| 153 | 11 | { |
|
| 154 | 11 | $this->setColumnTitlesFromData($data); |
|
| 155 | 11 | } else { |
|
| 156 | 7 | $this->setColumnTitlesFromColumnSpecifications($columnSpecs); |
|
| 157 | } |
||
| 158 | |||
| 159 | 11 | $this->tableHead .= "\n</tr>"; |
|
| 160 | 11 | $this->tableHead .= "\n</thead>"; |
|
| 161 | 11 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Helper method to set the column titles from the data array. |
||
| 165 | * |
||
| 166 | * Uses the first row in the table cell data array to set the titles of |
||
| 167 | * the columns. The name of the columns are the key name for the objects in |
||
| 168 | * the array containing data for the table. |
||
| 169 | * |
||
| 170 | * @param mixed[] $data table cell data. |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | 11 | private function setColumnTitlesFromData($data) |
|
| 175 | { |
||
| 176 | 11 | $firstRow = isset($data[0]) ? $data[0] : []; |
|
| 177 | 11 | foreach ($firstRow as $key => $value) { |
|
| 178 | 4 | $this->tableHead .= "\n<th>"; |
|
| 179 | 4 | $this->tableHead .= $key; |
|
| 180 | 4 | $this->tableHead .= "</th>"; |
|
| 181 | 11 | } |
|
| 182 | 11 | } |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Helper method to set the column titles from column specifications. |
||
| 186 | * |
||
| 187 | * Uses column specifications to set the name of the columns in the table |
||
| 188 | * head. |
||
| 189 | * |
||
| 190 | * @param mixed[] $columnSpecs table columns cell settings |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | 7 | private function setColumnTitlesFromColumnSpecifications($columnSpecs) |
|
| 195 | { |
||
| 196 | 7 | foreach ($columnSpecs as $key => $columnSpec) { |
|
| 197 | 7 | if (!$this->isTableFooter($columnSpec)) { |
|
| 198 | 7 | $this->tableHead .= "\n<th>"; |
|
| 199 | 7 | $this->tableHead .= $this->getTitle($key, $columnSpec); |
|
|
|
|||
| 200 | 7 | $this->tableHead .= "</th>"; |
|
| 201 | 7 | } |
|
| 202 | 7 | } |
|
| 203 | 7 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Helper method to check if the column cell belongs to the footer. |
||
| 207 | * |
||
| 208 | * Checks the type tag, in the column specification for one column, if the |
||
| 209 | * tag is present and set to footer. |
||
| 210 | * |
||
| 211 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 212 | * |
||
| 213 | * @return boolean true if the cell type belongs to the footer, false otherwise. |
||
| 214 | */ |
||
| 215 | 7 | private function isTableFooter($columnSpec) |
|
| 216 | { |
||
| 217 | 7 | $isFooter = false; |
|
| 218 | 7 | if (isset($columnSpec['type'])) { |
|
| 219 | 2 | if (strcmp($columnSpec['type'], self::FOOTER) === 0) { |
|
| 220 | 2 | $isFooter = true; |
|
| 221 | 2 | } |
|
| 222 | 2 | } |
|
| 223 | |||
| 224 | 7 | return $isFooter; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Helper method to get title from a column specification, if specified. |
||
| 229 | * |
||
| 230 | * Uses the title tag in the column specification for one column to get |
||
| 231 | * the title. If the title tag is not set, the title is the key for the |
||
| 232 | * objects int the array containing data for the table. |
||
| 233 | * |
||
| 234 | * @param array<integer,string> $key the name of the key for the |
||
| 235 | * table cell data. |
||
| 236 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 237 | * |
||
| 238 | * @return string[] the name from the title tag in the cell specification. |
||
| 239 | * Otherwise, the table cell data key name. |
||
| 240 | */ |
||
| 241 | 7 | private function getTitle($key, $columnSpec) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Helper method to create the table body with table cell data. |
||
| 248 | * |
||
| 249 | * Sets the table cell data in the table body. |
||
| 250 | * |
||
| 251 | * @param mixed[] $data table cell data. |
||
| 252 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 253 | * |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | 11 | private function createTableBody($data, $columnSpecs) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Helper method to set table data in table body. |
||
| 266 | * |
||
| 267 | * Sets table data according to the column specifications, if it is |
||
| 268 | * specified. Otherwise it sets the data as it is stored in the data array. |
||
| 269 | * |
||
| 270 | * @param mixed[] $data table cell data. |
||
| 271 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | 11 | private function setTableData($data, $columnSpecs) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Helper method to set table data from the data array. |
||
| 286 | * |
||
| 287 | * Sets table data from the data array. |
||
| 288 | * |
||
| 289 | * @param mixed[] $data table cell data. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 11 | private function setTableDataFromData($data) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Helper method to set table data according to the column specifications. |
||
| 308 | * |
||
| 309 | * Sets the table data according to the column specifications, if the cell |
||
| 310 | * does not belong to the footer. Adds a colspan tag, if it is specified |
||
| 311 | * for the cell in the column. |
||
| 312 | * |
||
| 313 | * @param mixed[] $data table cell data. |
||
| 314 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | 7 | private function setTableDataAsSpecified($data, $columnSpecs) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Helper method to get the colspan value, if specified in the column |
||
| 336 | * specification for the cell. |
||
| 337 | * |
||
| 338 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 339 | * |
||
| 340 | * @return int the colspan value if specified. Otherwise null. |
||
| 341 | */ |
||
| 342 | 7 | private function getColspan($columnSpec) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Helper method to get the value for a specific position in one row in |
||
| 349 | * the data array. |
||
| 350 | * |
||
| 351 | * Gets the data from a specific position in one row in the data array. |
||
| 352 | * If a function is specified for the cell in the column, the data is |
||
| 353 | * runned through the function before it is returned.If the object key is |
||
| 354 | * specified to object, the reference to the object is fetched from the |
||
| 355 | * array of objects. |
||
| 356 | * |
||
| 357 | * @param object $row one row of in the array of table data. |
||
| 358 | * @param string $key the name of the key in the associative data array. |
||
| 359 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 360 | */ |
||
| 361 | 7 | private function getValue($row, $key, $columnSpec) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Helper method t check if the function tag is specified for the cells in |
||
| 378 | * one column. |
||
| 379 | * |
||
| 380 | * Checks if the function tag is set for the cell in one column. |
||
| 381 | * |
||
| 382 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 383 | * |
||
| 384 | * @return boolean true if a function is connected to the cell, false otherwise. |
||
| 385 | */ |
||
| 386 | 7 | private function isFunctionSpecified($columnSpec) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Helper method to check if the object key is specified to object. |
||
| 393 | * |
||
| 394 | * Checks if the object key starts with object. The check is case insensitive. |
||
| 395 | * |
||
| 396 | * @param string $key the name of the key for the object. |
||
| 397 | * |
||
| 398 | * @return boolean true if the key starts with object, false otherwise. |
||
| 399 | */ |
||
| 400 | 3 | private function isObjectSpecifiedAsKey($key) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Helper method to run the value through a function before it is returned. |
||
| 409 | * |
||
| 410 | * Runs the value through a function, if a function is connected to the cell |
||
| 411 | * in the column. If not function is connected to the cell through the |
||
| 412 | * column specification, the value is returned as it is. |
||
| 413 | * |
||
| 414 | * @param mixed[] $columnSpec cell settings for one column |
||
| 415 | * @param mixed $dataValue the value to run through function, if specified. |
||
| 416 | * |
||
| 417 | * @return the value. |
||
| 418 | */ |
||
| 419 | 3 | private function getValueThroughFunction($columnSpec, $dataValue) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Helper method to create table footer with data. |
||
| 430 | * |
||
| 431 | * Creates table footer if the cell settings for the column is set to |
||
| 432 | * footer in the column specifications. |
||
| 433 | * Adds a colspan tag, if it is specified for the cell in the column. |
||
| 434 | * |
||
| 435 | * @param mixed[] $columnSpecs table columns cell settings. |
||
| 436 | * |
||
| 437 | * @return void |
||
| 438 | */ |
||
| 439 | 11 | private function createTableFooter($columnSpecs) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Helper method to get table footer data. |
||
| 457 | * |
||
| 458 | * Gets table footer data from the column specification. Checks if the |
||
| 459 | * value should be fetched from a function or from the value tag. |
||
| 460 | * If either the function or the value specified, an empty string is |
||
| 461 | * returned. |
||
| 462 | * |
||
| 463 | * @param mixed[] $columnSpec cell settings for one column. |
||
| 464 | * |
||
| 465 | * @return mixed the cell data value. |
||
| 466 | */ |
||
| 467 | 2 | private function getFooterData($columnSpec) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Gets the table. |
||
| 478 | * |
||
| 479 | * Gets the table with table data. |
||
| 480 | * |
||
| 481 | * @return html the table with table data. |
||
| 482 | */ |
||
| 483 | 11 | public function getHTMLTable() |
|
| 498 | } |
||
| 499 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: