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 declare(strict_types=1); |
||
| 55 | abstract class AGlobalScaleEvent { |
||
| 56 | |||
| 57 | |||
| 58 | /** @var SharesRequest */ |
||
| 59 | private $sharesRequest; |
||
| 60 | |||
| 61 | /** @var TokensRequest */ |
||
| 62 | private $tokensRequest; |
||
| 63 | |||
| 64 | /** @var CirclesRequest */ |
||
| 65 | protected $circlesRequest; |
||
| 66 | |||
| 67 | /** @var MembersRequest */ |
||
| 68 | protected $membersRequest; |
||
| 69 | |||
| 70 | /** @var CirclesService */ |
||
| 71 | protected $circlesService; |
||
| 72 | |||
| 73 | /** @var MembersService */ |
||
| 74 | protected $membersService; |
||
| 75 | |||
| 76 | /** @var EventsService */ |
||
| 77 | protected $eventsService; |
||
| 78 | |||
| 79 | /** @var ConfigService */ |
||
| 80 | protected $configService; |
||
| 81 | |||
| 82 | /** @var MiscService */ |
||
| 83 | protected $miscService; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * AGlobalScaleEvent constructor. |
||
| 88 | * |
||
| 89 | * @param SharesRequest $sharesRequest |
||
| 90 | * @param TokensRequest $tokensRequest |
||
| 91 | * @param CirclesRequest $circlesRequest |
||
| 92 | * @param MembersRequest $membersRequest |
||
| 93 | * @param CirclesService $circlesService |
||
| 94 | * @param MembersService $membersService |
||
| 95 | * @param EventsService $eventsService |
||
| 96 | * @param ConfigService $configService |
||
| 97 | * @param MiscService $miscService |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function __construct( |
|
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @param GSEvent $event |
||
| 124 | * @param bool $localCheck |
||
| 125 | * |
||
| 126 | * @param bool $mustBeCheck |
||
| 127 | * |
||
| 128 | * @throws CircleDoesNotExistException |
||
| 129 | * @throws ConfigNoCircleAvailableException |
||
| 130 | * @throws GlobalScaleDSyncException |
||
| 131 | * @throws GlobalScaleEventException |
||
| 132 | */ |
||
| 133 | public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void { |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @param GSEvent $event |
||
| 142 | */ |
||
| 143 | abstract public function manage(GSEvent $event): void; |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @param GSEvent $event |
||
| 148 | * @param bool $mustBeChecked |
||
| 149 | * |
||
| 150 | * @throws CircleDoesNotExistException |
||
| 151 | * @throws ConfigNoCircleAvailableException |
||
| 152 | * @throws GlobalScaleDSyncException |
||
| 153 | * @throws GlobalScaleEventException |
||
| 154 | */ |
||
| 155 | private function checkViewer(GSEvent $event, bool $mustBeChecked) { |
||
| 180 | |||
| 181 | |||
| 182 | private function compareMembers(Member $member1, Member $member2) { |
||
| 202 | |||
| 203 | |||
| 204 | protected function cleanMember(Member $member) { |
||
| 209 | |||
| 210 | } |
||
| 211 | |||
| 212 |
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.