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:
| 1 | <?php |
||
| 24 | class Confirmation extends StringTemplateMessage |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var ApplicationInterface |
||
| 28 | */ |
||
| 29 | protected $application; |
||
| 30 | |||
| 31 | protected $callbacks = array( |
||
| 32 | 'anrede_formell' => 'getFormalSalutation', |
||
| 33 | 'salutation_formal' => 'getFormalSalutation', |
||
| 34 | 'anrede_informell' => 'getInformalSalutation', |
||
| 35 | 'salutation_informal' => 'getInformalSalutation', |
||
| 36 | 'job_title' => 'getJobTitle', |
||
| 37 | 'date' => 'getDate', |
||
| 38 | 'link' => 'getApplicationLink', |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * |
||
| 44 | * @var RouteStackInterface |
||
| 45 | */ |
||
| 46 | protected $router; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * |
||
| 51 | * @var UserInterface |
||
| 52 | */ |
||
| 53 | protected $user; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param RouteStackInterface $router |
||
| 57 | * |
||
| 58 | * @return self |
||
| 59 | */ |
||
| 60 | public function setRouter($router) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param UserInterface $user |
||
| 69 | * |
||
| 70 | * @return self |
||
| 71 | */ |
||
| 72 | public function setUser($user) |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param ApplicationInterface $application |
||
| 84 | * @return StringTemplateMessage |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function setVariablesFromApplication(ApplicationInterface $application) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param ApplicationInterface $application |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function setApplication(ApplicationInterface $application) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param $recruiter |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function setRecruiter($recruiter) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function getFormalSalutation() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | View Code Duplication | protected function getInformalSalutation() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | protected function getJobTitle() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | protected function getDate() |
||
| 167 | |||
| 168 | protected function getApplicationLink() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $subject |
||
| 187 | * @param bool $translate |
||
| 188 | * @return \Zend\Mail\Message |
||
| 189 | */ |
||
| 190 | public function setSubject($subject, $translate = true) |
||
| 197 | } |
||
| 198 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.