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 |
||
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) |
||
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) |
|
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) |
||
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) |
||
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) |
|
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) |
|
177 | } |
||
178 |
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.