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 MountRequest extends MountRequestBuilder { |
||
47 | |||
48 | |||
49 | use TStringTools; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param Mount $mount |
||
54 | */ |
||
55 | View Code Duplication | public function save(Mount $mount): void { |
|
69 | |||
70 | |||
71 | /** |
||
72 | * @param string $token |
||
73 | */ |
||
74 | public function delete(string $token): void { |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @param IFederatedUser $federatedUser |
||
84 | * |
||
85 | * @return Mount[] |
||
86 | * @throws RequestBuilderException |
||
87 | */ |
||
88 | public function getForUser(IFederatedUser $federatedUser): array { |
||
135 | // /** |
||
136 | // * @param string $userId |
||
137 | // * |
||
138 | // * @return Mount[] |
||
139 | // */ |
||
140 | // public function getForUser(string $userId): array { |
||
141 | // $qb = $this->getMountSelectSql(); |
||
142 | // |
||
143 | // $this-> |
||
144 | // $this->joinMembership($qb, $userId); |
||
145 | // $this->leftJoinMountPoint($qb, $userId); |
||
146 | // |
||
147 | // $shares = []; |
||
148 | // $cursor = $qb->execute(); |
||
149 | // while ($data = $cursor->fetch()) { |
||
150 | // $shares[] = $this->parseGSSharesSelectSql($data); |
||
151 | // } |
||
152 | // $cursor->closeCursor(); |
||
153 | // |
||
154 | // return $shares; |
||
155 | // } |
||
156 | |||
157 | // |
||
158 | // /** |
||
159 | // * @param DeprecatedMember $member |
||
160 | // */ |
||
161 | // public function removeGSSharesFromMember(DeprecatedMember $member) { |
||
162 | // $qb = $this->getMountDeleteSql(); |
||
163 | // $this->limitToCircleId($qb, $member->getCircleId()); |
||
164 | // $this->limitToInstance($qb, $member->getInstance()); |
||
165 | // $this->limitToOwner($qb, $member->getUserId()); |
||
166 | // |
||
167 | // $qb->execute(); |
||
168 | // } |
||
169 | // |
||
170 | // |
||
171 | // /** |
||
172 | // * @param IQueryBuilder $qb |
||
173 | // * @param string $userId |
||
174 | // */ |
||
175 | // private function joinMembership(IQueryBuilder $qb, string $userId) { |
||
176 | // $qb->from(DeprecatedRequestBuilder::TABLE_MEMBERS, 'm'); |
||
177 | // |
||
178 | // $expr = $qb->expr(); |
||
179 | // $andX = $expr->andX(); |
||
180 | // |
||
181 | // $andX->add($expr->eq('m.user_id', $qb->createNamedParameter($userId))); |
||
182 | // $andX->add($expr->eq('m.instance', $qb->createNamedParameter(''))); |
||
183 | // $andX->add($expr->gt('m.level', $qb->createNamedParameter(0))); |
||
184 | // $andX->add($expr->eq('m.user_type', $qb->createNamedParameter(DeprecatedMember::TYPE_USER))); |
||
185 | // $andX->add($expr->eq('m.circle_id', 'gsh.circle_id')); |
||
186 | // |
||
187 | // $qb->andWhere($andX); |
||
188 | // } |
||
189 | // |
||
190 | // |
||
191 | // private function leftJoinMountPoint(IQueryBuilder $qb, string $userId) { |
||
192 | // $expr = $qb->expr(); |
||
193 | // $pf = '' . $this->default_select_alias . '.'; |
||
194 | // |
||
195 | // $on = $expr->andX(); |
||
196 | // $on->add($expr->eq('mp.user_id', $qb->createNamedParameter($userId))); |
||
197 | // $on->add($expr->eq('mp.share_id', $pf . 'id')); |
||
198 | // |
||
199 | // /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
200 | // $qb->selectAlias('mp.mountPoint', 'gsshares_mountpoint') |
||
201 | // ->leftJoin($this->default_select_alias, DeprecatedRequestBuilder::TABLE_GSSHARES_MOUNTPOINT, 'mp', $on); |
||
202 | // } |
||
203 | // |
||
204 | // |
||
205 | // /** |
||
206 | // * @param string $userId |
||
207 | // * @param string $target |
||
208 | // * |
||
209 | // * @return GSShareMountpoint |
||
210 | // * @throws ShareNotFound |
||
211 | // */ |
||
212 | // public function getShareMountPointByPath(string $userId, string $target): GSShareMountpoint { |
||
213 | // $qb = $this->getMountMountpointSelectSql(); |
||
214 | // |
||
215 | // $targetHash = md5($target); |
||
216 | // $this->limitToUserId($qb, $userId); |
||
217 | // $this->limitToMountpointHash($qb, $targetHash); |
||
218 | // |
||
219 | // $shares = []; |
||
220 | // $cursor = $qb->execute(); |
||
221 | // $data = $cursor->fetch(); |
||
222 | // |
||
223 | // if ($data === false) { |
||
224 | // throw new ShareNotFound(); |
||
225 | // } |
||
226 | // |
||
227 | // return $this->parseGSSharesMountpointSelectSql($data); |
||
228 | // } |
||
229 | // |
||
230 | // |
||
231 | // /** |
||
232 | // * @param int $gsShareId |
||
233 | // * @param string $userId |
||
234 | // * |
||
235 | // * @return GSShareMountpoint |
||
236 | // * @throws ShareNotFound |
||
237 | // */ |
||
238 | // public function getShareMountPointById(int $gsShareId, string $userId): GSShareMountpoint { |
||
239 | // $qb = $this->getMountMountpointSelectSql(); |
||
240 | // |
||
241 | // $this->limitToShareId($qb, $gsShareId); |
||
242 | // $this->limitToUserId($qb, $userId); |
||
243 | // |
||
244 | // $shares = []; |
||
245 | // $cursor = $qb->execute(); |
||
246 | // $data = $cursor->fetch(); |
||
247 | // if ($data === false) { |
||
248 | // throw new ShareNotFound(); |
||
249 | // } |
||
250 | // |
||
251 | // return $this->parseGSSharesMountpointSelectSql($data); |
||
252 | // } |
||
253 | // |
||
254 | // |
||
255 | // /** |
||
256 | // * @param GSShareMountpoint $mountpoint |
||
257 | // */ |
||
258 | // public function generateShareMountPoint(GSShareMountpoint $mountpoint) { |
||
259 | // $qb = $this->getMountMountpointInsertSql(); |
||
260 | // |
||
261 | // $hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint()); |
||
262 | // |
||
263 | // $qb->setValue('user_id', $qb->createNamedParameter($mountpoint->getUserId())) |
||
264 | // ->setValue('share_id', $qb->createNamedParameter($mountpoint->getShareId())) |
||
265 | // ->setValue('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint())) |
||
266 | // ->setValue('mountpoint_hash', $qb->createNamedParameter($hash)); |
||
267 | // $qb->execute(); |
||
268 | // } |
||
269 | // |
||
270 | // |
||
271 | // /** |
||
272 | // * @param GSShareMountpoint $mountpoint |
||
273 | // * |
||
274 | // * @return bool |
||
275 | // */ |
||
276 | // public function updateShareMountPoint(GSShareMountpoint $mountpoint) { |
||
277 | // $qb = $this->getMountMountpointUpdateSql(); |
||
278 | // |
||
279 | // $hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint()); |
||
280 | // |
||
281 | // $qb->set('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint())) |
||
282 | // ->set('mountpoint_hash', $qb->createNamedParameter($hash)); |
||
283 | // |
||
284 | // $this->limitToShareId($qb, $mountpoint->getShareId()); |
||
285 | // $this->limitToUserId($qb, $mountpoint->getUserId()); |
||
286 | // $nb = $qb->execute(); |
||
287 | // |
||
288 | // return ($nb === 1); |
||
289 | // } |
||
290 | // |
||
291 | |||
292 | |||
293 | } |
||
294 | |||
295 |
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.