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 |
||
| 18 | View Code Duplication | class DuplicateRecordsCommandController extends CommandController |
|
|
|
|||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $message = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $duplicateRecords = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \TYPO3\CMS\Core\Mail\MailMessage |
||
| 33 | */ |
||
| 34 | protected $mailMessage; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Check whether the Index is Ok. In case not, display a message on the console. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function analyseCommand() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Print a message and store its content in a variable for the email report. |
||
| 95 | * |
||
| 96 | * @param string $message |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | protected function printOut($message = '') |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Send a possible report to an admin. |
||
| 107 | * |
||
| 108 | * @throws \Exception |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | protected function sendReport() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Send a report |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | protected function hasReport() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | protected function getTo() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | protected function getFrom() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return StorageRepository|object |
||
| 181 | */ |
||
| 182 | protected function getStorageRepository() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return \TYPO3\CMS\Core\Mail\MailMessage|object |
||
| 189 | */ |
||
| 190 | public function getMailMessage() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return a pointer to the database. |
||
| 200 | * |
||
| 201 | * @return \Fab\Media\Index\IndexAnalyser|object |
||
| 202 | */ |
||
| 203 | protected function getIndexAnalyser() |
||
| 207 | |||
| 208 | } |
||
| 209 |
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.