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:
Complex classes like UPS 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 UPS, and based on these observations, apply Extract Interface, too.
1 | <?php namespace SimpleUPS; |
||
13 | class UPS |
||
14 | { |
||
15 | |||
16 | private static |
||
17 | |||
18 | /* @var null $accountNumber */ |
||
19 | $accountNumber, |
||
20 | |||
21 | /* @var null $accessLicenseNumber */ |
||
22 | $accessLicenseNumber, |
||
23 | |||
24 | /* @var null $userId */ |
||
25 | $userId, |
||
26 | |||
27 | /* @var null $shipperNumber */ |
||
28 | $shipperNumber, |
||
29 | |||
30 | /* @var null $password */ |
||
31 | $password, |
||
32 | |||
33 | /* @var null $debug */ |
||
34 | $debug, |
||
35 | |||
36 | /* @var \SimpleUPS\AddressValidate\Response $addressValidationResponse */ |
||
37 | $addressValidationResponse, |
||
38 | |||
39 | /* @var \SimpleUPS\RegionValidate\Response $regionValidationResponse */ |
||
40 | $regionValidationResponse, |
||
41 | |||
42 | /* @var Shipper $shipper */ |
||
43 | $shipper, |
||
44 | |||
45 | /* @var string $currencyCode */ |
||
46 | $currencyCode = 'USD'; |
||
47 | |||
48 | public static |
||
49 | /* @var Object UPS::$request The last request object */ |
||
50 | $request, |
||
51 | |||
52 | /* @var string UPS::$response The last response xml */ |
||
53 | $response, |
||
54 | |||
55 | /* @var string UPS::$libraryName The name of this library */ |
||
56 | $libraryName = 'SimpleUPS'; |
||
57 | |||
58 | /** |
||
59 | * @api |
||
60 | * @since 1.0 |
||
61 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
62 | * @throws \SimpleUPS\Api\RequestFailedException |
||
63 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
64 | * @throws \SimpleUPS\Api\MissingParameterException |
||
65 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
66 | */ |
||
67 | public static function getRates( |
||
84 | |||
85 | /** |
||
86 | * Determine if various combinations of city, state/province, postal code and country code are valid. |
||
87 | * If an address is invalid while the region is, then the invalid portion of the address is the street. Also |
||
88 | * assists with determining if city is misspelled or state doesn't match the postal code. |
||
89 | * Address fields used include: |
||
90 | * <ul> |
||
91 | * <li>city</li> |
||
92 | * <li>stateProvinceCode</li> |
||
93 | * <li>postalCode - REQUIRED</li> |
||
94 | * <li>countryCode - REQUIRED</li> |
||
95 | * </ul> |
||
96 | * @note If a postal code is missing, the region is not accurate enough and thus will always return false. |
||
97 | * @see \SimpleUPS\UPS::isValidAddress() |
||
98 | * @see \SimpleUPS\UPS::getSuggestedRegions() |
||
99 | * |
||
100 | * @param Address $address |
||
101 | * |
||
102 | * @return bool |
||
103 | * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_valid.phps" A valid region |
||
104 | * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_invalid.phps" An invalid region |
||
105 | * @example "http://ups.local/examples/SimpleUPS.isValidRegion()_with_isValidAddress().phps" An invalid region |
||
106 | * @api |
||
107 | * @since 1.0 |
||
108 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
109 | * @throws \SimpleUPS\Api\RequestFailedException |
||
110 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
111 | * @throws \SimpleUPS\Api\MissingParameterException |
||
112 | */ |
||
113 | public static function isValidRegion(Address $address) |
||
117 | |||
118 | /** |
||
119 | * Get the suggested regions available for an invalid region. |
||
120 | * Suggested regions are ordered by a "rank" that UPS provides based on estimated |
||
121 | * accuracy. |
||
122 | * A suggested region may be flagged as "corrected" and have a quality rating of 1 |
||
123 | * if there are minor mistakes such as a misspelled city or missing information that |
||
124 | * UPS is able to correct. |
||
125 | * @see \SimpleUPS\UPS::isValidRegion() |
||
126 | * |
||
127 | * @param Address $address |
||
128 | * |
||
129 | * @return \SimpleUPS\RegionValidate\RegionSuggestion[]|null |
||
130 | * @api |
||
131 | * @since 1.0 |
||
132 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
133 | * @throws \SimpleUPS\Api\RequestFailedException |
||
134 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
135 | * @throws \SimpleUPS\Api\MissingParameterException |
||
136 | */ |
||
137 | public static function getSuggestedRegions(Address $address) |
||
141 | |||
142 | /** |
||
143 | * Track a small package by tracking number |
||
144 | * |
||
145 | * @param string $trackingNumber |
||
146 | * |
||
147 | * @return \SimpleUPS\Track\SmallPackage\Shipment[]|null |
||
148 | * @api |
||
149 | * @since 1.0 |
||
150 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
151 | * @throws \SimpleUPS\Api\RequestFailedException |
||
152 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
153 | * @throws \SimpleUPS\Api\MissingParameterException |
||
154 | * @throws \SimpleUPS\Api\NoTrackingInformationException |
||
155 | * @throws \SimpleUPS\Track\TrackingNumberNotFoundException |
||
156 | */ |
||
157 | public static function trackByTrackingNumber($trackingNumber) |
||
168 | |||
169 | /** |
||
170 | * Determine if provided street address is valid. |
||
171 | * If an address is invalid while the region is, then the invalid portion of the address is the street. Also |
||
172 | * assists with determining if city is misspelled or state doesn't match the postal code. |
||
173 | * UPS will auto correct certain things about an address. For example, if the state is "XY" |
||
174 | * and the zip code is correct, UPS will ignore the state completely. However, UPS will provide |
||
175 | * a fully corrected address. It's advisable you use this address. |
||
176 | * @see getCorrectedAddress |
||
177 | * @see getSuggestedAddresses |
||
178 | * @see isValidRegion |
||
179 | * |
||
180 | * @param Address $address |
||
181 | * |
||
182 | * @return bool |
||
183 | * @api |
||
184 | * @since 1.0 |
||
185 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
186 | * @throws \SimpleUPS\Api\RequestFailedException |
||
187 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
188 | * @throws \SimpleUPS\Api\MissingParameterException |
||
189 | */ |
||
190 | public static function isValidAddress(Address $address) |
||
194 | |||
195 | /** |
||
196 | * Take an address and provide a corrected, fully valid address |
||
197 | * UPS will correct a slightly invalid address for you if it is able to determine the real |
||
198 | * address. For example, if the state is "XY" and the zip code is correct, UPS will provide |
||
199 | * a correct address where the state matches the zip code. It's advisable under all circumstances |
||
200 | * you use this address when it's provided. |
||
201 | * It is recommended that you always check the validity of an address before obtaining it's |
||
202 | * corrected version. |
||
203 | * @see isValidAddress |
||
204 | * @see getSuggestedAddresses |
||
205 | * |
||
206 | * @param Address $address |
||
207 | * |
||
208 | * @return \SimpleUPS\AddressValidate\Address|null |
||
209 | * @api |
||
210 | * @since 1.0 |
||
211 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
212 | * @throws \SimpleUPS\Api\RequestFailedException |
||
213 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
214 | * @throws \SimpleUPS\Api\MissingParameterException |
||
215 | */ |
||
216 | public static function getCorrectedAddress(Address $address) |
||
220 | |||
221 | /** |
||
222 | * If a address is invalid, UPS may provide suggestions for what the correct address is. |
||
223 | * If there are errors in a street address such as the street number not matching an actual |
||
224 | * address UPS may provide some suggestions. |
||
225 | * @note Suggested addresses can sometimes be grouped together. For instance if you enter a street number that is significantly off from what's available on the street, the suggested addresses will be grouped by the hundred. (ie: 10900-10998 MY STREET DR) |
||
226 | * @see isValidAddress |
||
227 | * @see getCorrectedAddress |
||
228 | * |
||
229 | * @param Address $address |
||
230 | * |
||
231 | * @return \SimpleUPS\AddressValidate\Address[]|null |
||
232 | * @api |
||
233 | * @since 1.0 |
||
234 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
235 | * @throws \SimpleUPS\Api\RequestFailedException |
||
236 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
237 | * @throws \SimpleUPS\Api\MissingParameterException |
||
238 | */ |
||
239 | public static function getSuggestedAddresses(Address $address) |
||
243 | |||
244 | /** |
||
245 | * Perform validation on the provided address if not already performed |
||
246 | * |
||
247 | * @param Address $address |
||
248 | * |
||
249 | * @internal |
||
250 | * @return \SimpleUPS\AddressValidate\Request |
||
251 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
252 | * @throws \SimpleUPS\Api\RequestFailedException |
||
253 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
254 | * @throws \SimpleUPS\Api\MissingParameterException |
||
255 | */ |
||
256 | View Code Duplication | private static function _performAddressValidation(Address $address) |
|
267 | |||
268 | /** |
||
269 | * Perform validation on the provided region of the address if not already performed |
||
270 | * |
||
271 | * @param Address $address |
||
272 | * |
||
273 | * @internal |
||
274 | * @return \SimpleUPS\AddressValidate\Request |
||
275 | * @throws \SimpleUPS\Api\InvalidResponseException |
||
276 | * @throws \SimpleUPS\Api\RequestFailedException |
||
277 | * @throws \SimpleUPS\Api\ResponseErrorException |
||
278 | * @throws \SimpleUPS\Api\MissingParameterException |
||
279 | */ |
||
280 | View Code Duplication | private static function _performRegionValidation(Address $address) |
|
291 | |||
292 | /** |
||
293 | * Set the UPS Access License Number, User ID, Password and Account Number. |
||
294 | * Can also be set with the constants UPS_ACCESSLICENSENUMBER, UPS_USERID, UPS_PASSWORD |
||
295 | * @see \SimpleUPS\UPS::getAccountNumber() |
||
296 | * @see \SimpleUPS\UPS::getAccessLicenseNumber() |
||
297 | * @see \SimpleUPS\UPS::getUserId() |
||
298 | * @see \SimpleUPS\UPS::getPassword() |
||
299 | * @api |
||
300 | * @since 1.0 |
||
301 | * |
||
302 | * @param string $accessLicenseNumber |
||
303 | * @param string $userId |
||
304 | * @param string $password |
||
305 | * |
||
306 | * @throws \Exception |
||
307 | */ |
||
308 | public static function setAuthentication($accessLicenseNumber, $userId, $password) |
||
314 | |||
315 | /** |
||
316 | * @internal |
||
317 | * |
||
318 | * @param $accountNumber |
||
319 | */ |
||
320 | private static function setAccountNumber($accountNumber) |
||
324 | |||
325 | /** |
||
326 | * Get the account number to be used in API requests |
||
327 | * Can also be set with the constant UPS_ACCOUNTNUMBER |
||
328 | * @see \SimpleUPS\UPS::setAuthentication() |
||
329 | * @see \SimpleUPS\UPS::getAccessLicenseNumber() |
||
330 | * @see \SimpleUPS\UPS::getUserId() |
||
331 | * @see \SimpleUPS\UPS::getPassword() |
||
332 | * @api |
||
333 | * @since 1.0 |
||
334 | * @return string|null |
||
335 | */ |
||
336 | public static function getAccountNumber() |
||
344 | |||
345 | /** |
||
346 | * @param $accessLicenseNumber |
||
347 | * |
||
348 | * @internal |
||
349 | */ |
||
350 | private static function setAccessLicenseNumber($accessLicenseNumber) |
||
354 | |||
355 | /** |
||
356 | * Get the license number to be used in API requests |
||
357 | * Can also be set with the constant UPS_ACCESSLICENSENUMBER |
||
358 | * @see \SimpleUPS\UPS::setAuthentication() |
||
359 | * @see \SimpleUPS\UPS::getAccountNumber() |
||
360 | * @see \SimpleUPS\UPS::getUserId() |
||
361 | * @see \SimpleUPS\UPS::getPassword() |
||
362 | * @api |
||
363 | * @since 1.0 |
||
364 | * @return string|null |
||
365 | */ |
||
366 | public static function getAccessLicenseNumber() |
||
374 | |||
375 | /** |
||
376 | * @param $password |
||
377 | * |
||
378 | * @internal |
||
379 | */ |
||
380 | private static function setPassword($password) |
||
384 | |||
385 | /** |
||
386 | * Get the password to be used in API requests |
||
387 | * Can also be set with the constant UPS_PASSWORD |
||
388 | * @see \SimpleUPS\UPS::setAuthentication() |
||
389 | * @see \SimpleUPS\UPS::getAccountNumber() |
||
390 | * @see \SimpleUPS\UPS::getAccessLicenseNumber() |
||
391 | * @api |
||
392 | * @since 1.0 |
||
393 | * @return string|null |
||
394 | */ |
||
395 | public static function getPassword() |
||
403 | |||
404 | /** |
||
405 | * @param $userId |
||
406 | * |
||
407 | * @internal |
||
408 | */ |
||
409 | private static function setUserId($userId) |
||
413 | |||
414 | /** |
||
415 | * Get the user id to be used in API requests |
||
416 | * Can also be set with the constant UPS_USERID |
||
417 | * @see \SimpleUPS\UPS::setAuthentication() |
||
418 | * @see \SimpleUPS\UPS::getAccountNumber() |
||
419 | * @see \SimpleUPS\UPS::getAccessLicenseNumber() |
||
420 | * @see \SimpleUPS\UPS::getPassword() |
||
421 | * @api |
||
422 | * @since 1.0 |
||
423 | * @return string|null |
||
424 | */ |
||
425 | public static function getUserId() |
||
433 | |||
434 | /** |
||
435 | * Number to use in rate requests |
||
436 | * Can also be set with the constants UPS_SHIPPERNUMBER |
||
437 | * @api |
||
438 | * |
||
439 | * @param Shipper $shipper |
||
440 | */ |
||
441 | public static function setShipper(Shipper $shipper) |
||
445 | |||
446 | /** |
||
447 | * Shipper information used in rate requests |
||
448 | * @todo document how to set this via objects & constants |
||
449 | * @api |
||
450 | * @since 1.0 |
||
451 | * @return Shipper |
||
452 | */ |
||
453 | public static function getShipper() |
||
502 | |||
503 | /** |
||
504 | * Set the currency code to be used |
||
505 | * @api |
||
506 | * |
||
507 | * @param string $currencyCode |
||
508 | */ |
||
509 | public static function setCurrencyCode($currencyCode) |
||
513 | |||
514 | /** |
||
515 | * Get the currency code |
||
516 | * @since 1.0 |
||
517 | * @return string |
||
518 | */ |
||
519 | public static function getCurrencyCode() |
||
527 | |||
528 | /** |
||
529 | * Define debug mode |
||
530 | * Can also be set with the constant UPS_DEBUG |
||
531 | * @note While the library is in debug mode, it uses more memory storing additional information about each request. |
||
532 | * @see \SimpleUPS\UPS::getDebugOutput() |
||
533 | * @api |
||
534 | * @since 1.0 |
||
535 | * |
||
536 | * @param bool $debug |
||
537 | */ |
||
538 | public static function setDebug($debug) |
||
542 | |||
543 | /** |
||
544 | * Determine if the library is in debug mode |
||
545 | * @see \SimpleUPS\UPS::getDebugOutput() |
||
546 | * @see \SimpleUPS\UPS::getDebug() |
||
547 | * @see \SimpleUPS\UPS::setDebug() |
||
548 | * @since 1.0 |
||
549 | * @return bool |
||
550 | */ |
||
551 | public static function getDebug() |
||
563 | |||
564 | //@todo Add logging method here that could be overridden for frameworks like Yii |
||
565 | |||
566 | /** |
||
567 | * Prints debug output |
||
568 | * @see \SimpleUPS\UPS::getDebug() |
||
569 | * @see \SimpleUPS\UPS::setDebug() |
||
570 | * @api |
||
571 | * @since 1.0 |
||
572 | */ |
||
573 | public static function getDebugOutput() |
||
581 | |||
582 | /** |
||
583 | * Used as the autoloader to load classes as they're needed |
||
584 | * |
||
585 | * @param $class |
||
586 | * |
||
587 | * @internal |
||
588 | */ |
||
589 | public static function load($class) |
||
609 | } |
||
610 | |||
611 | spl_autoload_register(__NAMESPACE__ . '\UPS::load'); |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.