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 |
||
| 42 | class SharesRequest extends SharesRequestBuilder { |
||
| 43 | |||
| 44 | |||
| 45 | use TArrayTools; |
||
| 46 | use TStringTools; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * remove shares from a member to a circle |
||
| 51 | * |
||
| 52 | * @param Member $member |
||
| 53 | */ |
||
| 54 | public function removeSharesFromMember(Member $member) { |
||
| 55 | $qb = $this->getSharesDeleteSql(); |
||
| 56 | $expr = $qb->expr(); |
||
| 57 | |||
| 58 | $andX = $expr->andX(); |
||
| 59 | $andX->add($expr->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE))); |
||
| 60 | $andX->add($expr->eq('share_with', $qb->createNamedParameter($member->getCircleId()))); |
||
| 61 | $andX->add($expr->eq('uid_initiator', $qb->createNamedParameter($member->getUserId()))); |
||
| 62 | $qb->andWhere($andX); |
||
| 63 | |||
| 64 | $qb->execute(); |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @param int $shareId |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | * @throws TokenDoesNotExistException |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function getTokenByShareId(int $shareId) { |
|
|
|
|||
| 75 | $qb = $this->getSharesSelectSql(); |
||
| 76 | $this->limitToId($qb, $shareId); |
||
| 77 | $this->limitToShareType($qb, self::SHARE_TYPE); |
||
| 78 | |||
| 79 | $cursor = $qb->execute(); |
||
| 80 | $data = $cursor->fetch(); |
||
| 81 | $cursor->closeCursor(); |
||
| 82 | if ($data === false) { |
||
| 83 | throw new TokenDoesNotExistException('Unknown share token'); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this->get('token', $data, 'notfound'); |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $circleUniqueId |
||
| 92 | */ |
||
| 93 | public function shuffleTokensFromCircle(string $circleUniqueId) { |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param int $shareId |
||
| 103 | */ |
||
| 104 | public function shuffleTokenByShareId(int $shareId) { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $circleId |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function getSharesForCircle(string $circleId) { |
||
| 135 | |||
| 136 | |||
| 137 | } |
||
| 138 |
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.