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 |
||
14 | class PeopleMatter |
||
15 | { |
||
16 | protected $alias; |
||
17 | protected $authenticated = false; |
||
18 | protected $client; |
||
19 | protected $host; |
||
20 | protected $password; |
||
21 | protected $username; |
||
22 | |||
23 | /** |
||
24 | * Creates a PeopleMatter instance that can register and unregister webhooks with the API |
||
25 | * @param string $username The Username |
||
26 | * @param string $password The Password |
||
27 | * @param string $alias The business alias |
||
28 | * @param string $host The host to connect to |
||
29 | * @param Client|null $client The Guzzle client (used for testing) |
||
30 | */ |
||
31 | 15 | public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null) |
|
39 | |||
40 | 3 | public function hire(Person $person, Job $job, BusinessUnit $businessUnit, $timeStatus, DateTime $hired_at = null) |
|
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | 9 | protected function buildUrl($resource) |
|
90 | |||
91 | 3 | View Code Duplication | public function getBusinessUnits() |
107 | |||
108 | 3 | View Code Duplication | public function getJobs() |
124 | |||
125 | 3 | public function getEmployee($email) |
|
126 | 2 | { |
|
127 | 3 | if (empty($email)) { |
|
128 | 1 | throw new Exception("Email is invalid!"); |
|
129 | } |
||
130 | 3 | $this->login(); |
|
131 | 3 | $employees = []; |
|
132 | 3 | $response = $this->request("GET", $this->buildUrl("businessunitemployee"), [ |
|
133 | "query" => [ |
||
134 | 3 | "businessalias" => $this->alias, |
|
135 | 3 | "PersonEmailAddress" => $email, |
|
136 | ] |
||
137 | 3 | ]); |
|
138 | |||
139 | 3 | foreach ($response["Records"] as $unit) { |
|
140 | 3 | $employees[] = new Employee($unit); |
|
141 | 3 | } |
|
142 | |||
143 | 3 | return count($employees) > 0 ? $employees[0] : null; |
|
144 | } |
||
145 | |||
146 | |||
147 | 12 | protected function login() |
|
162 | |||
163 | /** |
||
164 | * Returns the Client instance |
||
165 | * @return Client |
||
166 | */ |
||
167 | 12 | public function getClient() |
|
176 | |||
177 | /** |
||
178 | * Executes a request to the PeopleMatter API |
||
179 | * @param string $method The request type |
||
180 | * @param string $url The url to request |
||
181 | * @param array $options An array of options for the request |
||
182 | * @return array The response as an array |
||
183 | */ |
||
184 | 12 | protected function request($method, $url, $options = []) |
|
207 | } |
||
208 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.