GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( aaa608...0f5427 )
by Amr
01:42
created

Friendable   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 129
Duplicated Lines 15.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.57%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 18
c 5
b 1
f 2
lcom 1
cbo 1
dl 20
loc 129
ccs 69
cts 70
cp 0.9857
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteFriend() 0 12 2
B checkFriendship() 0 23 6
A addFriend() 0 14 2
A acceptFriend() 0 12 2
A friends_ids() 0 13 1
A friends() 0 8 1
A friendRequestsReceived() 10 10 1
A friendRequestsSent() 10 10 1
A isFriendsWith() 0 6 1
A mutualFriends() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Merodiro\Friendships;
4
5
trait Friendable
6
{
7 34
    public function checkFriendship($user)
8
    {
9 34
        if ($this->id == $user->id) {
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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_ids()
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()
87
    {
88 4
        $friendsIds = $this->friends_ids();
89
90 4
        return static::whereIn('id', $friendsIds)
91 4
            ->distinct()
92 4
            ->get();
93
    }
94
95 14 View Code Duplication
    public function friendRequestsReceived()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
96
    {
97 14
        $senders = Friendship::whereRecipient($this)
98 14
            ->accepted(0)
99 14
            ->get(['requester'])
100 14
            ->toArray();
101
102 14
        return static::whereIn('id', $senders)
103 14
            ->get();
104
    }
105
106 8 View Code Duplication
    public function friendRequestsSent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
107
    {
108 8
        $recipients = Friendship::whereSender($this)
109 8
            ->accepted(0)
110 8
            ->get(['user_requested'])
111 8
            ->toArray();
112
113 8
        return static::whereIn('id', $recipients)
114 8
            ->get();
115
    }
116
117 4
    public function isFriendsWith($user)
118
    {
119 4
        $friendshipStatus = $this->checkFriendship($user);
120
121 4
        return $friendshipStatus === 'friends';
122
    }
123
124 2
    public function mutualFriends($user)
125
    {
126 2
        $userFriends = $user->friends_ids();
127 2
        $friends = $this->friends_ids();
128
129 2
        $mutualIds = $userFriends->intersect($friends);
130
131 2
        return static::whereIn('id', $mutualIds)->get();
132
    }
133
}
134