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 |
||
46 | class FederatedService { |
||
47 | |||
48 | |||
49 | /** @var string */ |
||
50 | private $userId; |
||
51 | |||
52 | /** @var IL10N */ |
||
53 | private $l10n; |
||
54 | |||
55 | /** @var ConfigService */ |
||
56 | private $configService; |
||
57 | |||
58 | /** @var CirclesService */ |
||
59 | private $circlesService; |
||
60 | |||
61 | /** @var FederatedLinksRequest */ |
||
62 | private $federatedLinksRequest; |
||
63 | |||
64 | /** @var CirclesMapper */ |
||
65 | private $dbCircles; |
||
66 | |||
67 | /** @var MembersMapper */ |
||
68 | private $dbMembers; |
||
69 | |||
70 | /** @var ClientService */ |
||
71 | private $clientService; |
||
72 | |||
73 | /** @var MiscService */ |
||
74 | private $miscService; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * CirclesService constructor. |
||
79 | * |
||
80 | * @param $userId |
||
81 | * @param IL10N $l10n |
||
82 | * @param ConfigService $configService |
||
83 | * @param DatabaseService $databaseService |
||
84 | * @param CirclesService $circlesService |
||
85 | * @param FederatedLinksRequest $federatedLinksRequest |
||
86 | * @param string $serverHost |
||
87 | * @param ClientService $clientService |
||
88 | * @param MiscService $miscService |
||
89 | */ |
||
90 | public function __construct( |
||
114 | |||
115 | |||
116 | /** |
||
117 | * linkCircle() |
||
118 | * |
||
119 | * link to a circle. |
||
120 | * Function will check if settings allow Federated links between circles, and the format of |
||
121 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
122 | * using requestLinkWithCircle() |
||
123 | * |
||
124 | * $remote format: <circle_name>@<remote_host> |
||
125 | * |
||
126 | * @param int $circleId |
||
127 | * @param string $remote |
||
128 | * |
||
129 | * @throws Exception |
||
130 | * @throws FederatedCircleLinkFormatException |
||
131 | * @throws CircleTypeNotValid |
||
132 | * @throws MemberIsNotAdminException |
||
133 | * |
||
134 | * @return FederatedLink |
||
135 | */ |
||
136 | public function linkCircle($circleId, $remote) { |
||
156 | |||
157 | |||
158 | /** |
||
159 | * requestLinkWithCircle() |
||
160 | * |
||
161 | * Using CircleId, function will get more infos from the database. |
||
162 | * Will check if author is not admin and initiate a FederatedLink, save it |
||
163 | * in the database and send a request to the remote circle using requestLink() |
||
164 | * If any issue, entry is removed from the database. |
||
165 | * |
||
166 | * @param $circleId |
||
167 | * @param $remote |
||
168 | * |
||
169 | * @return FederatedLink |
||
170 | * @throws Exception |
||
171 | */ |
||
172 | private function requestLinkWithCircle($circleId, $remote) { |
||
201 | |||
202 | |||
203 | /** |
||
204 | * @param string $remote |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
215 | |||
216 | /** |
||
217 | * @param string $remote |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
228 | |||
229 | |||
230 | /** |
||
231 | * requestLink() |
||
232 | * |
||
233 | * |
||
234 | * @param Circle $circle |
||
235 | * @param FederatedLink $link |
||
236 | * |
||
237 | * @return int |
||
238 | * @throws Exception |
||
239 | */ |
||
240 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
276 | |||
277 | |||
278 | private function parsingRequestLinkResult($result) { |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Create a new link into database and assign the correct status. |
||
310 | * |
||
311 | * @param Circle $circle |
||
312 | * @param FederatedLink $link |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
328 | |||
329 | |||
330 | /** |
||
331 | * @param $circleId |
||
332 | * @param $uniqueId |
||
333 | * |
||
334 | * @return FederatedLink |
||
335 | */ |
||
336 | public function getLink($circleId, $uniqueId) { |
||
339 | |||
340 | |||
341 | public function initiateRemoteShare($token) { |
||
366 | |||
367 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: