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 VatCalculator 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 VatCalculator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class VatCalculator |
||
11 | { |
||
12 | /** |
||
13 | * VAT Service check URL provided by the EU. |
||
14 | */ |
||
15 | const VAT_SERVICE_URL = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; |
||
16 | |||
17 | /** |
||
18 | * We're using the free ip2c service to lookup IP 2 country. |
||
19 | */ |
||
20 | const GEOCODE_SERVICE_URL = 'https://api.ip2country.info/ip?'; |
||
21 | |||
22 | protected $soapClient; |
||
23 | |||
24 | /** |
||
25 | * All available tax rules and their exceptions. |
||
26 | * |
||
27 | * Taken from: https://ec.europa.eu/taxation_customs/resources/documents/taxation/vat/how_vat_works/rates/vat_rates_en.pdf |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $taxRules = [ |
||
32 | 'AT' => [ // Austria |
||
33 | 'rate' => 0.20, |
||
34 | 'exceptions' => [ |
||
35 | 'Jungholz' => 0.19, |
||
36 | 'Mittelberg' => 0.19, |
||
37 | ], |
||
38 | ], |
||
39 | 'BE' => [ // Belgium |
||
40 | 'rate' => 0.21, |
||
41 | ], |
||
42 | 'BG' => [ // Bulgaria |
||
43 | 'rate' => 0.20, |
||
44 | ], |
||
45 | 'CY' => [ // Cyprus |
||
46 | 'rate' => 0.19, |
||
47 | ], |
||
48 | 'CZ' => [ // Czech Republic |
||
49 | 'rate' => 0.21, |
||
50 | ], |
||
51 | 'DE' => [ // Germany |
||
52 | 'rate' => 0.19, |
||
53 | 'exceptions' => [ |
||
54 | 'Heligoland' => 0, |
||
55 | 'Büsingen am Hochrhein' => 0, |
||
56 | ], |
||
57 | ], |
||
58 | 'DK' => [ // Denmark |
||
59 | 'rate' => 0.25, |
||
60 | ], |
||
61 | 'EE' => [ // Estonia |
||
62 | 'rate' => 0.20, |
||
63 | ], |
||
64 | 'EL' => [ // Hellenic Republic (Greece) |
||
65 | 'rate' => 0.24, |
||
66 | 'exceptions' => [ |
||
67 | 'Mount Athos' => 0, |
||
68 | ], |
||
69 | ], |
||
70 | 'ES' => [ // Spain |
||
71 | 'rate' => 0.21, |
||
72 | 'exceptions' => [ |
||
73 | 'Canary Islands' => 0, |
||
74 | 'Ceuta' => 0, |
||
75 | 'Melilla' => 0, |
||
76 | ], |
||
77 | ], |
||
78 | 'FI' => [ // Finland |
||
79 | 'rate' => 0.24, |
||
80 | ], |
||
81 | 'FR' => [ // France |
||
82 | 'rate' => 0.20, |
||
83 | 'exceptions' => [ |
||
84 | // Overseas France |
||
85 | 'Reunion' => 0.085, |
||
86 | 'Martinique' => 0.085, |
||
87 | 'Guadeloupe' => 0.085, |
||
88 | 'Guyane' => 0, |
||
89 | 'Mayotte' => 0, |
||
90 | ], |
||
91 | ], |
||
92 | 'GB' => [ // United Kingdom |
||
93 | 'rate' => 0.20, |
||
94 | 'exceptions' => [ |
||
95 | // UK RAF Bases in Cyprus are taxed at Cyprus rate |
||
96 | 'Akrotiri' => 0.19, |
||
97 | 'Dhekelia' => 0.19, |
||
98 | ], |
||
99 | ], |
||
100 | 'GR' => [ // Greece |
||
101 | 'rate' => 0.24, |
||
102 | 'exceptions' => [ |
||
103 | 'Mount Athos' => 0, |
||
104 | ], |
||
105 | ], |
||
106 | 'HR' => [ // Croatia |
||
107 | 'rate' => 0.25, |
||
108 | ], |
||
109 | 'HU' => [ // Hungary |
||
110 | 'rate' => 0.27, |
||
111 | ], |
||
112 | 'IE' => [ // Ireland |
||
113 | 'rate' => 0.23, |
||
114 | ], |
||
115 | 'IT' => [ // Italy |
||
116 | 'rate' => 0.22, |
||
117 | 'exceptions' => [ |
||
118 | 'Campione d\'Italia' => 0, |
||
119 | 'Livigno' => 0, |
||
120 | ], |
||
121 | ], |
||
122 | 'LT' => [ // Lithuania |
||
123 | 'rate' => 0.21, |
||
124 | ], |
||
125 | 'LU' => [ // Luxembourg |
||
126 | 'rate' => 0.17, |
||
127 | ], |
||
128 | 'LV' => [ // Latvia |
||
129 | 'rate' => 0.21, |
||
130 | ], |
||
131 | 'MT' => [ // Malta |
||
132 | 'rate' => 0.18, |
||
133 | ], |
||
134 | 'NL' => [ // Netherlands |
||
135 | 'rate' => 0.21, |
||
136 | 'rates' => [ |
||
137 | 'high' => 0.21, |
||
138 | 'low' => 0.09, |
||
139 | ], |
||
140 | ], |
||
141 | 'PL' => [ // Poland |
||
142 | 'rate' => 0.23, |
||
143 | ], |
||
144 | 'PT' => [ // Portugal |
||
145 | 'rate' => 0.23, |
||
146 | 'exceptions' => [ |
||
147 | 'Azores' => 0.18, |
||
148 | 'Madeira' => 0.22, |
||
149 | ], |
||
150 | ], |
||
151 | 'RO' => [ // Romania |
||
152 | 'rate' => 0.19, |
||
153 | ], |
||
154 | 'SE' => [ // Sweden |
||
155 | 'rate' => 0.25, |
||
156 | ], |
||
157 | 'SI' => [ // Slovenia |
||
158 | 'rate' => 0.22, |
||
159 | ], |
||
160 | 'SK' => [ // Slovakia |
||
161 | 'rate' => 0.20, |
||
162 | ], |
||
163 | |||
164 | // Countries associated with EU countries that have a special VAT rate |
||
165 | 'MC' => [ // Monaco France |
||
166 | 'rate' => 0.20, |
||
167 | ], |
||
168 | 'IM' => [ // Isle of Man - United Kingdom |
||
169 | 'rate' => 0.20, |
||
170 | ], |
||
171 | |||
172 | // Non-EU with their own VAT requirements |
||
173 | 'CH' => [ // Switzerland |
||
174 | 'rate' => 0.077, |
||
175 | 'rates' => [ |
||
176 | 'high' => 0.077, |
||
177 | 'low' => 0.025, |
||
178 | ], |
||
179 | ], |
||
180 | 'TR' => [ // Turkey |
||
181 | 'rate' => 0.18, |
||
182 | ], |
||
183 | 'NO' => [ // Norway |
||
184 | 'rate' => 0.25, |
||
185 | ], |
||
186 | ]; |
||
187 | |||
188 | /** |
||
189 | * All possible postal code exceptions. |
||
190 | * |
||
191 | * @var array |
||
192 | */ |
||
193 | protected $postalCodeExceptions = [ |
||
194 | 'AT' => [ |
||
195 | [ |
||
196 | 'postalCode' => '/^6691$/', |
||
197 | 'code' => 'AT', |
||
198 | 'name' => 'Jungholz', |
||
199 | ], |
||
200 | [ |
||
201 | 'postalCode' => '/^699[123]$/', |
||
202 | 'city' => '/\bmittelberg\b/i', |
||
203 | 'code' => 'AT', |
||
204 | 'name' => 'Mittelberg', |
||
205 | ], |
||
206 | ], |
||
207 | 'CH' => [ |
||
208 | [ |
||
209 | 'postalCode' => '/^8238$/', |
||
210 | 'code' => 'DE', |
||
211 | 'name' => 'Büsingen am Hochrhein', |
||
212 | ], |
||
213 | [ |
||
214 | 'postalCode' => '/^6911$/', |
||
215 | 'code' => 'IT', |
||
216 | 'name' => "Campione d'Italia", |
||
217 | ], |
||
218 | // The Italian city of Domodossola has a Swiss post office also |
||
219 | [ |
||
220 | 'postalCode' => '/^3907$/', |
||
221 | 'code' => 'IT', |
||
222 | ], |
||
223 | ], |
||
224 | 'DE' => [ |
||
225 | [ |
||
226 | 'postalCode' => '/^87491$/', |
||
227 | 'code' => 'AT', |
||
228 | 'name' => 'Jungholz', |
||
229 | ], |
||
230 | [ |
||
231 | 'postalCode' => '/^8756[789]$/', |
||
232 | 'city' => '/\bmittelberg\b/i', |
||
233 | 'code' => 'AT', |
||
234 | 'name' => 'Mittelberg', |
||
235 | ], |
||
236 | [ |
||
237 | 'postalCode' => '/^78266$/', |
||
238 | 'code' => 'DE', |
||
239 | 'name' => 'Büsingen am Hochrhein', |
||
240 | ], |
||
241 | [ |
||
242 | 'postalCode' => '/^27498$/', |
||
243 | 'code' => 'DE', |
||
244 | 'name' => 'Heligoland', |
||
245 | ], |
||
246 | ], |
||
247 | 'ES' => [ |
||
248 | [ |
||
249 | 'postalCode' => '/^(5100[1-5]|5107[0-1]|51081)$/', |
||
250 | 'code' => 'ES', |
||
251 | 'name' => 'Ceuta', |
||
252 | ], |
||
253 | [ |
||
254 | 'postalCode' => '/^(5200[0-6]|5207[0-1]|52081)$/', |
||
255 | 'code' => 'ES', |
||
256 | 'name' => 'Melilla', |
||
257 | ], |
||
258 | [ |
||
259 | 'postalCode' => '/^(35\d{3}|38\d{3})$/', |
||
260 | 'code' => 'ES', |
||
261 | 'name' => 'Canary Islands', |
||
262 | ], |
||
263 | ], |
||
264 | 'FR' => [ |
||
265 | [ |
||
266 | 'postalCode' => '/^971\d{2,}$/', |
||
267 | 'code' => 'FR', |
||
268 | 'name' => 'Guadeloupe', |
||
269 | ], |
||
270 | [ |
||
271 | 'postalCode' => '/^972\d{2,}$/', |
||
272 | 'code' => 'FR', |
||
273 | 'name' => 'Martinique', |
||
274 | ], |
||
275 | [ |
||
276 | 'postalCode' => '/^973\d{2,}$/', |
||
277 | 'code' => 'FR', |
||
278 | 'name' => 'Guyane', |
||
279 | ], |
||
280 | [ |
||
281 | 'postalCode' => '/^974\d{2,}$/', |
||
282 | 'code' => 'FR', |
||
283 | 'name' => 'Reunion', |
||
284 | ], |
||
285 | [ |
||
286 | 'postalCode' => '/^976\d{2,}$/', |
||
287 | 'code' => 'FR', |
||
288 | 'name' => 'Mayotte', |
||
289 | ], |
||
290 | ], |
||
291 | 'GB' => [ |
||
292 | // Akrotiri |
||
293 | [ |
||
294 | 'postalCode' => '/^BFPO57|BF12AT$/', |
||
295 | 'code' => 'CY', |
||
296 | ], |
||
297 | // Dhekelia |
||
298 | [ |
||
299 | 'postalCode' => '/^BFPO58|BF12AU$/', |
||
300 | 'code' => 'CY', |
||
301 | ], |
||
302 | ], |
||
303 | 'GR' => [ |
||
304 | [ |
||
305 | 'postalCode' => '/^63086$/', |
||
306 | 'code' => 'GR', |
||
307 | 'name' => 'Mount Athos', |
||
308 | ], |
||
309 | ], |
||
310 | 'IT' => [ |
||
311 | [ |
||
312 | 'postalCode' => '/^22060$/', |
||
313 | 'city' => '/\bcampione\b/i', |
||
314 | 'code' => 'IT', |
||
315 | 'name' => "Campione d'Italia", |
||
316 | ], |
||
317 | [ |
||
318 | 'postalCode' => '/^23030$/', |
||
319 | 'city' => '/\blivigno\b/i', |
||
320 | 'code' => 'IT', |
||
321 | 'name' => 'Livigno', |
||
322 | ], |
||
323 | ], |
||
324 | 'PT' => [ |
||
325 | [ |
||
326 | 'postalCode' => '/^9[0-4]\d{2,}$/', |
||
327 | 'code' => 'PT', |
||
328 | 'name' => 'Madeira', |
||
329 | ], |
||
330 | [ |
||
331 | 'postalCode' => '/^9[5-9]\d{2,}$/', |
||
332 | 'code' => 'PT', |
||
333 | 'name' => 'Azores', |
||
334 | ], |
||
335 | ], |
||
336 | ]; |
||
337 | |||
338 | /** |
||
339 | * @var float |
||
340 | */ |
||
341 | protected $netPrice = 0.0; |
||
342 | |||
343 | /** |
||
344 | * @var string |
||
345 | */ |
||
346 | protected $countryCode; |
||
347 | |||
348 | /** |
||
349 | * @var string |
||
350 | */ |
||
351 | protected $postalCode; |
||
352 | |||
353 | /** |
||
354 | * @var Repository |
||
355 | */ |
||
356 | protected $config; |
||
357 | |||
358 | /** |
||
359 | * @var float |
||
360 | */ |
||
361 | protected $taxValue = 0; |
||
362 | |||
363 | /** |
||
364 | * @var float |
||
365 | */ |
||
366 | protected $taxRate = 0; |
||
367 | |||
368 | /** |
||
369 | * The calculate net + tax value. |
||
370 | * |
||
371 | * @var float |
||
372 | */ |
||
373 | protected $value = 0; |
||
374 | |||
375 | /** |
||
376 | * @var bool |
||
377 | */ |
||
378 | protected $company = false; |
||
379 | |||
380 | /** |
||
381 | * @var string |
||
382 | */ |
||
383 | protected $businessCountryCode; |
||
384 | |||
385 | /** |
||
386 | * @param \Illuminate\Contracts\Config\Repository |
||
387 | */ |
||
388 | public function __construct($config = null) |
||
397 | |||
398 | /** |
||
399 | * Finds the client IP address. |
||
400 | * |
||
401 | * @return mixed |
||
402 | */ |
||
403 | private function getClientIP() |
||
415 | |||
416 | /** |
||
417 | * Returns the ISO 3166-1 alpha-2 two letter |
||
418 | * country code for the client IP. If the |
||
419 | * IP can't be resolved it returns false. |
||
420 | * |
||
421 | * @return bool|string |
||
422 | */ |
||
423 | public function getIPBasedCountry() |
||
440 | |||
441 | /** |
||
442 | * Determines if you need to collect VAT for the given country code. |
||
443 | * |
||
444 | * @param $countryCode |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function shouldCollectVAT($countryCode) |
||
454 | |||
455 | /** |
||
456 | * Calculate the VAT based on the net price, country code and indication if the |
||
457 | * customer is a company or not. |
||
458 | * |
||
459 | * @param int|float $netPrice The net price to use for the calculation |
||
460 | * @param null|string $countryCode The country code to use for the rate lookup |
||
461 | * @param null|string $postalCode The postal code to use for the rate exception lookup |
||
462 | * @param null|bool $company |
||
463 | * @param null|string $type The type can be low or high |
||
464 | * |
||
465 | * @return float |
||
466 | */ |
||
467 | public function calculate($netPrice, $countryCode = null, $postalCode = null, $company = null, $type = null) |
||
485 | |||
486 | /** |
||
487 | * Calculate the net price on the gross price, country code and indication if the |
||
488 | * customer is a company or not. |
||
489 | * |
||
490 | * @param int|float $gross The gross price to use for the calculation |
||
491 | * @param null|string $countryCode The country code to use for the rate lookup |
||
492 | * @param null|string $postalCode The postal code to use for the rate exception lookup |
||
493 | * @param null|bool $company |
||
494 | * @param null|string $type The type can be low or high |
||
495 | * |
||
496 | * @return float |
||
497 | */ |
||
498 | public function calculateNet($gross, $countryCode = null, $postalCode = null, $company = null, $type = null) |
||
517 | |||
518 | /** |
||
519 | * @return float |
||
520 | */ |
||
521 | public function getNetPrice() |
||
525 | |||
526 | /** |
||
527 | * @return string |
||
528 | */ |
||
529 | public function getCountryCode() |
||
533 | |||
534 | /** |
||
535 | * @param mixed $countryCode |
||
536 | */ |
||
537 | public function setCountryCode($countryCode) |
||
541 | |||
542 | /** |
||
543 | * @return string |
||
544 | */ |
||
545 | public function getPostalCode() |
||
549 | |||
550 | /** |
||
551 | * @param mixed $postalCode |
||
552 | */ |
||
553 | public function setPostalCode($postalCode) |
||
557 | |||
558 | /** |
||
559 | * @return float |
||
560 | */ |
||
561 | public function getTaxRate() |
||
565 | |||
566 | /** |
||
567 | * @return bool |
||
568 | */ |
||
569 | public function isCompany() |
||
573 | |||
574 | /** |
||
575 | * @param bool $company |
||
576 | */ |
||
577 | public function setCompany($company) |
||
581 | |||
582 | /** |
||
583 | * @param string $businessCountryCode |
||
584 | */ |
||
585 | public function setBusinessCountryCode($businessCountryCode) |
||
589 | |||
590 | /** |
||
591 | * Returns the tax rate for the given country code. |
||
592 | * This method is used to allow backwards compatibility. |
||
593 | * |
||
594 | * @param $countryCode |
||
595 | * @param bool $company |
||
596 | * @param string $type |
||
597 | * |
||
598 | * @return float |
||
599 | */ |
||
600 | public function getTaxRateForCountry($countryCode, $company = false, $type = null) |
||
604 | |||
605 | /** |
||
606 | * Returns the tax rate for the given country code. |
||
607 | * If a postal code is provided, it will try to lookup the different |
||
608 | * postal code exceptions that are possible. |
||
609 | * |
||
610 | * @param string $countryCode |
||
611 | * @param string|null $postalCode |
||
612 | * @param bool|false $company |
||
613 | * @param string|null $type |
||
614 | * |
||
615 | * @return float |
||
616 | */ |
||
617 | public function getTaxRateForLocation($countryCode, $postalCode = null, $company = false, $type = null) |
||
646 | |||
647 | /** |
||
648 | * @return float |
||
649 | */ |
||
650 | public function getTaxValue() |
||
654 | |||
655 | /** |
||
656 | * @param $vatNumber |
||
657 | * |
||
658 | * @throws VATCheckUnavailableException |
||
659 | * |
||
660 | * @return bool |
||
661 | */ |
||
662 | public function isValidVATNumber($vatNumber) |
||
672 | |||
673 | /** |
||
674 | * @param $vatNumber |
||
675 | * |
||
676 | * @throws VATCheckUnavailableException |
||
677 | * |
||
678 | * @return object|false |
||
679 | */ |
||
680 | public function getVATDetails($vatNumber) |
||
704 | |||
705 | /** |
||
706 | * @throws VATCheckUnavailableException |
||
707 | * |
||
708 | * @return void |
||
709 | */ |
||
710 | public function initSoapClient() |
||
725 | |||
726 | /** |
||
727 | * @param SoapClient $soapClient |
||
728 | */ |
||
729 | public function setSoapClient($soapClient) |
||
733 | } |
||
734 |