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 |
||
74 | class MemberService { |
||
75 | |||
76 | |||
77 | use TArrayTools; |
||
78 | use TStringTools; |
||
79 | use TNC22Logger; |
||
80 | |||
81 | |||
82 | /** @var CircleRequest */ |
||
83 | private $circleRequest; |
||
84 | |||
85 | /** @var MemberRequest */ |
||
86 | private $memberRequest; |
||
87 | |||
88 | /** @var FederatedUserService */ |
||
89 | private $federatedUserService; |
||
90 | |||
91 | /** @var MembershipService */ |
||
92 | private $membershipService; |
||
93 | |||
94 | /** @var FederatedEventService */ |
||
95 | private $federatedEventService; |
||
96 | |||
97 | /** @var RemoteStreamService */ |
||
98 | private $remoteStreamService; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * MemberService constructor. |
||
103 | * |
||
104 | * @param CircleRequest $circleRequest |
||
105 | * @param MemberRequest $memberRequest |
||
106 | * @param FederatedUserService $federatedUserService |
||
107 | * @param FederatedEventService $federatedEventService |
||
108 | * @param RemoteStreamService $remoteStreamService |
||
109 | */ |
||
110 | View Code Duplication | public function __construct( |
|
125 | |||
126 | // |
||
127 | // /** |
||
128 | // * @param Member $member |
||
129 | // * |
||
130 | // * @throws MemberAlreadyExistsException |
||
131 | // */ |
||
132 | // public function saveMember(Member $member) { |
||
133 | // $member->setId($this->token(Member::ID_LENGTH)); |
||
134 | // $this->memberRequest->save($member); |
||
135 | // } |
||
136 | // |
||
137 | |||
138 | |||
139 | /** |
||
140 | * @param string $memberId |
||
141 | * @param string $circleId |
||
142 | * @param bool $canBeVisitor |
||
143 | * |
||
144 | * @return Member |
||
145 | * @throws InitiatorNotFoundException |
||
146 | * @throws MemberNotFoundException |
||
147 | * @throws RequestBuilderException |
||
148 | */ |
||
149 | public function getMemberById( |
||
168 | |||
169 | |||
170 | /** |
||
171 | * @param string $circleId |
||
172 | * |
||
173 | * @return Member[] |
||
174 | * @throws InitiatorNotFoundException |
||
175 | * @throws RequestBuilderException |
||
176 | */ |
||
177 | public function getMembers(string $circleId): array { |
||
186 | |||
187 | |||
188 | /** |
||
189 | * @param string $circleId |
||
190 | * @param FederatedUser $federatedUser |
||
191 | * |
||
192 | * @return array |
||
193 | * @throws CircleNotFoundException |
||
194 | * @throws FederatedEventException |
||
195 | * @throws FederatedItemException |
||
196 | * @throws InitiatorNotConfirmedException |
||
197 | * @throws InitiatorNotFoundException |
||
198 | * @throws OwnerNotFoundException |
||
199 | * @throws RemoteInstanceException |
||
200 | * @throws RemoteNotFoundException |
||
201 | * @throws RemoteResourceNotFoundException |
||
202 | * @throws RequestBuilderException |
||
203 | * @throws UnknownRemoteException |
||
204 | * @throws ContactAddressBookNotFoundException |
||
205 | * @throws ContactFormatException |
||
206 | * @throws ContactNotFoundException |
||
207 | * @throws FederatedUserException |
||
208 | * @throws InvalidIdException |
||
209 | * @throws SingleCircleNotFoundException |
||
210 | */ |
||
211 | public function addMember(string $circleId, FederatedUser $federatedUser): array { |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @param string $circleId |
||
232 | * @param IFederatedUser[] $members |
||
233 | * |
||
234 | * @return FederatedUser[] |
||
235 | * @throws CircleNotFoundException |
||
236 | * @throws FederatedEventException |
||
237 | * @throws FederatedItemException |
||
238 | * @throws InitiatorNotConfirmedException |
||
239 | * @throws InitiatorNotFoundException |
||
240 | * @throws OwnerNotFoundException |
||
241 | * @throws RemoteNotFoundException |
||
242 | * @throws RemoteResourceNotFoundException |
||
243 | * @throws UnknownRemoteException |
||
244 | * @throws RemoteInstanceException |
||
245 | * @throws RequestBuilderException |
||
246 | */ |
||
247 | public function addMembers(string $circleId, array $federatedUsers): array { |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @param string $memberId |
||
280 | * |
||
281 | * @return array |
||
282 | * @throws FederatedEventException |
||
283 | * @throws FederatedItemException |
||
284 | * @throws InitiatorNotConfirmedException |
||
285 | * @throws InitiatorNotFoundException |
||
286 | * @throws MemberNotFoundException |
||
287 | * @throws OwnerNotFoundException |
||
288 | * @throws RemoteNotFoundException |
||
289 | * @throws RemoteResourceNotFoundException |
||
290 | * @throws UnknownRemoteException |
||
291 | * @throws RequestBuilderException |
||
292 | */ |
||
293 | public function removeMember(string $memberId): array { |
||
306 | |||
307 | /** |
||
308 | * @param string $memberId |
||
309 | * @param int $level |
||
310 | * |
||
311 | * @return array |
||
312 | * @throws FederatedEventException |
||
313 | * @throws InitiatorNotConfirmedException |
||
314 | * @throws InitiatorNotFoundException |
||
315 | * @throws MemberNotFoundException |
||
316 | * @throws OwnerNotFoundException |
||
317 | * @throws RemoteNotFoundException |
||
318 | * @throws RemoteResourceNotFoundException |
||
319 | * @throws UnknownRemoteException |
||
320 | * @throws FederatedItemException |
||
321 | * @throws RequestBuilderException |
||
322 | */ |
||
323 | public function memberLevel(string $memberId, int $level): array { |
||
337 | |||
338 | |||
339 | /** |
||
340 | * @param Member $member |
||
341 | * |
||
342 | * @return bool |
||
343 | * @throws InvalidIdException |
||
344 | * @throws RemoteNotFoundException |
||
345 | * @throws RequestBuilderException |
||
346 | * @throws UnknownRemoteException |
||
347 | */ |
||
348 | public function insertOrUpdate(Member $member): bool { |
||
364 | |||
365 | } |
||
366 | |||
367 |
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.