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 SimpleUPS\AddressValidate; |
||
12 | class Request extends \SimpleUPS\Api\Request |
||
13 | { |
||
14 | private |
||
15 | /* @var Address $address */ |
||
16 | $address; |
||
17 | |||
18 | /** |
||
19 | * @param null $debug |
||
20 | */ |
||
21 | public function __construct($debug = null) |
||
27 | |||
28 | /** |
||
29 | * Determine which API call will be made |
||
30 | * @return string |
||
31 | */ |
||
32 | public function getUrl() |
||
36 | |||
37 | /** |
||
38 | * Build the validate address request |
||
39 | * @return string |
||
40 | * @throws \SimpleUPS\Api\MissingParameterException |
||
41 | */ |
||
42 | public function buildXml() |
||
43 | { |
||
44 | if (serialize($this->getAddress()) == serialize(new Address())) { |
||
45 | throw new MissingParameterException( |
||
46 | 'Address requires a Country code and a City, a State\Province code or a Postal code' |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | if ($this->getAddress()->getCountryCode() === null) { |
||
51 | throw new MissingParameterException('Address requires a Country code'); |
||
52 | } |
||
53 | |||
54 | $dom = new \DomDocument('1.0'); |
||
55 | $dom->formatOutput = $this->getDebug(); |
||
56 | $dom->appendChild($addressRequest = $dom->createElement('AddressValidationRequest')); |
||
57 | $addressRequestLang = $dom->createAttribute('xml:lang'); |
||
58 | $addressRequestLang->value = parent::getXmlLang(); |
||
|
|||
59 | $addressRequest->appendChild($request = $dom->createElement('Request')); |
||
60 | $request->appendChild($transactionReference = $dom->createElement('TransactionReference')); |
||
61 | $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext())); |
||
62 | |||
63 | $request->appendChild($dom->createElement('RequestAction', 'XAV')); |
||
64 | $request->appendChild($dom->createElement('RequestOption', '3')); |
||
65 | |||
66 | $addressRequest->appendChild($address = $dom->createElement('AddressKeyFormat')); |
||
67 | |||
68 | $address->appendChild($dom->createElement('AddressLine', $this->getAddress()->getStreet())); |
||
69 | if ($this->getAddress()->getCity() != null) { |
||
70 | $address->appendChild($dom->createElement('PoliticalDivision2', $this->getAddress()->getCity())); |
||
71 | } |
||
72 | |||
73 | if ($this->getAddress()->getStateProvinceCode() != null) { |
||
74 | $address->appendChild( |
||
75 | $dom->createElement('PoliticalDivision1', $this->getAddress()->getStateProvinceCode()) |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | if ($this->getAddress()->getPostalCode() != null) { |
||
80 | $address->appendChild($dom->createElement('PostcodePrimaryLow', $this->getAddress()->getPostalCode())); |
||
81 | } |
||
82 | |||
83 | $address->appendChild($dom->createElement('CountryCode', $this->getAddress()->getCountryCode())); |
||
84 | $xml = parent::buildAuthenticationXml() . $dom->saveXML(); |
||
85 | |||
86 | return $xml; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return Response|\SimpleUPS\Api\Response|\SimpleXMLElement |
||
91 | */ |
||
92 | View Code Duplication | public function sendRequest() |
|
101 | |||
102 | /** |
||
103 | * @param Address $address |
||
104 | * |
||
105 | * @return Request |
||
106 | */ |
||
107 | public function setAddress(Address $address) |
||
112 | |||
113 | /** |
||
114 | * @return Address |
||
115 | */ |
||
116 | public function getAddress() |
||
120 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.