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.

Friendable   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 98.15%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 0
dl 0
loc 103
ccs 53
cts 54
cp 0.9815
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A checkFriendship() 0 18 5
A addFriend() 0 12 2
A acceptFriend() 0 13 2
A deleteFriend() 0 13 2
A friends() 0 5 1
A friendRequestsReceived() 0 5 1
A friendRequestsSent() 0 5 1
A isFriendsWith() 0 4 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);
0 ignored issues
show
Bug introduced by
The method betweenUsers() does not exist on Merodiro\Friendships\Friendship. Did you maybe mean scopeBetweenUsers()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
14
15 32
        if ($friendship->count() == 0) {
16 32
            return 'not_friends';
17 28
        } elseif ($friendship->count() == 2) {
18 8
            return 'friends';
19 28
        } elseif ($friendship->first()->user_id == $this->id) {
20 8
            return 'waiting';
21
        } else {
22 26
            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
            $this->friends()->attach($recipient);
32
33 32
            event('friendrequest.sent', [$this, $recipient]);
34
35 32
            return 'waiting';
36
        }
37 6
    }
38
39 22
    public function acceptFriend($sender)
40
    {
41 22
        $friendshipStatus = $this->checkFriendship($sender);
42
43 22
        if ($friendshipStatus == 'pending') {
44 20
            $this->friends()->attach($sender, ['status' => 1]);
45 20
            $sender->friends()->updateExistingPivot($this, ['status' => 1]);
46
47 20
            event('friendrequest.accepted', [$this, $sender]);
48
49 20
            return 'friends';
50
        }
51 4
    }
52
53 10
    public function deleteFriend($user)
54
    {
55 10
        $friendshipStatus = $this->checkFriendship($user);
56
57 10
        if ($friendshipStatus != 'not_friends') {
58 10
            $this->friends()->detach($user);
59 10
            $user->friends()->detach($this);
60
61 10
            event('friendship.deleted', [$this, $user]);
62
63 10
            return 'not_friends';
64
        }
65
    }
66
67 32
    public function friends()
68
    {
69 32
        return $this->belongsToMany(config('friendships.user_model'), '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...
70 32
            ->where('status', 1);
71
    }
72
73 14
    public function friendRequestsReceived()
74
    {
75 14
        return $this->belongsToMany(config('friendships.user_model'), '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...
76 14
            ->where('status', 0);
77
    }
78
79 8
    public function friendRequestsSent()
80
    {
81 8
        return $this->belongsToMany(config('friendships.user_model'), '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...
82 8
            ->where('status', 0);
83
    }
84
85 4
    public function isFriendsWith($user)
86
    {
87 4
        return $this->friends->contains($user);
0 ignored issues
show
Bug introduced by
The property friends 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...
88
    }
89
90 2
    public function mutualFriendsCount($user)
91
    {
92 2
        $userFriends = $user->friends->pluck('id');
93 2
        $friends = $this->friends->pluck('id');
94
95 2
        return $userFriends->intersect($friends)->count();
96
    }
97
98 2
    public function mutualFriends($user)
99
    {
100 2
        $userFriends = $user->friends->pluck('id');
101 2
        $friends = $this->friends->pluck('id');
102
103 2
        $mutualIds = $userFriends->intersect($friends);
104
105 2
        return static::whereIn('id', $mutualIds)->get();
106
    }
107
}
108