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 ReportTcpdf 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 ReportTcpdf, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ReportTcpdf extends TCPDF { |
||
| 26 | /** @var ReportBaseElement[] Array of elements in the header */ |
||
| 27 | public $headerElements = []; |
||
| 28 | |||
| 29 | /** @var ReportBaseElement[] Array of elements in the page header */ |
||
| 30 | public $pageHeaderElements = []; |
||
| 31 | |||
| 32 | /** @var ReportBaseElement[] Array of elements in the footer */ |
||
| 33 | public $footerElements = []; |
||
| 34 | |||
| 35 | /** @var ReportBaseElement[] Array of elements in the body */ |
||
| 36 | public $bodyElements = []; |
||
| 37 | |||
| 38 | /** @var ReportBaseFootnote[] Array of elements in the footer notes */ |
||
| 39 | public $printedfootnotes = []; |
||
| 40 | |||
| 41 | /** @var string Currently used style name */ |
||
| 42 | public $currentStyle; |
||
| 43 | |||
| 44 | /** @var int The last cell height */ |
||
| 45 | public $lastCellHeight = 0; |
||
| 46 | |||
| 47 | /** @var int The largest font size within a TextBox to calculate the height */ |
||
| 48 | public $largestFontHeight = 0; |
||
| 49 | |||
| 50 | /** @var int The last pictures page number */ |
||
| 51 | public $lastpicpage = 0; |
||
| 52 | |||
| 53 | /** @var ReportBase The current report. */ |
||
| 54 | public $wt_report; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * PDF Header -PDF |
||
| 58 | */ |
||
| 59 | public function header() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * PDF Body -PDF |
||
| 82 | */ |
||
| 83 | public function body() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * PDF Footnotes -PDF |
||
| 100 | */ |
||
| 101 | public function footnotes() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * PDF Footer -PDF |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function footer() { |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Add an element to the Header -PDF |
||
| 130 | * |
||
| 131 | * @param object|string $element |
||
| 132 | * |
||
| 133 | * @return int The number of the Header elements |
||
| 134 | */ |
||
| 135 | public function addHeader($element) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Add an element to the Page Header -PDF |
||
| 143 | * |
||
| 144 | * @param object|string $element |
||
| 145 | * |
||
| 146 | * @return int The number of the Page Header elements |
||
| 147 | */ |
||
| 148 | public function addPageHeader($element) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Add an element to the Body -PDF |
||
| 156 | * |
||
| 157 | * @param object|string $element |
||
| 158 | * |
||
| 159 | * @return int The number of the Body elements |
||
| 160 | */ |
||
| 161 | public function addBody($element) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add an element to the Footer -PDF |
||
| 169 | * |
||
| 170 | * @param object|string $element |
||
| 171 | * |
||
| 172 | * @return int The number of the Footer elements |
||
| 173 | */ |
||
| 174 | public function addFooter($element) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Remove the header. |
||
| 182 | * |
||
| 183 | * @param $index |
||
| 184 | */ |
||
| 185 | public function removeHeader($index) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Remove the page header. |
||
| 191 | * |
||
| 192 | * @param $index |
||
| 193 | */ |
||
| 194 | public function removePageHeader($index) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Remove the body. |
||
| 200 | * |
||
| 201 | * @param $index |
||
| 202 | */ |
||
| 203 | public function removeBody($index) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Remove the footer. |
||
| 209 | * |
||
| 210 | * @param $index |
||
| 211 | */ |
||
| 212 | public function removeFooter($index) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Clear the Header -PDF |
||
| 218 | */ |
||
| 219 | public function clearHeader() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Clear the Page Header -PDF |
||
| 226 | */ |
||
| 227 | public function clearPageHeader() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set the report. |
||
| 234 | * |
||
| 235 | * @param $r |
||
| 236 | */ |
||
| 237 | public function setReport($r) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the currently used style name -PDF |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getCurrentStyle() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Setup a style for usage -PDF |
||
| 252 | * |
||
| 253 | * @param string $s Style name |
||
| 254 | */ |
||
| 255 | public function setCurrentStyle($s) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the style -PDF |
||
| 263 | * |
||
| 264 | * @param string $s Style name |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function getStyle($s) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Add margin when static horizontal position is used -PDF |
||
| 279 | * RTL supported |
||
| 280 | * |
||
| 281 | * @param float $x Static position |
||
| 282 | * |
||
| 283 | * @return float |
||
| 284 | */ |
||
| 285 | public function addMarginX($x) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the maximum line width to draw from the curren position -PDF |
||
| 299 | * RTL supported |
||
| 300 | * |
||
| 301 | * @return float |
||
| 302 | */ |
||
| 303 | public function getMaxLineWidth() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the height of the footnote. |
||
| 314 | * |
||
| 315 | * @return int |
||
| 316 | */ |
||
| 317 | public function getFootnotesHeight() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns the the current font size height -PDF |
||
| 328 | * |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | View Code Duplication | public function getCurrentStyleHeight() { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Checks the Footnote and numbers them |
||
| 342 | * |
||
| 343 | * @param object $footnote |
||
| 344 | * |
||
| 345 | * @return bool false if not numbered befor | object if already numbered |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function checkFootnote($footnote) { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Used this function instead of AddPage() |
||
| 371 | * This function will make sure that images will not be overwritten |
||
| 372 | */ |
||
| 373 | public function newPage() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Add a page if needed -PDF |
||
| 382 | * |
||
| 383 | * @param int $height Cell height |
||
| 384 | * |
||
| 385 | * @return bool true in case of page break, false otherwise |
||
| 386 | */ |
||
| 387 | public function checkPageBreakPDF($height) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns the remaining width between the current position and margins -PDF |
||
| 393 | * |
||
| 394 | * @return float Remaining width |
||
| 395 | */ |
||
| 396 | public function getRemainingWidthPDF() { |
||
| 399 | } |
||
| 400 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: