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 |
||
28 | class Tax implements ITax |
||
29 | { |
||
30 | use TPayload, TPayloadLogger, TAmount; |
||
31 | |||
32 | const ROOT_NODE = 'Tax'; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $type; |
||
36 | /** @var string */ |
||
37 | protected $taxability; |
||
38 | /** @var string */ |
||
39 | protected $situs; |
||
40 | /** @var string */ |
||
41 | protected $jurisdiction; |
||
42 | /** @var string */ |
||
43 | protected $jurisdictionLevel; |
||
44 | /** @var string */ |
||
45 | protected $jurisdictionId; |
||
46 | /** @var string */ |
||
47 | protected $imposition; |
||
48 | /** @var string */ |
||
49 | protected $impositionType; |
||
50 | /** @var float */ |
||
51 | protected $effectiveRate; |
||
52 | /** @var float */ |
||
53 | protected $taxableAmount; |
||
54 | /** @var float */ |
||
55 | protected $calculatedTax; |
||
56 | /** @var string */ |
||
57 | protected $sellerRegistrationId; |
||
58 | /** @var array */ |
||
59 | protected $allowedTaxTypes = [ |
||
60 | self::TAX_TYPE_SALES, |
||
61 | self::TAX_TYPE_SELLER_USE, |
||
62 | self::TAX_TYPE_CONSUMER_USE, |
||
63 | self::TAX_TYPE_VAT, |
||
64 | self::TAX_TYPE_IMPORT_VAT, |
||
65 | self::TAX_TYPE_NONE, |
||
66 | ]; |
||
67 | /** @var array */ |
||
68 | protected $allowedTaxabilities = [ |
||
69 | self::TAXABILITY_TAXABLE, |
||
70 | self::TAXABILITY_NONTAXABLE, |
||
71 | self::TAXABILITY_EXEMPT, |
||
72 | self::TAXABILITY_DPPAPPLIED, |
||
73 | self::TAXABILITY_NO_TAX, |
||
74 | self::TAXABILITY_DEFERRED, |
||
75 | ]; |
||
76 | /** @var array */ |
||
77 | protected $allowedSituses = [ |
||
78 | self::SITUS_ADMINISTRATIVE_DESTINATION, |
||
79 | self::SITUS_ADMINISTRATIVE_ORIGIN, |
||
80 | self::SITUS_DESTINATION, |
||
81 | self::SITUS_PHYSICAL_ORIGIN, |
||
82 | ]; |
||
83 | /** @var array */ |
||
84 | protected $allowedJurisdictionLevels = [ |
||
85 | self::JURISDICTION_LEVEL_APO, |
||
86 | self::JURISDICTION_LEVEL_BOROUGH, |
||
87 | self::JURISDICTION_LEVEL_CITY, |
||
88 | self::JURISDICTION_LEVEL_COUNTRY, |
||
89 | self::JURISDICTION_LEVEL_COUNTY, |
||
90 | self::JURISDICTION_LEVEL_DISTRICT, |
||
91 | self::JURISDICTION_LEVEL_FPO, |
||
92 | self::JURISDICTION_LEVEL_LOCAL_IMPROVEMENT_DISTRICT, |
||
93 | self::JURISDICTION_LEVEL_PARISH, |
||
94 | self::JURISDICTION_LEVEL_SPECIAL_PURPOSE_DISTRICT, |
||
95 | self::JURISDICTION_LEVEL_STATE, |
||
96 | self::JURISDICTION_LEVEL_TERRITORY, |
||
97 | self::JURISDICTION_LEVEL_TOWNSHIP, |
||
98 | self::JURISDICTION_LEVEL_TRADE_BLOCK, |
||
99 | self::JURISDICTION_LEVEL_TRANSIT_DISTRICT, |
||
100 | self::JURISDICTION_LEVEL_PROVINCE, |
||
101 | ]; |
||
102 | |||
103 | /** |
||
104 | * @param IValidatorIterator |
||
105 | * @param ISchemaValidator |
||
106 | * @param IPayloadMap |
||
107 | * @param LoggerInterface |
||
108 | * @param IPayload |
||
109 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
110 | */ |
||
111 | public function __construct( |
||
112 | IValidatorIterator $validators, |
||
113 | ISchemaValidator $schemaValidator, |
||
114 | IPayloadMap $payloadMap, |
||
115 | LoggerInterface $logger, |
||
116 | IPayload $parentPayload = null |
||
117 | ) { |
||
118 | $this->logger = $logger; |
||
|
|||
119 | $this->validators = $validators; |
||
120 | $this->parentPayload = $parentPayload; |
||
121 | |||
122 | $this->extractionPaths = [ |
||
123 | 'type' => 'string(@taxType)', |
||
124 | 'taxability' => 'string(@taxability)', |
||
125 | 'situs' => 'string(x:Situs)', |
||
126 | 'effectiveRate' => 'number(x:EffectiveRate)', |
||
127 | 'calculatedTax' => 'number(x:CalculatedTax)', |
||
128 | ]; |
||
129 | $this->optionalExtractionPaths = [ |
||
130 | 'jurisdiction' => 'x:Jurisdiction', |
||
131 | 'jurisdictionLevel' => 'x:Jurisdiction/@jurisdictionLevel', |
||
132 | 'jurisdictionId' => 'x:Jurisdiction/@jurisdictionId', |
||
133 | 'imposition' => 'x:Imposition', |
||
134 | 'impositionType' => 'x:Imposition/@impositionType', |
||
135 | 'taxableAmount' => 'x:TaxableAmount', |
||
136 | 'sellerRegistrationId' => 'x:SellerRegistrationId', |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | public function getType() |
||
144 | |||
145 | public function setType($type) |
||
146 | { |
||
147 | $this->type = in_array($type, $this->allowedTaxTypes) ? $type : null; |
||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | public function getTaxability() |
||
155 | |||
156 | public function setTaxability($taxability) |
||
157 | { |
||
158 | $this->taxability = in_array($taxability, $this->allowedTaxabilities) ? $taxability : null; |
||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | public function getSitus() |
||
166 | |||
167 | public function setSitus($situs) |
||
168 | { |
||
169 | $this->situs = in_array($situs, $this->allowedSituses) ? $situs : null; |
||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | public function getJurisdiction() |
||
177 | |||
178 | public function setJurisdiction($jurisdiction) |
||
179 | { |
||
180 | $this->jurisdiction = $jurisdiction; |
||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | public function getJurisdictionLevel() |
||
188 | |||
189 | View Code Duplication | public function setJurisdictionLevel($jurisdictionLevel) |
|
190 | { |
||
191 | $isAllowed = in_array($jurisdictionLevel, $this->allowedJurisdictionLevels); |
||
192 | $this->jurisdictionLevel = $isAllowed ? $jurisdictionLevel : null; |
||
193 | if (!$isAllowed) { |
||
194 | $logData = ['jurisdiction_level' => $jurisdictionLevel]; |
||
195 | $this->logger->warning( |
||
196 | 'Jurisdiction Level "{jurisdiction_level}" is not allowed.', |
||
197 | $this->getLogContextData(__CLASS__, $logData) |
||
198 | ); |
||
199 | } |
||
200 | return $this; |
||
201 | } |
||
202 | public function getJurisdictionId() |
||
206 | |||
207 | public function setJurisdictionId($jurisdictionId) |
||
208 | { |
||
209 | $this->jurisdictionId = $jurisdictionId; |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function getImposition() |
||
217 | |||
218 | public function setImposition($imposition) |
||
219 | { |
||
220 | $this->imposition = $imposition; |
||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | public function getImpositionType() |
||
228 | |||
229 | public function setImpositionType($impositionType) |
||
230 | { |
||
231 | $this->impositionType = $this->cleanString($impositionType, 60); |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getEffectiveRate() |
||
239 | |||
240 | public function setEffectiveRate($effectiveRate) |
||
241 | { |
||
242 | $this->effectiveRate = $effectiveRate; |
||
243 | return $this; |
||
244 | } |
||
245 | |||
246 | public function getTaxableAmount() |
||
250 | |||
251 | public function setTaxableAmount($taxableAmount) |
||
252 | { |
||
253 | $this->taxableAmount = $this->sanitizeAmount($taxableAmount); |
||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | public function getCalculatedTax() |
||
261 | |||
262 | public function setCalculatedTax($calculatedTax) |
||
263 | { |
||
264 | $this->calculatedTax = $this->sanitizeAmount($calculatedTax); |
||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | public function getSellerRegistrationId() |
||
272 | |||
273 | public function setSellerRegistrationId($sellerRegistrationId) |
||
274 | { |
||
275 | $this->sellerRegistrationId = $sellerRegistrationId; |
||
276 | return $this; |
||
277 | } |
||
278 | |||
279 | protected function serializeContents() |
||
280 | { |
||
281 | return "<Situs>{$this->xmlEncode($this->getSitus())}</Situs>" |
||
282 | . $this->serializeJurisdiction() |
||
283 | . $this->serializeImposition() |
||
284 | . $this->serializeEffectiveRate() |
||
285 | . $this->serializeOptionalAmount('TaxableAmount', $this->getTaxableAmount()) |
||
286 | . $this->serializeAmount('CalculatedTax', $this->getCalculatedTax()) |
||
287 | . $this->serializeOptionalXmlEncodedValue('SellerRegistrationId', $this->getSellerRegistrationId()); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Serialize the EffectiveRate. |
||
292 | * Despite the xsd documentation, this value should not be rounded. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected function serializeEffectiveRate() |
||
297 | { |
||
298 | // As a side effect, sprintf will prevent any xml-unsafe value if getEffectiveRate is a string. |
||
299 | return sprintf('<EffectiveRate>%F</EffectiveRate>', $this->getEffectiveRate()); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Serialize the tax jurisdiction, id and level. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | protected function serializeJurisdiction() |
||
308 | { |
||
309 | $jurisdiction = $this->getJurisdiction(); |
||
310 | return !is_null($jurisdiction) |
||
311 | ? '<Jurisdiction' |
||
312 | . $this->serializeOptionalAttribute('jurisdictionId', $this->xmlEncode($this->getJurisdictionId())) |
||
313 | . $this->serializeOptionalAttribute('jurisdictionLevel', $this->xmlEncode($this->getJurisdictionLevel())) |
||
314 | . ">{$this->xmlEncode($jurisdiction)}</Jurisdiction>" |
||
315 | : ''; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Serialize the tax imposition and type. |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | protected function serializeImposition() |
||
324 | { |
||
325 | $imposition = $this->getImposition(); |
||
326 | return !is_null($imposition) |
||
327 | ? '<Imposition' |
||
328 | . $this->serializeOptionalAttribute('impositionType', $this->xmlEncode($this->getImpositionType())) |
||
329 | . ">{$this->xmlEncode($imposition)}</Imposition>" |
||
330 | : ''; |
||
331 | } |
||
332 | |||
333 | protected function getRootAttributes() |
||
334 | { |
||
335 | return [ |
||
336 | 'taxType' => $this->getType(), |
||
337 | 'taxability' => $this->getTaxability(), |
||
338 | ]; |
||
339 | } |
||
340 | |||
341 | protected function getRootNodeName() |
||
345 | |||
346 | protected function getXmlNamespace() |
||
350 | } |
||
351 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..