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 namespace Ups\Entity; |
||
7 | class AddressValidationResponse |
||
8 | { |
||
9 | protected $response; |
||
10 | protected $requestAction; |
||
11 | |||
12 | /** |
||
13 | * AddressValidationResponse constructor. |
||
14 | * @param \SimpleXMLElement $xmlDocument |
||
15 | * @param $requestAction |
||
16 | */ |
||
17 | public function __construct(\SimpleXMLElement $xmlDocument, $requestAction) |
||
22 | |||
23 | /** |
||
24 | * Tells whether or not the NoCandidatesIndicator is present on the XML document. |
||
25 | * This indicator is returned if the address is so badly formed that UPS is |
||
26 | * unable to even offer any suggested alternatives |
||
27 | * |
||
28 | * @throws \BadMethodCallException |
||
29 | * @return bool |
||
30 | */ |
||
31 | View Code Duplication | public function noCandidates() |
|
38 | |||
39 | /** |
||
40 | * Tells whether or not the ValidAddressIndicator is present on the XML document. |
||
41 | * This indicator is present if provided address is valid and represents a |
||
42 | * single, unique address in the UPS Address Validation system. |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | public function isValid() |
||
53 | |||
54 | /** |
||
55 | * Tells whether or not the AmbiguousAddressIndicator is present on the XML document. |
||
56 | * This indicator is present when the address provided is not specific enough to |
||
57 | * be unique to one physical location, but provides enough |
||
58 | * |
||
59 | * @throws \BadMethodCallException |
||
60 | * @return bool |
||
61 | */ |
||
62 | View Code Duplication | public function isAmbiguous() |
|
69 | |||
70 | /** |
||
71 | * @throws \BadMethodCallException |
||
72 | * @return AddressClassification |
||
73 | */ |
||
74 | public function getAddressClassification() |
||
81 | |||
82 | /** |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getCandidateAddressList() |
||
96 | |||
97 | /** |
||
98 | * @return AVAddress |
||
99 | */ |
||
100 | public function getValidatedAddress() |
||
108 | } |
||
109 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.