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 |
||
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) |
||
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 |
||
|
|||
60 | */ |
||
61 | View Code Duplication | public function all($params = [], $serialize = true) |
|
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 |
||
86 | */ |
||
87 | public function getOfIds($ids, array $params = [], $serialize = true) |
||
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) |
||
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 |
||
135 | */ |
||
136 | View Code Duplication | public function moderators(array $params = [], $serialize = true) |
|
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 |
||
160 | */ |
||
161 | View Code Duplication | public function electedModerators(array $params = [], $serialize = true) |
|
175 | } |
||
176 |
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[]
orarray<String>
.