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