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 | 6 | protected function buildUrl($resource) |
|
80 | |||
81 | 3 | View Code Duplication | public function getBusinessUnits() |
93 | |||
94 | 3 | View Code Duplication | public function getJobs() |
106 | |||
107 | 9 | protected function login() |
|
122 | |||
123 | /** |
||
124 | * Returns the Client instance |
||
125 | * @return Client |
||
126 | */ |
||
127 | 9 | public function getClient() |
|
136 | |||
137 | /** |
||
138 | * Executes a request to the PeopleMatter API |
||
139 | * @param string $method The request type |
||
140 | * @param string $url The url to request |
||
141 | * @param array $options An array of options for the request |
||
142 | * @return array The response as an array |
||
143 | */ |
||
144 | 9 | protected function request($method, $url, $options = []) |
|
162 | } |
||
163 |
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: