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 |
||
| 5 | trait Friendable |
||
| 6 | { |
||
| 7 | 34 | public function checkFriendship($user) |
|
| 8 | { |
||
| 9 | 34 | if ($this->id == $user->id) { |
|
|
|
|||
| 10 | 2 | return 'same_user'; |
|
| 11 | } |
||
| 12 | |||
| 13 | 32 | $friendship = Friendship::betweenUsers($this, $user) |
|
| 14 | 32 | ->first(); |
|
| 15 | |||
| 16 | 32 | if (!$friendship) { |
|
| 17 | 32 | return 'not_friends'; |
|
| 18 | } |
||
| 19 | |||
| 20 | 30 | if ($friendship->status == 1) { |
|
| 21 | 8 | return 'friends'; |
|
| 22 | } |
||
| 23 | 30 | if ($friendship->requester == $this->id) { |
|
| 24 | 10 | return 'waiting'; |
|
| 25 | } |
||
| 26 | 28 | if ($friendship->user_requested == $this->id) { |
|
| 27 | 28 | return 'pending'; |
|
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | 34 | public function addFriend($recipient) |
|
| 32 | { |
||
| 33 | 34 | $friendshipStatus = $this->checkFriendship($recipient); |
|
| 34 | |||
| 35 | 34 | if ($friendshipStatus == 'not_friends') { |
|
| 36 | 32 | Friendship::create([ |
|
| 37 | 32 | 'requester' => $this->id, |
|
| 38 | 32 | 'user_requested' => $recipient->id, |
|
| 39 | ]); |
||
| 40 | 32 | event('friendrequest.sent', [$this, $recipient]); |
|
| 41 | } |
||
| 42 | |||
| 43 | 34 | return $friendshipStatus == 'not_friends'; |
|
| 44 | } |
||
| 45 | |||
| 46 | 22 | public function acceptFriend($sender) |
|
| 47 | { |
||
| 48 | 22 | $friendshipStatus = $this->checkFriendship($sender); |
|
| 49 | |||
| 50 | 22 | if ($friendshipStatus == 'pending') { |
|
| 51 | 20 | Friendship::betweenUsers($this, $sender) |
|
| 52 | 20 | ->update(['status' => 1]); |
|
| 53 | 20 | event('friendrequest.accepted', [$this, $sender]); |
|
| 54 | } |
||
| 55 | |||
| 56 | 22 | return $friendshipStatus == 'pending'; |
|
| 57 | } |
||
| 58 | |||
| 59 | 10 | public function deleteFriend($user) |
|
| 60 | { |
||
| 61 | 10 | $friendshipStatus = $this->checkFriendship($user); |
|
| 62 | |||
| 63 | 10 | if ($friendshipStatus != 'not_friends') { |
|
| 64 | 10 | Friendship::betweenUsers($this, $user) |
|
| 65 | 10 | ->delete(); |
|
| 66 | 10 | event('friendship.deleted', [$this, $user]); |
|
| 67 | } |
||
| 68 | |||
| 69 | 10 | return $friendshipStatus != 'not_friends'; |
|
| 70 | } |
||
| 71 | |||
| 72 | 6 | public function friends() |
|
| 73 | { |
||
| 74 | 6 | $recipients = Friendship::whereSender($this) |
|
| 75 | 6 | ->accepted(1) |
|
| 76 | 6 | ->get(['user_requested']) |
|
| 77 | 6 | ->toArray(); |
|
| 78 | |||
| 79 | 6 | $senders = Friendship::whereRecipient($this) |
|
| 80 | 6 | ->accepted(1) |
|
| 81 | 6 | ->get(['requester']) |
|
| 82 | 6 | ->toArray(); |
|
| 83 | |||
| 84 | 6 | $friendsIds = array_merge($recipients, $senders); |
|
| 85 | |||
| 86 | 6 | return static::whereIn('id', $friendsIds) |
|
| 87 | 6 | ->get(); |
|
| 88 | } |
||
| 89 | |||
| 90 | 14 | View Code Duplication | public function friendRequestsReceived() |
| 100 | |||
| 101 | 8 | View Code Duplication | public function friendRequestsSent() |
| 111 | |||
| 112 | 4 | public function isFriendsWith($user) |
|
| 118 | |||
| 119 | 2 | public function mutualFriends($user) |
|
| 120 | { |
||
| 121 | 2 | $userFriends = $user->friends(); |
|
| 122 | 2 | $friends = $this->friends(); |
|
| 123 | 2 | return $userFriends->intersect($friends); |
|
| 125 | } |
||
| 126 |
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: