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) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the punches from some given day. |
||
| 154 | * |
||
| 155 | * @param int $day |
||
| 156 | * @param int $month |
||
| 157 | * @param int $year |
||
| 158 | * |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function getPunchesFromDay($day, $month, $year) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the employee name. |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getEmployeeName() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the employer name. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getEmployeeRole() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the employer department. |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getDepartment() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the user data and punches. |
||
| 213 | * |
||
| 214 | * @param null $field |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | private function getEmployeeData($field = null) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets all the data and punches. Cached method. |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | private function getData() { |
||
| 249 | |||
| 250 | private function buildUrl($endpoint, array $params = []) { |
||
| 255 | |||
| 256 | private function parsePunches($dias) |
||
| 270 | } |
||
| 271 |
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: