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 |
||
| 21 | class BeerCommand extends AbstractCommand |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * AbstractCommand constructor. |
||
| 25 | * |
||
| 26 | * @param Payload $payload |
||
| 27 | * @param RealTimeClient $client |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function __construct(Payload $payload, RealTimeClient $client) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * stats for all beer counter. |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | * |
||
| 47 | * @throws \Doctrine\DBAL\DBALException |
||
| 48 | */ |
||
| 49 | protected function processAll() : string |
||
| 53 | |||
| 54 | /** |
||
| 55 | * stats for TOP 10. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | * |
||
| 59 | * @throws \Doctrine\DBAL\DBALException |
||
| 60 | */ |
||
| 61 | protected function processTop10() : string |
||
| 71 | |||
| 72 | /** |
||
| 73 | * stats for beer counter. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | * |
||
| 77 | * @throws \Doctrine\DBAL\DBALException |
||
| 78 | */ |
||
| 79 | protected function processStats() : string |
||
| 91 | |||
| 92 | /** |
||
| 93 | * give someone a beer. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | * |
||
| 97 | * @throws \Doctrine\DBAL\DBALException |
||
| 98 | */ |
||
| 99 | protected function processFor() : string |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $username |
||
| 141 | * |
||
| 142 | * @return int |
||
| 143 | * |
||
| 144 | * @throws \Doctrine\DBAL\DBALException |
||
| 145 | */ |
||
| 146 | protected function getBeerCountByUsername($username) : int |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return int |
||
| 160 | * |
||
| 161 | * @throws \Doctrine\DBAL\DBALException |
||
| 162 | */ |
||
| 163 | protected function getBeerCountAll() : int |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return array |
||
| 175 | * |
||
| 176 | * @throws \Doctrine\DBAL\DBALException |
||
| 177 | */ |
||
| 178 | protected function getBeerTop10() : array |
||
| 190 | } |
||
| 191 |
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.