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 ( 713883...b30a3a )
by Amr
01:37
created

Friendable   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 18
c 7
b 1
f 3
lcom 1
cbo 1
dl 0
loc 126
ccs 65
cts 65
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteFriend() 0 12 2
B checkFriendship() 0 18 5
A addFriend() 0 14 2
A acceptFriend() 0 17 2
A friendsIds() 0 14 1
A friends() 0 6 1
A friendRequestsReceived() 0 5 1
A friendRequestsSent() 0 5 1
A isFriendsWith() 0 6 1
A mutualFriendsCount() 0 7 1
A mutualFriends() 0 9 1
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
15 32
        if ($friendship->count() == 0) {
16 32
            return 'not_friends';
17 30
        } elseif ($friendship->count() == 2) {
18 8
            return 'friends';
19 30
        } elseif ($friendship->first()->user_id == $this->id) {
20 10
            return 'waiting';
21
        } else {
22 28
            return 'pending';
23
        }
24
    }
25
26 34
    public function addFriend($recipient)
27
    {
28 34
        $friendshipStatus = $this->checkFriendship($recipient);
29
30 34
        if ($friendshipStatus == 'not_friends') {
31 32
            Friendship::create([
32 32
                'user_id' => $this->id,
33 32
                'friend_id' => $recipient->id,
34
            ]);
35 32
            event('friendrequest.sent', [$this, $recipient]);
36
        }
37
38 34
        return $friendshipStatus == 'waiting';
39
    }
40
41 22
    public function acceptFriend($sender)
42
    {
43 22
        $friendshipStatus = $this->checkFriendship($sender);
44
45 22
        if ($friendshipStatus == 'pending') {
46 20
            Friendship::create([
47 20
                'user_id' => $this->id,
48 20
                'friend_id' => $sender->id,
49 20
                'status' => 1
50
            ]);
51 20
            Friendship::betweenUsers($this, $sender)
52 20
                ->update(['status' => 1]);
53 20
            event('friendrequest.accepted', [$this, $sender]);
54
        }
55
56 22
        return $friendshipStatus == 'friends';
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 2
    public function friendsIds()
73
    {
74
        $friendsIds = Friendship::where(function ($query) {
75 2
            $query->whereSender($this);
76
        })->orWhere(function ($query) {
77 2
            $query->whereRecipient($this);
78 2
        })->accepted(1)->get(['friend_id', 'user_id'])->toArray();
79
80
        $friendsIds = collect($friendsIds)->flatten()->unique()->reject(function ($id) {
81 2
            return $id == $this->id;
82 2
        });
83
84 2
        return $friendsIds;
85
    }
86
87 4
    public function friends()
88
    {
89
90 4
        return $this->belongsToMany('User', 'friendships', 'user_id', 'friend_id')
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
91 4
            ->where('status', 1);
92
    }
93
94 14
    public function friendRequestsReceived()
95
    {
96 14
        return $this->belongsToMany('User', 'friendships', 'friend_id', 'user_id')
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97 14
            ->where('status', 0);
98
    }
99
100 8
    public function friendRequestsSent()
101
    {
102 8
        return $this->belongsToMany('User', 'friendships', 'user_id', 'friend_id')
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103 8
            ->where('status', 0);
104
    }
105
106 4
    public function isFriendsWith($user)
107
    {
108 4
        $friendshipStatus = $this->checkFriendship($user);
109
110 4
        return $friendshipStatus === 'friends';
111
    }
112
113 2
    public function mutualFriendsCount($user)
114
    {
115 2
        $userFriends = $user->friendsIds();
116 2
        $friends = $this->friendsIds();
117
118 2
        return $userFriends->intersect($friends)->count();
119
    }
120
121 2
    public function mutualFriends($user)
122
    {
123 2
        $userFriends = $user->friendsIds();
124 2
        $friends = $this->friendsIds();
125
126 2
        $mutualIds = $userFriends->intersect($friends);
127
128 2
        return static::whereIn('id', $mutualIds)->get();
129
    }
130
}
131