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 |
||
49 | class RemoteRequest extends RemoteRequestBuilder { |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param RemoteInstance $remote |
||
54 | * |
||
55 | * @throws RemoteUidException |
||
56 | */ |
||
57 | public function save(RemoteInstance $remote): void { |
||
58 | $remote->mustBeIdentityAuthed(); |
||
59 | $qb = $this->getRemoteInsertSql(); |
||
60 | $qb->setValue('uid', $qb->createNamedParameter($remote->getUid(true))) |
||
61 | ->setValue('instance', $qb->createNamedParameter($remote->getInstance())) |
||
62 | ->setValue('href', $qb->createNamedParameter($remote->getId())) |
||
63 | ->setValue('type', $qb->createNamedParameter($remote->getType())) |
||
64 | ->setValue('interface', $qb->createNamedParameter($remote->getInterface())) |
||
65 | ->setValue('item', $qb->createNamedParameter(json_encode($remote->getOrigData()))); |
||
66 | |||
67 | $qb->execute(); |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param RemoteInstance $remote |
||
73 | * |
||
74 | * @throws RemoteUidException |
||
75 | */ |
||
76 | public function update(RemoteInstance $remote) { |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @param RemoteInstance $remote |
||
92 | * |
||
93 | * @throws RemoteUidException |
||
94 | */ |
||
95 | View Code Duplication | public function updateItem(RemoteInstance $remote) { |
|
104 | |||
105 | |||
106 | /** |
||
107 | * @param RemoteInstance $remote |
||
108 | * |
||
109 | * @throws RemoteUidException |
||
110 | */ |
||
111 | View Code Duplication | public function updateInstance(RemoteInstance $remote) { |
|
120 | |||
121 | |||
122 | /** |
||
123 | * @param RemoteInstance $remote |
||
124 | * |
||
125 | * @throws RemoteUidException |
||
126 | */ |
||
127 | View Code Duplication | public function updateType(RemoteInstance $remote) { |
|
136 | |||
137 | |||
138 | /** |
||
139 | * @param RemoteInstance $remote |
||
140 | * |
||
141 | * @throws RemoteUidException |
||
142 | */ |
||
143 | View Code Duplication | public function updateHref(RemoteInstance $remote) { |
|
152 | |||
153 | |||
154 | /** |
||
155 | * @return RemoteInstance[] |
||
156 | */ |
||
157 | public function getAllInstances(): array { |
||
162 | |||
163 | /** |
||
164 | * @return RemoteInstance[] |
||
165 | */ |
||
166 | public function getKnownInstances(): array { |
||
172 | |||
173 | |||
174 | /** |
||
175 | * - returns: |
||
176 | * - all GLOBAL_SCALE |
||
177 | * - TRUSTED if Circle is Federated |
||
178 | * - EXTERNAL if Circle is Federated and a contains a member from instance |
||
179 | * |
||
180 | * @param Circle $circle |
||
181 | * @param bool $broadcastAsFederated |
||
182 | * |
||
183 | * @return RemoteInstance[] |
||
184 | * @throws RequestBuilderException |
||
185 | */ |
||
186 | public function getOutgoingRecipient(Circle $circle, bool $broadcastAsFederated = false): array { |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @param string $host |
||
227 | * |
||
228 | * @return RemoteInstance |
||
229 | * @throws RemoteNotFoundException |
||
230 | */ |
||
231 | public function getFromInstance(string $host): RemoteInstance { |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param string $href |
||
241 | * |
||
242 | * @return RemoteInstance |
||
243 | * @throws RemoteNotFoundException |
||
244 | */ |
||
245 | public function getFromHref(string $href): RemoteInstance { |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @param string $status |
||
255 | * |
||
256 | * @return RemoteInstance[] |
||
257 | */ |
||
258 | public function getFromType(string $status): array { |
||
264 | |||
265 | |||
266 | /** |
||
267 | * @param RemoteInstance $remoteInstance |
||
268 | * |
||
269 | * @return RemoteInstance |
||
270 | * @throws RemoteNotFoundException |
||
271 | */ |
||
272 | public function searchDuplicate(RemoteInstance $remoteInstance) { |
||
282 | |||
283 | |||
284 | /** |
||
285 | * @param RemoteInstance $remoteInstance |
||
286 | */ |
||
287 | public function deleteById(RemoteInstance $remoteInstance) { |
||
292 | |||
293 | |||
294 | } |
||
295 | |||
296 |
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.