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); |
||
| 59 | abstract class AGlobalScaleEvent { |
||
| 60 | |||
| 61 | |||
| 62 | /** @var IRootFolder */ |
||
| 63 | protected $rootFolder; |
||
| 64 | |||
| 65 | /** @var IUserManager */ |
||
| 66 | protected $userManager; |
||
| 67 | |||
| 68 | /** @var SharesRequest */ |
||
| 69 | protected $sharesRequest; |
||
| 70 | |||
| 71 | /** @var TokensRequest */ |
||
| 72 | protected $tokensRequest; |
||
| 73 | |||
| 74 | /** @var CirclesRequest */ |
||
| 75 | protected $circlesRequest; |
||
| 76 | |||
| 77 | /** @var MembersRequest */ |
||
| 78 | protected $membersRequest; |
||
| 79 | |||
| 80 | /** @var GSSharesRequest */ |
||
| 81 | protected $gsSharesRequest; |
||
| 82 | |||
| 83 | /** @var CirclesService */ |
||
| 84 | protected $circlesService; |
||
| 85 | |||
| 86 | /** @var MembersService */ |
||
| 87 | protected $membersService; |
||
| 88 | |||
| 89 | /** @var EventsService */ |
||
| 90 | protected $eventsService; |
||
| 91 | |||
| 92 | /** @var ConfigService */ |
||
| 93 | protected $configService; |
||
| 94 | |||
| 95 | /** @var MiscService */ |
||
| 96 | protected $miscService; |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * AGlobalScaleEvent constructor. |
||
| 101 | * |
||
| 102 | * @param IRootFolder $rootFolder |
||
| 103 | * @param IUserManager $userManager |
||
| 104 | * @param SharesRequest $sharesRequest |
||
| 105 | * @param TokensRequest $tokensRequest |
||
| 106 | * @param CirclesRequest $circlesRequest |
||
| 107 | * @param MembersRequest $membersRequest |
||
| 108 | * @param GSSharesRequest $gsSharesRequest |
||
| 109 | * @param CirclesService $circlesService |
||
| 110 | * @param MembersService $membersService |
||
| 111 | * @param EventsService $eventsService |
||
| 112 | * @param ConfigService $configService |
||
| 113 | * @param MiscService $miscService |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function __construct( |
|
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * @param GSEvent $event |
||
| 146 | * @param bool $localCheck |
||
| 147 | * |
||
| 148 | * @param bool $mustBeCheck |
||
| 149 | * |
||
| 150 | * @throws CircleDoesNotExistException |
||
| 151 | * @throws ConfigNoCircleAvailableException |
||
| 152 | * @throws GlobalScaleDSyncException |
||
| 153 | * @throws GlobalScaleEventException |
||
| 154 | */ |
||
| 155 | public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void { |
||
| 156 | if ($localCheck && !$event->isForced()) { |
||
| 157 | $this->checkViewer($event, $mustBeCheck); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param GSEvent $event |
||
| 164 | */ |
||
| 165 | abstract public function manage(GSEvent $event): void; |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param GSEvent[] $events |
||
| 170 | */ |
||
| 171 | abstract public function result(array $events): void; |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * @param GSEvent $event |
||
| 176 | * @param bool $mustBeChecked |
||
| 177 | * |
||
| 178 | * @throws CircleDoesNotExistException |
||
| 179 | * @throws ConfigNoCircleAvailableException |
||
| 180 | * @throws GlobalScaleDSyncException |
||
| 181 | * @throws GlobalScaleEventException |
||
| 182 | */ |
||
| 183 | private function checkViewer(GSEvent $event, bool $mustBeChecked) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param Member $member1 |
||
| 212 | * @param Member $member2 |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | protected function compareMembers(Member $member1, Member $member2) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * @param Circle $circle1 |
||
| 241 | * @param Circle $circle2 |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | protected function compareCircles(Circle $circle1, Circle $circle2): bool { |
||
| 256 | |||
| 257 | |||
| 258 | protected function cleanMember(Member $member) { |
||
| 263 | |||
| 264 | } |
||
| 265 | |||
| 266 |
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.