Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Client extends Guzzle |
||
12 | { |
||
13 | const BASE_URL = 'https://onfleet.com/api/{version}/'; |
||
14 | const VERSION = 'v2'; |
||
15 | |||
16 | /** |
||
17 | * Client constructor. |
||
18 | * @param string $username |
||
19 | * @param array $config |
||
20 | */ |
||
21 | public function __construct($username, array $config = []) |
||
35 | |||
36 | /** |
||
37 | * @return Organization |
||
38 | */ |
||
39 | public function getMyOrganization(): Organization |
||
44 | |||
45 | /** |
||
46 | * @param array $data { |
||
47 | * @var string $name The administrator’s complete name. |
||
48 | * @var string $email The administrator’s email address. |
||
49 | * @var string $phone Optional. The administrator’s phone number. |
||
50 | * @var boolean $isReadOnly Optional. Whether this administrator can perform write operations. |
||
51 | * } |
||
52 | * @return Administrator |
||
53 | */ |
||
54 | public function createAdministrator(array $data): Administrator |
||
59 | |||
60 | /** |
||
61 | * @return Administrator[] |
||
62 | */ |
||
63 | public function getAdministrators(): array |
||
74 | |||
75 | /** |
||
76 | * @param array $data { |
||
77 | * @var string $name The worker’s complete name. |
||
78 | * @var string $phone A valid phone number as per the worker’s organization’s country. |
||
79 | * @var string|array $teams One or more team IDs of which the worker is a member. |
||
80 | * @var object $vehicle Optional. The worker’s vehicle; providing no vehicle details is equivalent to the |
||
81 | * worker being on foot. |
||
82 | * @var string $type The vehicle’s type, must be one of CAR, MOTORCYCLE, BICYCLE or TRUCK. |
||
83 | * @var string $description Optional. The vehicle’s make, model, year, or any other relevant identifying details. |
||
84 | * @var string $licensePlate Optional. The vehicle’s license plate number. |
||
85 | * @var string $color Optional. The vehicle's color. |
||
86 | * @var integer $capacity Optional. The maximum number of units this worker can carry, for route optimization purposes. |
||
87 | * } |
||
88 | * @return Worker |
||
89 | */ |
||
90 | public function createWorker(array $data): Worker |
||
95 | |||
96 | /** |
||
97 | * @param string $filter Optional. A comma-separated list of fields to return, if all are not desired. For example, name, location. |
||
98 | * @param string $teams Optional. A comma-separated list of the team IDs that workers must be part of. |
||
99 | * @param string $states Optional. A comma-separated list of worker states, where |
||
100 | * 0 is off-duty, 1 is idle (on-duty, no active task) and 2 is active (on-duty, active task). |
||
101 | * @return Worker[] |
||
102 | */ |
||
103 | public function getWorkers($filter = null, $teams = null, $states = null): array |
||
120 | |||
121 | /** |
||
122 | * @param string $id |
||
123 | * @param string $filter Optional. A comma-separated list of fields to return, if all are not desired. |
||
124 | * For example: "name, location". |
||
125 | * @param bool $analytics Basic worker duty event, traveled distance (meters) and time analytics are optionally |
||
126 | * available by specifying the query parameter analytics as true. |
||
127 | * @return Worker |
||
128 | * |
||
129 | * @todo: Add "from" and "to" timestamps if analytics is true |
||
130 | */ |
||
131 | public function getWorker($id, $filter = null, $analytics = false): Worker |
||
142 | |||
143 | /** |
||
144 | * @return Hub[] |
||
145 | */ |
||
146 | public function getHubs(): array |
||
157 | |||
158 | /** |
||
159 | * @param array $data { |
||
160 | * @var string $name A unique name for the team. |
||
161 | * @var array $workers An array of worker IDs. |
||
162 | * @var array $managers An array of managing administrator IDs. |
||
163 | * @var string $hub Optional. The ID of the team's hub. |
||
164 | * } |
||
165 | * |
||
166 | * @return Team |
||
167 | */ |
||
168 | public function createTeam(array $data): Team |
||
173 | |||
174 | /** |
||
175 | * @return Team[] |
||
176 | */ |
||
177 | public function getTeams(): array |
||
188 | |||
189 | /** |
||
190 | * @param $id |
||
191 | * @return Team |
||
192 | */ |
||
193 | public function getTeam($id): Team |
||
198 | |||
199 | /** |
||
200 | * @param array $data { |
||
201 | * @var array $address The destination’s street address details. { |
||
202 | * @var string $name Optional. A name associated with this address, e.g., "Transamerica Pyramid". |
||
203 | * @var string $number The number component of this address, it may also contain letters. |
||
204 | * @var string $street The name of the street. |
||
205 | * @var string $apartment Optional. The suite or apartment number, or any additional relevant information. |
||
206 | * @var string $city The name of the municipality. |
||
207 | * @var string $state Optional. The name of the state, province or jurisdiction. |
||
208 | * @var string $postalCode Optional. The postal or zip code. |
||
209 | * @var string $country The name of the country. |
||
210 | * @var string $unparsed Optional. A complete address specified in a single, unparsed string where the |
||
211 | * various elements are separated by commas. If present, all other address |
||
212 | * properties will be ignored (with the exception of name and apartment). |
||
213 | * In some countries, you may skip most address details (like city or state) |
||
214 | * if you provide a valid postalCode: for example, |
||
215 | * 543 Howard St, 94105, USA will be geocoded correctly. |
||
216 | * } |
||
217 | * @var string $notes Optional. Note that goes with this destination, e.g. "Please call before" |
||
218 | * @var array $location Optional. The [ longitude, latitude ] geographic coordinates. If missing, the API will |
||
219 | * geocode based on the address details provided. Note that geocoding may slightly modify |
||
220 | * the format of the address properties. address.unparsed cannot be provided if you are |
||
221 | * also including a location. |
||
222 | * } |
||
223 | * @return Destination |
||
224 | */ |
||
225 | public function createDestination(array $data): Destination |
||
230 | |||
231 | /** |
||
232 | * @param $id |
||
233 | * @return Destination |
||
234 | */ |
||
235 | public function getDestination($id): Destination |
||
240 | |||
241 | /** |
||
242 | * @param array $data { |
||
243 | * @var string $name The recipient’s complete name. |
||
244 | * @var string $phone A unique, valid phone number as per the recipient’s organization’s country. |
||
245 | * @var string $notes Optional. Notes for this recipient: these are global notes that should not be |
||
246 | * task- or destination-specific. |
||
247 | * For example, "Customer since June 2012, does not drink non-specialty coffee". |
||
248 | * @var boolean $skipSMSNotifications Optional. Whether this recipient has requested to not receive SMS |
||
249 | * notifications. Defaults to false if not provided. |
||
250 | * @var boolean $skipPhoneNumberValidation Optional. Whether to skip validation of this recipient's phone |
||
251 | * number. An E.164-like number is still required (must start with +), however the API |
||
252 | * will not enforce any country-specific validation rules. |
||
253 | * } |
||
254 | * @return Recipient |
||
255 | */ |
||
256 | public function createRecipient(array $data): Recipient |
||
261 | |||
262 | /** |
||
263 | * @param $id |
||
264 | * @return Recipient |
||
265 | */ |
||
266 | public function getRecipient($id): Recipient |
||
271 | |||
272 | /** |
||
273 | * @param string $name |
||
274 | * @return Recipient |
||
275 | */ |
||
276 | public function getRecipientByName($name): Recipient |
||
281 | |||
282 | /** |
||
283 | * @param string $phone |
||
284 | * @return Recipient |
||
285 | */ |
||
286 | public function getRecipientByPhone($phone): Recipient |
||
291 | |||
292 | /** |
||
293 | * @see Task::createAutoAssignedArray |
||
294 | * @param array $data |
||
295 | * @return Task |
||
296 | */ |
||
297 | public function createTask(array $data): Task |
||
302 | |||
303 | /** |
||
304 | * @param int $from The starting time of the range. Tasks created or completed at or after this time will be included. |
||
305 | * @param int $to Optional. If missing, defaults to the current time. The ending time of the range. |
||
306 | * Tasks created or completed before this time will be included. |
||
307 | * @param string $lastId Optional. Used to walk the paginated response, if there is one. Tasks created after this ID |
||
308 | * will be returned, up to the per-query limit of 64. |
||
309 | * @return Task[] |
||
310 | */ |
||
311 | public function getTasks($from, $to = null, &$lastId = null): array |
||
330 | |||
331 | /** |
||
332 | * @param $id |
||
333 | * @return Task |
||
334 | */ |
||
335 | public function getTask($id): Task |
||
340 | |||
341 | /** |
||
342 | * @param $shortId |
||
343 | * @return Task |
||
344 | */ |
||
345 | public function getTaskByShortId($shortId): Task |
||
350 | |||
351 | /** |
||
352 | * Replace all organization's tasks. |
||
353 | * |
||
354 | * @param array $taskIds |
||
355 | * @param string $organizationId |
||
356 | */ |
||
357 | public function setOrganizationTasks(array $taskIds, $organizationId) |
||
361 | |||
362 | /** |
||
363 | * @param array $taskIds |
||
364 | * @param string $organizationId |
||
365 | */ |
||
366 | public function addTasksToOrganization(array $taskIds, $organizationId) |
||
370 | |||
371 | /** |
||
372 | * Replace all team's tasks. |
||
373 | * |
||
374 | * @param array $taskIds |
||
375 | * @param string $teamId |
||
376 | */ |
||
377 | public function setTeamTasks(array $taskIds, $teamId) |
||
381 | |||
382 | /** |
||
383 | * @param array $taskIds |
||
384 | * @param string $teamId |
||
385 | */ |
||
386 | public function addTasksToTeam(array $taskIds, $teamId) |
||
390 | |||
391 | /** |
||
392 | * Replace all worker's tasks. |
||
393 | * |
||
394 | * @param array $taskIds |
||
395 | * @param string $workerId |
||
396 | */ |
||
397 | public function setWorkerTasks(array $taskIds, $workerId) |
||
401 | |||
402 | /** |
||
403 | * @param array $taskIds |
||
404 | * @param string $workerId |
||
405 | */ |
||
406 | public function addTasksToWorker(array $taskIds, $workerId) |
||
410 | |||
411 | /** |
||
412 | * @param $url |
||
413 | * @param $triggerId |
||
414 | * @return Webhook |
||
415 | */ |
||
416 | public function createWebhook($url, $triggerId): Webhook |
||
427 | |||
428 | /** |
||
429 | * @return Webhook[] |
||
430 | */ |
||
431 | public function getWebhooks(): array |
||
442 | |||
443 | /** |
||
444 | * @param string $containerEndpoint "organizations", "workers" or "teams" |
||
445 | * @param string $targetId ID of organization, worker or team. |
||
446 | * @param array $taskIds Array of task IDs. |
||
447 | * @param int $position Insert tasks at a given index. To append to the end, use -1, to prepend, use 0. |
||
448 | */ |
||
449 | private function setContainerTasks($containerEndpoint, $targetId, array $taskIds, $position = null) |
||
465 | } |
||
466 | |||
467 |