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 ExportPresenter 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 ExportPresenter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ExportPresenter extends BasePresenter |
||
| 24 | { |
||
| 25 | |||
| 26 | const TEMPLATE_DIR = __DIR__ . '/../templates/Export/'; |
||
| 27 | const TEMPLATE_EXT = 'latte'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ProgramModel |
||
| 31 | */ |
||
| 32 | protected $programModel; |
||
| 33 | |||
| 34 | protected $blockModel; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Mpdf\Mpdf |
||
| 38 | */ |
||
| 39 | protected $pdf; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var PHPExcel |
||
| 43 | */ |
||
| 44 | protected $excel; |
||
| 45 | |||
| 46 | protected $filename; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var RegistrationGraphControl |
||
| 50 | */ |
||
| 51 | private $registrationGraphControl; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var MaterialsControl |
||
| 55 | */ |
||
| 56 | private $materialControl; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var MealControl |
||
| 60 | */ |
||
| 61 | private $mealControl; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param ExportModel $export |
||
| 65 | * @param ProgramModel $program |
||
| 66 | * @param ExcelFactory $excel |
||
| 67 | * @param PdfFactory $pdf |
||
| 68 | * @param RegistrationGraphControl $control |
||
| 69 | * @param MaterialsControl $materialControl |
||
| 70 | */ |
||
| 71 | public function __construct( |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function startup() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function renderDefault() |
||
| 116 | |||
| 117 | public function renderEvidence($type, $id = null) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Print Attendance into PDF file |
||
| 160 | * |
||
| 161 | * @param void |
||
| 162 | * @return file PDF file |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function renderAttendance() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Print meal tickets into PDF file |
||
| 188 | * |
||
| 189 | * @param void |
||
| 190 | * @return file PDF file |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function renderMealTicket() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Print name list into PDF file |
||
| 210 | * |
||
| 211 | * @param void |
||
| 212 | * @return file PDF file |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function renderNameList() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $type |
||
| 238 | * @param integer $id |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function renderProgram($type, $id) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Print program cards into PDF file |
||
| 250 | * |
||
| 251 | * @param void |
||
| 252 | * @return file PDF file |
||
| 253 | */ |
||
| 254 | protected function renderProgramCards() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Print large program into PDF file |
||
| 272 | * |
||
| 273 | * @param voide |
||
| 274 | * @return file PDF file |
||
| 275 | */ |
||
| 276 | View Code Duplication | protected function renderProgramLarge() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Print public program into PDF file |
||
| 300 | * |
||
| 301 | * @param void |
||
| 302 | * @return file PDF file |
||
| 303 | */ |
||
| 304 | View Code Duplication | protected function renderProgramPublic() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Print program badges into PDF file |
||
| 324 | * |
||
| 325 | * @param void |
||
| 326 | * @return file PDF file |
||
| 327 | */ |
||
| 328 | protected function renderProgramBadges() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Print visitors on program into PDF file |
||
| 361 | * |
||
| 362 | * @param int program id |
||
| 363 | * @return file PDF file |
||
| 364 | */ |
||
| 365 | protected function renderProgramVisitors($id) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Print details of program into PDF file |
||
| 386 | * |
||
| 387 | * @param void |
||
| 388 | * @return file PDF file |
||
| 389 | */ |
||
| 390 | View Code Duplication | protected function renderProgramDetails() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | public function actionNameBadges() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Print name badges into PDF file |
||
| 418 | * |
||
| 419 | * @param string comma separated values |
||
| 420 | * @return file PDF file |
||
| 421 | */ |
||
| 422 | public function renderNameBadges($namesStringified) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Print data of visitors into excel file |
||
| 454 | * |
||
| 455 | * @return file *.xlsx file type |
||
| 456 | */ |
||
| 457 | public function renderVisitorsExcel() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param string $name |
||
| 576 | * @param array $parameters |
||
| 577 | * @return self |
||
| 578 | */ |
||
| 579 | protected function forgeView($name = '', array $parameters = []) |
||
| 599 | |||
| 600 | protected function publish() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @return RegistrationGraphControl |
||
| 619 | */ |
||
| 620 | protected function createComponentRegistrationGraph() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param RegistrationGraphControl $control |
||
| 627 | * @return $this |
||
| 628 | */ |
||
| 629 | protected function setRegistrationGraphControl(RegistrationGraphControl $control) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @return MaterialControl |
||
| 638 | */ |
||
| 639 | protected function createComponentMaterials() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param MaterialControl $control |
||
| 646 | * @return $this |
||
| 647 | */ |
||
| 648 | protected function setMaterialControl(MaterialsControl $control) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return MealControl |
||
| 657 | */ |
||
| 658 | protected function createComponentMeal() |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param MealControl $control |
||
| 665 | * @return $this |
||
| 666 | */ |
||
| 667 | protected function setMealControl(MealControl $control) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return integer |
||
| 676 | */ |
||
| 677 | protected function calculateGraphHeight() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return ProgramModel |
||
| 694 | */ |
||
| 695 | protected function getProgramModel() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param BlockModel $model |
||
| 702 | * @return $this |
||
| 703 | */ |
||
| 704 | protected function setBlockModel(BlockModel $model) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @return BlockModel |
||
| 712 | */ |
||
| 713 | protected function getBlockModel() |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @param ProgramModel $model |
||
| 720 | * @return $this |
||
| 721 | */ |
||
| 722 | protected function setProgramModel(ProgramModel $model) |
||
| 727 | |||
| 728 | |||
| 729 | /** |
||
| 730 | * @return PHPExcel |
||
| 731 | */ |
||
| 732 | protected function getExcel() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @param PHPExcel $excel |
||
| 739 | * @return $this |
||
| 740 | */ |
||
| 741 | protected function setExcel(\PHPExcel $excel) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @return mPDF |
||
| 749 | */ |
||
| 750 | protected function getPdf() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param Mpdf $pdf |
||
| 757 | * @return self |
||
| 758 | */ |
||
| 759 | protected function setPdf(Mpdf $pdf) |
||
| 764 | |||
| 765 | } |
||
| 766 |
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: