Completed
Push — v2 ( f8a429...e6c7b3 )
by Beñat
03:14
created

UserApi   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 27.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 21
c 2
b 1
f 1
lcom 1
cbo 3
dl 42
loc 152
rs 10

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
 * Copyright (c) 2014-2016 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
namespace BenatEspina\StackExchangeApiClient\Api;
13
14
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
15
use BenatEspina\StackExchangeApiClient\Http\Http;
16
use BenatEspina\StackExchangeApiClient\Model\User;
17
use BenatEspina\StackExchangeApiClient\Serializer\UserSerializer;
18
19
/**
20
 * The user api class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
final class UserApi
25
{
26
    const URL = 'users/';
27
    const QUERY_PARAMS = [
28
        'order'  => 'desc',
29
        'sort'   => 'reputation',
30
        'site'   => 'stackoverflow',
31
        'filter' => Http::FILTER_ALL,
32
    ];
33
34
    /**
35
     * The authentication.
36
     *
37
     * @var Authentication|null
38
     */
39
    private $authentication;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Authentication|null $anAuthentication The authentication
45
     */
46
    public function __construct(Authentication $anAuthentication = null)
47
    {
48
        $this->authentication = $anAuthentication;
49
    }
50
51
    /**
52
     * Returns all users on a site.
53
     *
54
     * More info: https://api.stackexchange.com/docs/users
55
     *
56
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
57
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
58
     *
59
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
60
     */
61 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...
62
    {
63
        if ($this->authentication instanceof Authentication) {
64
            if (true === empty($params)) {
65
                $params = array_merge($params, self::QUERY_PARAMS);
66
            }
67
            $params = array_merge($params, $this->authentication->toArray());
68
        }
69
        $response = Http::instance()->get(
70
            self::URL, $params
71
        );
72
73
        return $serialize === true ? UserSerializer::serialize($response) : $response;
74
    }
75
76
    /**
77
     * Gets the users identified in ids in {ids}.
78
     *
79
     * More info: https://api.stackexchange.com/docs/users-by-ids
80
     *
81
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
82
     * @param array        $params    QueryString parameter(s)
83
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
84
     *
85
     * @return array|User
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
86
     */
87
    public function getOfIds($ids, array $params = [], $serialize = true)
88
    {
89
        if ($this->authentication instanceof Authentication) {
90
            if (true === empty($params)) {
91
                $params = array_merge($params, self::QUERY_PARAMS);
92
            }
93
            $params = array_merge($params, $this->authentication->toArray());
94
        }
95
        $response = Http::instance()->get(
96
            self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params
97
        );
98
99
        return $serialize === true ? UserSerializer::serialize($response) : $response;
100
    }
101
102
    /**
103
     * Returns the user associated with the passed access_token.
104
     *
105
     * More info: https://api.stackexchange.com/docs/me
106
     *
107
     * @param array $params    QueryString parameter(s)
108
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
109
     *
110
     * @throws \Exception when the auth is null
111
     *
112
     * @return User
113
     */
114
    public function me(array $params = self::QUERY_PARAMS, $serialize = true)
115
    {
116
        if (!$this->authentication instanceof Authentication) {
117
            throw new \Exception('Authentication is required');
118
        }
119
        $response = Http::instance()->get(
120
            'me', array_merge($params, $this->authentication->toArray())
121
        );
122
123
        return $serialize === true ? UserSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 123 which is incompatible with the return type documented by BenatEspina\StackExchangeApiClient\Api\UserApi::me of type BenatEspina\StackExchangeApiClient\Model\User.
Loading history...
124
    }
125
126
    /**
127
     * Gets those users on a site who can exercise moderation powers.
128
     *
129
     * More info: https://api.stackexchange.com/docs/moderators
130
     *
131
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
132
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
133
     *
134
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
135
     */
136 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...
137
    {
138
        if ($this->authentication instanceof Authentication) {
139
            if (true === empty($params)) {
140
                $params = array_merge($params, self::QUERY_PARAMS);
141
            }
142
            $params = array_merge($params, $this->authentication->toArray());
143
        }
144
        $response = Http::instance()->get(
145
            self::URL . 'moderators', $params
146
        );
147
148
        return $serialize === true ? UserSerializer::serialize($response) : $response;
149
    }
150
151
    /**
152
     * Returns those users on a site who both have moderator powers, and were actually elected.
153
     *
154
     * More info: https://api.stackexchange.com/docs/elected-moderators
155
     *
156
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
157
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
158
     *
159
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
160
     */
161 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...
162
    {
163
        if ($this->authentication instanceof Authentication) {
164
            if (true === empty($params)) {
165
                $params = array_merge($params, self::QUERY_PARAMS);
166
            }
167
            $params = array_merge($params, $this->authentication->toArray());
168
        }
169
        $response = Http::instance()->get(
170
            self::URL . 'moderators/elected', $params
171
        );
172
173
        return $serialize === true ? UserSerializer::serialize($response) : $response;
174
    }
175
}
176