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 |
||
8 | class RestApi extends AbstractApi |
||
9 | { |
||
10 | /** |
||
11 | * The API base url. |
||
12 | */ |
||
13 | const API_BASE_URL = 'https://www.ahgora.com.br/externo'; |
||
14 | |||
15 | /** |
||
16 | * The endpoint to get the punches. |
||
17 | */ |
||
18 | const ENDPOINT_APURACAO = '%s/getApuracao'; |
||
19 | |||
20 | /** |
||
21 | * The company id. |
||
22 | * string @var |
||
23 | */ |
||
24 | private $companyId; |
||
25 | |||
26 | /** |
||
27 | * The account username. |
||
28 | * string @var |
||
29 | */ |
||
30 | private $username; |
||
31 | |||
32 | /** |
||
33 | * The account password. |
||
34 | * @var |
||
35 | */ |
||
36 | private $password; |
||
37 | |||
38 | /** |
||
39 | * @var \Katapoka\Ahgora\Contracts\IHttpClient |
||
40 | */ |
||
41 | private $httpClient; |
||
42 | |||
43 | /** |
||
44 | * Api constructor. |
||
45 | * |
||
46 | * @param IHttpClient $httpClient |
||
47 | */ |
||
48 | public function __construct(IHttpClient $httpClient) |
||
52 | |||
53 | /** |
||
54 | * Set the company id of the ahgora system. |
||
55 | * |
||
56 | * @param string $companyId |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setCompanyId($companyId) |
||
66 | |||
67 | /** |
||
68 | * Set the username of the employee, from the company set at the setCompanyId. |
||
69 | * |
||
70 | * @param string $username |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function setUsername($username) |
||
80 | |||
81 | /** |
||
82 | * Set the password of the employee, from the company set at the setCompanyId. |
||
83 | * |
||
84 | * @param string $password |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function setPassword($password) |
||
94 | |||
95 | /** |
||
96 | * Try to execute the login on the page. |
||
97 | * To execute some actions the user needs to be loggedin. |
||
98 | * After a successful login, the status loggedin is saved as true. |
||
99 | * |
||
100 | * @return bool Returns true if the login was successful and false otherwise |
||
101 | */ |
||
102 | public function doLogin() |
||
120 | |||
121 | /** |
||
122 | * Get the punches at the given parameters. |
||
123 | * |
||
124 | * @param int|null $month The month you want to get the punches - Must be between 01 and 12 (both included) |
||
125 | * @param int|null $year The year you want to get the punches |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getPunches($month = null, $year = null) |
||
154 | |||
155 | /** |
||
156 | * Get the punches from some given day. |
||
157 | * |
||
158 | * @param int $day |
||
159 | * @param int $month |
||
160 | * @param int $year |
||
161 | * |
||
162 | * @return mixed |
||
163 | */ |
||
164 | View Code Duplication | public function getPunchesFromDay($day, $month, $year) |
|
183 | |||
184 | /** |
||
185 | * Gets the employee name. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getEmployeeName() |
||
193 | |||
194 | /** |
||
195 | * Gets the employer name. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getEmployeeRole() |
||
203 | |||
204 | /** |
||
205 | * Get the employer department. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getDepartment() |
||
213 | |||
214 | /** |
||
215 | * Gets the user data and punches. |
||
216 | * |
||
217 | * @param null $field |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | private function getEmployeeData($field = null) |
||
229 | |||
230 | /** |
||
231 | * Gets all the data and punches. Cached method. |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | private function getData() { |
||
252 | |||
253 | private function buildUrl($endpoint, array $params = []) { |
||
258 | |||
259 | private function parsePunches($dias) |
||
273 | |||
274 | private function parseExtra($dias) |
||
293 | |||
294 | } |
||
295 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: