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) |
|
| 71 | |||
| 72 | 6 | public function friendsIds() |
|
| 73 | { |
||
| 74 | $friendsIds = Friendship::where(function ($query) { |
||
| 75 | 6 | $query->whereSender($this); |
|
| 76 | })->orWhere(function ($query) { |
||
| 77 | 6 | $query->whereRecipient($this); |
|
| 78 | 6 | })->accepted(1)->get(['user_requested', 'requester'])->toArray(); |
|
| 79 | |||
| 80 | $friendsIds = collect($friendsIds)->flatten()->unique()->reject(function ($id) { |
||
| 81 | 6 | return $id == $this->id; |
|
| 82 | 6 | }); |
|
| 83 | 6 | return $friendsIds; |
|
| 84 | } |
||
| 85 | |||
| 86 | 4 | public function friends() |
|
| 94 | |||
| 95 | 14 | View Code Duplication | public function friendRequestsReceived() |
| 105 | |||
| 106 | 8 | View Code Duplication | public function friendRequestsSent() |
| 116 | |||
| 117 | 4 | public function isFriendsWith($user) |
|
| 123 | |||
| 124 | 2 | public function mutualFriendsCount($user) |
|
| 125 | { |
||
| 126 | 2 | $userFriends = $user->friendsIds(); |
|
| 127 | 2 | $friends = $this->friendsIds(); |
|
| 128 | |||
| 129 | 2 | return $userFriends->intersect($friends)->count(); |
|
| 130 | } |
||
| 131 | |||
| 132 | 2 | public function mutualFriends($user) |
|
| 141 | } |
||
| 142 |
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: