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 MakeTable 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 MakeTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace EvolutionCMS\Support; |
||
| 13 | class MakeTable implements MakeTableInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public $actionField = ''; |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public $cellAction = ''; |
||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $linkAction = ''; |
||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | public $tableWidth = 0; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $tableClass = ''; |
||
| 35 | /** |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | public $tableID; |
||
| 39 | /** |
||
| 40 | * @var |
||
| 41 | */ |
||
| 42 | public $thClass; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $rowHeaderClass = ''; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $columnHeaderClass = ''; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $rowRegularClass = ''; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $rowAlternateClass = 'alt'; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $formName = 'tableForm'; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $formAction = '[~[*id*]~]'; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $formElementType = ''; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $formElementName = ''; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $rowAlternatingScheme = 'EVEN'; |
||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | public $excludeFields = array(); |
||
| 83 | /** |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | public $allOption = 0; |
||
| 87 | /** |
||
| 88 | * @var |
||
| 89 | */ |
||
| 90 | public $pageNav; |
||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public $columnWidths = array(); |
||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | public $selectedValues = array(); |
||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | public $fieldHeaders = array(); |
||
| 103 | /** |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | public $extra = ''; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sets the default link href for all cells in the table. |
||
| 110 | * |
||
| 111 | * @param string $value A URL to execute when table cells are clicked. |
||
| 112 | */ |
||
| 113 | public function setCellAction($value) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the default link href for the text presented in a cell. |
||
| 120 | * |
||
| 121 | * @param string $value A URL to execute when text within table cells are clicked. |
||
| 122 | */ |
||
| 123 | public function setLinkAction($value) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Sets the width attribute of the main HTML TABLE. |
||
| 130 | * |
||
| 131 | * @param int $value A valid width attribute for the HTML TABLE tag |
||
| 132 | */ |
||
| 133 | public function setTableWidth($value) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the class attribute of the main HTML TABLE. |
||
| 140 | * |
||
| 141 | * @param string $value A class for the main HTML TABLE. |
||
| 142 | */ |
||
| 143 | public function setTableClass($value) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Sets the id attribute of the main HTML TABLE. |
||
| 150 | * |
||
| 151 | * @param string $value A class for the main HTML TABLE. |
||
| 152 | */ |
||
| 153 | public function setTableID($value) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the class attribute of the table header row. |
||
| 160 | * |
||
| 161 | * @param string $value A class for the table header row. |
||
| 162 | */ |
||
| 163 | public function setRowHeaderClass($value) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Sets the class attribute of the table header row. |
||
| 170 | * |
||
| 171 | * @param string $value A class for the table header row. |
||
| 172 | */ |
||
| 173 | public function setThHeaderClass($value) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the class attribute of the column header row. |
||
| 180 | * |
||
| 181 | * @param string $value A class for the column header row. |
||
| 182 | */ |
||
| 183 | public function setColumnHeaderClass($value) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Sets the class attribute of regular table rows. |
||
| 190 | * |
||
| 191 | * @param string $value A class for regular table rows. |
||
| 192 | */ |
||
| 193 | public function setRowRegularClass($value) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets the class attribute of alternate table rows. |
||
| 200 | * |
||
| 201 | * @param string $value A class for alternate table rows. |
||
| 202 | */ |
||
| 203 | public function setRowAlternateClass($value) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Sets the type of INPUT form element to be presented as the first column. |
||
| 210 | * |
||
| 211 | * @param string $value Indicates the INPUT form element type attribute. |
||
| 212 | */ |
||
| 213 | public function setFormElementType($value) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the name of the INPUT form element to be presented as the first column. |
||
| 220 | * |
||
| 221 | * @param string $value Indicates the INPUT form element name attribute. |
||
| 222 | */ |
||
| 223 | public function setFormElementName($value) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Sets the name of the FORM to wrap the table in when a form element has |
||
| 230 | * been indicated. |
||
| 231 | * |
||
| 232 | * @param string $value Indicates the FORM name attribute. |
||
| 233 | */ |
||
| 234 | public function setFormName($value) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets the action of the FORM element. |
||
| 241 | * |
||
| 242 | * @param string $value Indicates the FORM action attribute. |
||
| 243 | */ |
||
| 244 | public function setFormAction($value) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Excludes fields from the table by array key. |
||
| 251 | * |
||
| 252 | * @param array $value An Array of field keys to exclude from the table. |
||
| 253 | */ |
||
| 254 | public function setExcludeFields($value) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the table to provide alternate row colors using ODD or EVEN rows |
||
| 261 | * |
||
| 262 | * @param string $value 'ODD' or 'EVEN' to indicate the alternate row scheme. |
||
| 263 | */ |
||
| 264 | public function setRowAlternatingScheme($value) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sets the default field value to be used when appending query parameters |
||
| 271 | * to link actions. |
||
| 272 | * |
||
| 273 | * @param string $value The key of the field to add as a query string parameter. |
||
| 274 | */ |
||
| 275 | public function setActionFieldName($value) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Sets the width attribute of each column in the array. |
||
| 282 | * |
||
| 283 | * @param array $value An Array of column widths in the order of the keys in the |
||
|
|
|||
| 284 | * source table array. |
||
| 285 | */ |
||
| 286 | public function setColumnWidths($widthArray) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * An optional array of values that can be preselected when using |
||
| 293 | * |
||
| 294 | * @param array $value Indicates the INPUT form element type attribute. |
||
| 295 | */ |
||
| 296 | public function setSelectedValues($valueArray) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Sets extra content to be presented following the table (but within |
||
| 303 | * the form, if a form is being rendered with the table). |
||
| 304 | * |
||
| 305 | * @param string $value A string of additional content. |
||
| 306 | */ |
||
| 307 | public function setExtra($value) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieves the width of a specific table column by index position. |
||
| 314 | * |
||
| 315 | * @param int $columnPosition The index of the column to get the width for. |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getColumnWidth($columnPosition) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Determines what class the current row should have applied. |
||
| 330 | * |
||
| 331 | * @param int $value The position of the current row being rendered. |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function determineRowClass($position) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Generates an onclick action applied to the current cell, to execute |
||
| 356 | * any specified cell actions. |
||
| 357 | * |
||
| 358 | * @param string $value Indicates the INPUT form element type attribute. |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | View Code Duplication | public function getCellAction($currentActionFieldValue) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Generates the cell content, including any specified action fields values. |
||
| 373 | * |
||
| 374 | * @param string $currentActionFieldValue The value to be applied to the link action. |
||
| 375 | * @param string $value The value of the cell. |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function createCellText($currentActionFieldValue, $value) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Sets an option to generate a check all link when checkbox is indicated |
||
| 390 | * as the table formElementType. |
||
| 391 | */ |
||
| 392 | public function setAllOption() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Function to prepare a link generated in the table cell/link actions. |
||
| 399 | * |
||
| 400 | * @param string $value Indicates the INPUT form element type attribute. |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function prepareLink($link) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Generates the table content. |
||
| 416 | * |
||
| 417 | * @param array $fieldsArray The associative array representing the table rows |
||
| 418 | * and columns. |
||
| 419 | * @param array $fieldHeadersArray An optional array of values for providing |
||
| 420 | * alternative field headers; this is an associative arrays of keys from |
||
| 421 | * the $fieldsArray where the values represent the alt heading content |
||
| 422 | * for each column. |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function create($fieldsArray, $fieldHeadersArray = array(), $linkpage = "") |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Generates optional paging navigation controls for the table. |
||
| 508 | * |
||
| 509 | * @param int $numRecords The number of records to show per page. |
||
| 510 | * @param string $qs An optional query string to be appended to the paging links |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function createPagingNavigation($numRecords, $qs = '') |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Creates an individual page link for the paging navigation. |
||
| 550 | * |
||
| 551 | * @param string $link The link for the page, defaulted to the current document. |
||
| 552 | * @param int $pageNum The page number of the link. |
||
| 553 | * @param string $displayText The text of the link. |
||
| 554 | * @param bool $currentPage Indicates if the link is to the current page. |
||
| 555 | * @param string $qs And optional query string to be appended to the link. |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function createPageLink($link = '', $pageNum, $displayText, $currentPage = false, $qs = '') |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Adds an INPUT form element column to the table. |
||
| 575 | * |
||
| 576 | * @param string $value The value attribute of the element. |
||
| 577 | * @param bool $isChecked Indicates if the checked attribute should apply to the |
||
| 578 | * element. |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | public function addFormField($value, $isChecked) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Generates the proper LIMIT clause for queries to retrieve paged results in |
||
| 594 | * a MakeTable $fieldsArray. |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function handlePaging() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Generates the SORT BY clause for queries used to retrieve a MakeTable |
||
| 607 | * $fieldsArray |
||
| 608 | * |
||
| 609 | * @param bool $natural_order If true, the results are returned in natural order. |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public function handleSorting($natural_order = false) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Generates a link to order by a specific $fieldsArray key; use to generate |
||
| 626 | * sort by links in the MakeTable $fieldHeadingsArray values. |
||
| 627 | * |
||
| 628 | * @param string $key The $fieldsArray key for the column to sort by. |
||
| 629 | * @param string $text The text for the link (e.g. table column header). |
||
| 630 | * @param string $qs An optional query string to append to the order by link. |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | public function prepareOrderByLink($key, $text, $qs = '') |
||
| 649 | |||
| 650 | } |
||
| 651 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.