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\Rates; |
||
11 | class Request extends \SimpleUPS\Api\Request |
||
12 | { |
||
13 | private |
||
14 | /* @var string $pickupType */ |
||
15 | $pickupType = PickupType::DAILY_PICKUP, |
||
16 | /* @var string $rateType */ |
||
17 | $rateType, |
||
18 | |||
19 | /* @var Shipment $shipment */ |
||
20 | $shipment; |
||
21 | |||
22 | public function __construct() |
||
28 | |||
29 | /** |
||
30 | * Determine which API call will be made |
||
31 | * @internal |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getUrl() |
||
38 | |||
39 | /** |
||
40 | * Build the validate address request |
||
41 | * @internal |
||
42 | * @return string |
||
43 | * @throws \SimpleUPS\Api\MissingParameterException |
||
44 | */ |
||
45 | public function buildXml() |
||
81 | |||
82 | /** |
||
83 | * How the shipment will be picked up |
||
84 | * Default value is PickupType::DAILY_PICKUP |
||
85 | * @see PickupType |
||
86 | * |
||
87 | * @param string $pickupType |
||
88 | * |
||
89 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
90 | * @return Request |
||
91 | */ |
||
92 | View Code Duplication | public function setPickupType($pickupType) |
|
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | private function getPickupType() |
||
112 | |||
113 | /** |
||
114 | * How the shipment will be quoted |
||
115 | * Defaults: |
||
116 | * <ul> |
||
117 | * <li>RateType::DAILY_RATES when pickup type is PickupType::DAILY_PICKUP</li> |
||
118 | * <li>RateType::RETAIL_RATES when pickup type is PickupType::ONE_TIME_PICKUP, PickupType::ON_CALL_AIR, PickupType::LETTER_CENTER or PickupType::AIR_SERVICE_CENTER</li> |
||
119 | * </ul> |
||
120 | * @see PickupType |
||
121 | * @see RateType |
||
122 | * |
||
123 | * @param string $rateType |
||
124 | * |
||
125 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
126 | * @return Request |
||
127 | */ |
||
128 | View Code Duplication | public function setRateType($rateType) |
|
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | private function getRateType() |
||
148 | |||
149 | /** |
||
150 | * @param Shipment $shipment |
||
151 | * |
||
152 | * @return Request |
||
153 | */ |
||
154 | public function setShipment(Shipment $shipment) |
||
159 | |||
160 | /** |
||
161 | * @return Shipment |
||
162 | */ |
||
163 | public function getShipment() |
||
167 | } |
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.