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 |
||
| 34 | class SharingFrameRequest extends SharingFrameRequestBuilder { |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $circleUniqueId |
||
| 39 | * @param string $frameUniqueId |
||
| 40 | * |
||
| 41 | * @return SharingFrame |
||
| 42 | * @throws SharingFrameDoesNotExistException |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function getSharingFrame($circleUniqueId, $frameUniqueId) { |
|
|
|
|||
| 45 | $qb = $this->getSharesSelectSql(); |
||
| 46 | $this->limitToUniqueId($qb, $frameUniqueId); |
||
| 47 | $this->limitToCircleId($qb, $circleUniqueId); |
||
| 48 | $this->leftJoinCircle($qb); |
||
| 49 | |||
| 50 | $cursor = $qb->execute(); |
||
| 51 | $data = $cursor->fetch(); |
||
| 52 | $cursor->closeCursor(); |
||
| 53 | |||
| 54 | if ($data === false) { |
||
| 55 | throw new SharingFrameDoesNotExistException($this->l10n->t('Sharing Frame does not exist')); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $this->parseSharesSelectSql($data); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $circleUniqueId |
||
| 64 | * |
||
| 65 | * @return SharingFrame[] |
||
| 66 | */ |
||
| 67 | public function getSharingFramesFromCircle($circleUniqueId) { |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * saveFrame() |
||
| 84 | * |
||
| 85 | * Insert a new entry in the database to save the SharingFrame. |
||
| 86 | * |
||
| 87 | * @param SharingFrame $frame |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function saveSharingFrame(SharingFrame $frame) { |
|
| 103 | |||
| 104 | |||
| 105 | View Code Duplication | public function updateSharingFrame(SharingFrame $frame) { |
|
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | public function updatePasswordOnShares(string $circleId, string $password) { |
||
| 129 | |||
| 130 | |||
| 131 | } |
||
| 132 |
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.