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 |
||
| 9 | class PeopleMatter |
||
| 10 | { |
||
| 11 | const V3 = 'v3'; |
||
| 12 | |||
| 13 | protected $host; |
||
| 14 | protected $version; |
||
| 15 | protected $client; |
||
| 16 | protected $token; |
||
| 17 | protected $authenticated = false; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Creates a PeopleMatter instance that can register and unregister webhooks with the API |
||
| 21 | * @param string $username The Username |
||
| 22 | * @param string $password The Password |
||
| 23 | * @param string $alias The business alias |
||
| 24 | * @param string $host The host to connect to |
||
| 25 | * @param Client|null $client The Guzzle client (used for testing) |
||
| 26 | */ |
||
| 27 | 12 | public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null) |
|
| 35 | |||
| 36 | 3 | public function hire(Person $person, Job $job, BusinessUnit $businessUnit, DateTime $hired_at = null, $timeStatus = "FullTime") |
|
| 75 | |||
| 76 | 3 | View Code Duplication | public function getBusinessUnits() |
| 89 | |||
| 90 | // public function getPerson($email) |
||
| 91 | // { |
||
| 92 | // if (empty($email)) { |
||
| 93 | // throw new \Exception("Email is invalid!"); |
||
| 94 | // } |
||
| 95 | // $this->login(); |
||
| 96 | // $units = []; |
||
| 97 | // $url = "https://{$this->host}/api/employees/list"; |
||
| 98 | // $response = $this->request('GET', $url); |
||
| 99 | // var_dump($response); |
||
| 100 | // foreach ($response["Records"] as $unit) { |
||
| 101 | // $units[] = new BusinessUnit($unit); |
||
| 102 | // } |
||
| 103 | |||
| 104 | // return $units; |
||
| 105 | // } |
||
| 106 | |||
| 107 | 3 | View Code Duplication | public function getJobs() |
| 120 | |||
| 121 | 9 | protected function login() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the Client instance |
||
| 139 | * @return Client |
||
| 140 | */ |
||
| 141 | 9 | public function getClient() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Executes a request to the PeopleMatter API |
||
| 153 | * @param string $method The request type |
||
| 154 | * @param string $url The url to request |
||
| 155 | * @param array $options An array of options for the request |
||
| 156 | * @return array The response as an array |
||
| 157 | */ |
||
| 158 | 9 | protected function request($method, $url, $options = []) |
|
| 176 | } |
||
| 177 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: