Completed
Pull Request — master (#296)
by Sergey
02:05
created

BoardsInvites   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 38
loc 114
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A invites() 0 11 2
A sendInviteByEmail() 10 10 2
A sendInvite() 0 10 3
A sendInviteByUserId() 10 10 2
A deleteInvite() 0 11 1
A ignoreInvite() 9 9 1
A acceptInvite() 9 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 seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
7
trait BoardsInvites
8
{
9
    use HandlesRequest;
10
11
    /**
12
     * Get boards invites
13
     * @return array
14
     */
15
    public function invites()
16
    {
17
        $data = [
18
            'current_user' => true,
19
            'field_set_key' => 'news',
20
        ];
21
22
        $invites = $this->get($data, UrlBuilder::RESOURCE_BOARDS_INVITES);
23
24
        return !$invites ? [] : $invites;
25
    }
26
27
    /**
28
     * @param string $boardId
29
     * @param string|array $emails
30
     * @return bool
31
     */
32 View Code Duplication
    public function sendInviteByEmail($boardId, $emails)
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...
33
    {
34
        $emails = is_array($emails) ? $emails : [$emails];
35
        $data = [
36
            "board_id" => $boardId,
37
            "emails" => $emails,
38
        ];
39
40
        return $this->post($data, UrlBuilder::RESOURCE_CREATE_EMAIL_INVITE);
41
    }
42
43
    /**
44
     * @param string $boardId
45
     * @param string|array $users
46
     * @return bool
47
     */
48
    public function sendInvite($boardId, $users)
49
    {
50
        $users = is_array($users) ? $users : [$users];
51
52
        $isEmail = filter_var($users[0], FILTER_VALIDATE_EMAIL);
53
54
        return $isEmail ?
55
            $this->sendInviteByEmail($boardId, $users) :
56
            $this->sendInviteByUserId($boardId, $users);
57
    }
58
59
    /**
60
     * @param string $boardId
61
     * @param string|array $userIds
62
     * @return bool
63
     */
64 View Code Duplication
    public function sendInviteByUserId($boardId, $userIds)
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...
65
    {
66
        $userIds = is_array($userIds) ? $userIds : [$userIds];
67
        $data = [
68
            "board_id"         => $boardId,
69
            "invited_user_ids" => $userIds,
70
        ];
71
72
        return $this->post($data, UrlBuilder::RESOURCE_CREATE_USER_ID_INVITE);
73
    }
74
75
    /**
76
     * @param string $boardId
77
     * @param string $userId
78
     * @param bool $ban
79
     * @return bool
80
     */
81
    public function deleteInvite($boardId, $userId, $ban = false)
82
    {
83
        $data = [
84
            'ban'             => $ban,
85
            'board_id'        => $boardId,
86
            'field_set_key'   => 'boardEdit',
87
            'invited_user_id' => $userId,
88
        ];
89
90
        return $this->post($data, UrlBuilder::RESOURCE_DELETE_INVITE);
91
    }
92
93
    /**
94
     * @param string $boardId
95
     * @return bool
96
     */
97 View Code Duplication
    public function ignoreInvite($boardId)
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...
98
    {
99
        $data = [
100
            'board_id'        => $boardId,
101
            'invited_user_id' => $this->container->user->id(),
0 ignored issues
show
Bug introduced by
The property container 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...
102
        ];
103
104
        return $this->post($data, UrlBuilder::RESOURCE_DELETE_INVITE);
105
    }
106
107
    /**
108
     * @param string $boardId
109
     * @return bool
110
     */
111 View Code Duplication
    public function acceptInvite($boardId)
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...
112
    {
113
        $data = [
114
            'board_id'         => $boardId,
115
            'invited_user_id' => $this->container->user->id(),
116
        ];
117
118
        return $this->post($data, UrlBuilder::RESOURCE_ACCEPT_INVITE);
119
    }
120
}