Completed
Push — master ( f5580e...4e6c29 )
by Beñat
01:39
created

UserApi   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 27.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 42
loc 152
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 14 14 4
B getOfIds() 0 14 5
A me() 0 11 3
A moderators() 14 14 4
A electedModerators() 14 14 4

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
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Api;
15
16
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
17
use BenatEspina\StackExchangeApiClient\Http\HttpClient;
18
use BenatEspina\StackExchangeApiClient\Model\User;
19
use BenatEspina\StackExchangeApiClient\Serializer\UserSerializer;
20
21
/**
22
 * The user api class.
23
 *
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
final class UserApi
27
{
28
    const URL = 'users/';
29
    const QUERY_PARAMS = [
30
        'order'  => 'desc',
31
        'sort'   => 'reputation',
32
        'site'   => 'stackoverflow',
33
        'filter' => HttpClient::FILTER_ALL,
34
    ];
35
36
    /**
37
     * The authentication.
38
     *
39
     * @var Authentication|null
40
     */
41
    private $authentication;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Authentication|null $anAuthentication The authentication
47
     */
48
    public function __construct(Authentication $anAuthentication = null)
49
    {
50
        $this->authentication = $anAuthentication;
51
    }
52
53
    /**
54
     * Returns all users on a site.
55
     *
56
     * More info: https://api.stackexchange.com/docs/users
57
     *
58
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
59
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
60
     *
61
     * @return array
62
     */
63 View Code Duplication
    public function all($params = [], $serialize = true)
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...
64
    {
65
        if ($this->authentication instanceof Authentication) {
66
            if (true === empty($params)) {
67
                $params = array_merge($params, self::QUERY_PARAMS);
68
            }
69
            $params = array_merge($params, $this->authentication->toArray());
70
        }
71
        $response = HttpClient::instance()->get(
0 ignored issues
show
Bug introduced by
The method instance() does not seem to exist on object<BenatEspina\Stack...Client\Http\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
            self::URL, $params
73
        );
74
75
        return $serialize === true ? UserSerializer::serialize($response) : $response;
76
    }
77
78
    /**
79
     * Gets the users identified in ids in {ids}.
80
     *
81
     * More info: https://api.stackexchange.com/docs/users-by-ids
82
     *
83
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
84
     * @param array        $params    QueryString parameter(s)
85
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
86
     *
87
     * @return array|User
88
     */
89
    public function getOfIds($ids, array $params = [], $serialize = true)
90
    {
91
        if ($this->authentication instanceof Authentication) {
92
            if (true === empty($params)) {
93
                $params = array_merge($params, self::QUERY_PARAMS);
94
            }
95
            $params = array_merge($params, $this->authentication->toArray());
96
        }
97
        $response = HttpClient::instance()->get(
0 ignored issues
show
Bug introduced by
The method instance() does not seem to exist on object<BenatEspina\Stack...Client\Http\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
            self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params
99
        );
100
101
        return $serialize === true ? UserSerializer::serialize($response) : $response;
102
    }
103
104
    /**
105
     * Returns the user associated with the passed access_token.
106
     *
107
     * More info: https://api.stackexchange.com/docs/me
108
     *
109
     * @param array $params    QueryString parameter(s)
110
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
111
     *
112
     * @throws \Exception when the auth is null
113
     *
114
     * @return User
115
     */
116
    public function me(array $params = self::QUERY_PARAMS, $serialize = true)
117
    {
118
        if (!$this->authentication instanceof Authentication) {
119
            throw new \Exception('Authentication is required');
120
        }
121
        $response = HttpClient::instance()->get(
0 ignored issues
show
Bug introduced by
The method instance() does not seem to exist on object<BenatEspina\Stack...Client\Http\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            'me', array_merge($params, $this->authentication->toArray())
123
        );
124
125
        return $serialize === true ? UserSerializer::serialize($response) : $response;
126
    }
127
128
    /**
129
     * Gets those users on a site who can exercise moderation powers.
130
     *
131
     * More info: https://api.stackexchange.com/docs/moderators
132
     *
133
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
134
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
135
     *
136
     * @return array
137
     */
138 View Code Duplication
    public function moderators(array $params = [], $serialize = true)
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...
139
    {
140
        if ($this->authentication instanceof Authentication) {
141
            if (true === empty($params)) {
142
                $params = array_merge($params, self::QUERY_PARAMS);
143
            }
144
            $params = array_merge($params, $this->authentication->toArray());
145
        }
146
        $response = HttpClient::instance()->get(
0 ignored issues
show
Bug introduced by
The method instance() does not seem to exist on object<BenatEspina\Stack...Client\Http\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
            self::URL . 'moderators', $params
148
        );
149
150
        return $serialize === true ? UserSerializer::serialize($response) : $response;
151
    }
152
153
    /**
154
     * Returns those users on a site who both have moderator powers, and were actually elected.
155
     *
156
     * More info: https://api.stackexchange.com/docs/elected-moderators
157
     *
158
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
159
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
160
     *
161
     * @return array
162
     */
163 View Code Duplication
    public function electedModerators(array $params = [], $serialize = true)
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...
164
    {
165
        if ($this->authentication instanceof Authentication) {
166
            if (true === empty($params)) {
167
                $params = array_merge($params, self::QUERY_PARAMS);
168
            }
169
            $params = array_merge($params, $this->authentication->toArray());
170
        }
171
        $response = HttpClient::instance()->get(
0 ignored issues
show
Bug introduced by
The method instance() does not seem to exist on object<BenatEspina\Stack...Client\Http\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
            self::URL . 'moderators/elected', $params
173
        );
174
175
        return $serialize === true ? UserSerializer::serialize($response) : $response;
176
    }
177
}
178