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 = 'http://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 = 'http://ip2c.org/'; |
||
21 | |||
22 | protected $soapClient; |
||
23 | |||
24 | /** |
||
25 | * All available tax rules and their exceptions. |
||
26 | * |
||
27 | * Taken from: http://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 | 'since' => [ |
||
54 | '2021-01-01 00:00:00 Europe/Berlin' => [ |
||
55 | 'rate' => 0.19, |
||
56 | ], |
||
57 | '2020-07-01 00:00:00 Europe/Berlin' => [ |
||
58 | 'rate' => 0.16, |
||
59 | ], |
||
60 | ], |
||
61 | 'exceptions' => [ |
||
62 | 'Heligoland' => 0, |
||
63 | 'Büsingen am Hochrhein' => 0, |
||
64 | ], |
||
65 | ], |
||
66 | 'DK' => [ // Denmark |
||
67 | 'rate' => 0.25, |
||
68 | ], |
||
69 | 'EE' => [ // Estonia |
||
70 | 'rate' => 0.20, |
||
71 | ], |
||
72 | 'EL' => [ // Hellenic Republic (Greece) |
||
73 | 'rate' => 0.24, |
||
74 | 'exceptions' => [ |
||
75 | 'Mount Athos' => 0, |
||
76 | ], |
||
77 | ], |
||
78 | 'ES' => [ // Spain |
||
79 | 'rate' => 0.21, |
||
80 | 'exceptions' => [ |
||
81 | 'Canary Islands' => 0, |
||
82 | 'Ceuta' => 0, |
||
83 | 'Melilla' => 0, |
||
84 | ], |
||
85 | ], |
||
86 | 'FI' => [ // Finland |
||
87 | 'rate' => 0.24, |
||
88 | ], |
||
89 | 'FR' => [ // France |
||
90 | 'rate' => 0.20, |
||
91 | 'exceptions' => [ |
||
92 | // Overseas France |
||
93 | 'Reunion' => 0.085, |
||
94 | 'Martinique' => 0.085, |
||
95 | 'Guadeloupe' => 0.085, |
||
96 | 'Guyane' => 0, |
||
97 | 'Mayotte' => 0, |
||
98 | ], |
||
99 | ], |
||
100 | 'GB' => [ // United Kingdom |
||
101 | 'rate' => 0.20, |
||
102 | 'exceptions' => [ |
||
103 | // UK RAF Bases in Cyprus are taxed at Cyprus rate |
||
104 | 'Akrotiri' => 0.19, |
||
105 | 'Dhekelia' => 0.19, |
||
106 | ], |
||
107 | ], |
||
108 | 'GR' => [ // Greece |
||
109 | 'rate' => 0.24, |
||
110 | 'exceptions' => [ |
||
111 | 'Mount Athos' => 0, |
||
112 | ], |
||
113 | ], |
||
114 | 'HR' => [ // Croatia |
||
115 | 'rate' => 0.25, |
||
116 | ], |
||
117 | 'HU' => [ // Hungary |
||
118 | 'rate' => 0.27, |
||
119 | ], |
||
120 | 'IE' => [ // Ireland |
||
121 | 'rate' => 0.23, |
||
122 | ], |
||
123 | 'IT' => [ // Italy |
||
124 | 'rate' => 0.22, |
||
125 | 'exceptions' => [ |
||
126 | 'Campione d\'Italia' => 0, |
||
127 | 'Livigno' => 0, |
||
128 | ], |
||
129 | ], |
||
130 | 'LT' => [ // Lithuania |
||
131 | 'rate' => 0.21, |
||
132 | ], |
||
133 | 'LU' => [ // Luxembourg |
||
134 | 'rate' => 0.17, |
||
135 | ], |
||
136 | 'LV' => [ // Latvia |
||
137 | 'rate' => 0.21, |
||
138 | ], |
||
139 | 'MT' => [ // Malta |
||
140 | 'rate' => 0.18, |
||
141 | ], |
||
142 | 'NL' => [ // Netherlands |
||
143 | 'rate' => 0.21, |
||
144 | 'rates' => [ |
||
145 | 'high' => 0.21, |
||
146 | 'low' => 0.09, |
||
147 | ], |
||
148 | ], |
||
149 | 'PL' => [ // Poland |
||
150 | 'rate' => 0.23, |
||
151 | ], |
||
152 | 'PT' => [ // Portugal |
||
153 | 'rate' => 0.23, |
||
154 | 'exceptions' => [ |
||
155 | 'Azores' => 0.18, |
||
156 | 'Madeira' => 0.22, |
||
157 | ], |
||
158 | ], |
||
159 | 'RO' => [ // Romania |
||
160 | 'rate' => 0.19, |
||
161 | ], |
||
162 | 'SE' => [ // Sweden |
||
163 | 'rate' => 0.25, |
||
164 | ], |
||
165 | 'SI' => [ // Slovenia |
||
166 | 'rate' => 0.22, |
||
167 | ], |
||
168 | 'SK' => [ // Slovakia |
||
169 | 'rate' => 0.20, |
||
170 | ], |
||
171 | |||
172 | // Countries associated with EU countries that have a special VAT rate |
||
173 | 'MC' => [ // Monaco France |
||
174 | 'rate' => 0.20, |
||
175 | ], |
||
176 | 'IM' => [ // Isle of Man - United Kingdom |
||
177 | 'rate' => 0.20, |
||
178 | ], |
||
179 | |||
180 | // Non-EU with their own VAT requirements |
||
181 | 'CH' => [ // Switzerland |
||
182 | 'rate' => 0.077, |
||
183 | 'rates' => [ |
||
184 | 'high' => 0.077, |
||
185 | 'low' => 0.025, |
||
186 | ], |
||
187 | ], |
||
188 | 'TR' => [ // Turkey |
||
189 | 'rate' => 0.18, |
||
190 | ], |
||
191 | 'NO' => [ // Norway |
||
192 | 'rate' => 0.25, |
||
193 | ], |
||
194 | ]; |
||
195 | |||
196 | /** |
||
197 | * All possible postal code exceptions. |
||
198 | * |
||
199 | * @var array |
||
200 | */ |
||
201 | protected $postalCodeExceptions = [ |
||
202 | 'AT' => [ |
||
203 | [ |
||
204 | 'postalCode' => '/^6691$/', |
||
205 | 'code' => 'AT', |
||
206 | 'name' => 'Jungholz', |
||
207 | ], |
||
208 | [ |
||
209 | 'postalCode' => '/^699[123]$/', |
||
210 | 'city' => '/\bmittelberg\b/i', |
||
211 | 'code' => 'AT', |
||
212 | 'name' => 'Mittelberg', |
||
213 | ], |
||
214 | ], |
||
215 | 'CH' => [ |
||
216 | [ |
||
217 | 'postalCode' => '/^8238$/', |
||
218 | 'code' => 'DE', |
||
219 | 'name' => 'Büsingen am Hochrhein', |
||
220 | ], |
||
221 | [ |
||
222 | 'postalCode' => '/^6911$/', |
||
223 | 'code' => 'IT', |
||
224 | 'name' => "Campione d'Italia", |
||
225 | ], |
||
226 | // The Italian city of Domodossola has a Swiss post office also |
||
227 | [ |
||
228 | 'postalCode' => '/^3907$/', |
||
229 | 'code' => 'IT', |
||
230 | ], |
||
231 | ], |
||
232 | 'DE' => [ |
||
233 | [ |
||
234 | 'postalCode' => '/^87491$/', |
||
235 | 'code' => 'AT', |
||
236 | 'name' => 'Jungholz', |
||
237 | ], |
||
238 | [ |
||
239 | 'postalCode' => '/^8756[789]$/', |
||
240 | 'city' => '/\bmittelberg\b/i', |
||
241 | 'code' => 'AT', |
||
242 | 'name' => 'Mittelberg', |
||
243 | ], |
||
244 | [ |
||
245 | 'postalCode' => '/^78266$/', |
||
246 | 'code' => 'DE', |
||
247 | 'name' => 'Büsingen am Hochrhein', |
||
248 | ], |
||
249 | [ |
||
250 | 'postalCode' => '/^27498$/', |
||
251 | 'code' => 'DE', |
||
252 | 'name' => 'Heligoland', |
||
253 | ], |
||
254 | ], |
||
255 | 'ES' => [ |
||
256 | [ |
||
257 | 'postalCode' => '/^(5100[1-5]|5107[0-1]|51081)$/', |
||
258 | 'code' => 'ES', |
||
259 | 'name' => 'Ceuta', |
||
260 | ], |
||
261 | [ |
||
262 | 'postalCode' => '/^(5200[0-6]|5207[0-1]|52081)$/', |
||
263 | 'code' => 'ES', |
||
264 | 'name' => 'Melilla', |
||
265 | ], |
||
266 | [ |
||
267 | 'postalCode' => '/^(35\d{3}|38\d{3})$/', |
||
268 | 'code' => 'ES', |
||
269 | 'name' => 'Canary Islands', |
||
270 | ], |
||
271 | ], |
||
272 | 'FR' => [ |
||
273 | [ |
||
274 | 'postalCode' => '/^971\d{2,}$/', |
||
275 | 'code' => 'FR', |
||
276 | 'name' => 'Guadeloupe', |
||
277 | ], |
||
278 | [ |
||
279 | 'postalCode' => '/^972\d{2,}$/', |
||
280 | 'code' => 'FR', |
||
281 | 'name' => 'Martinique', |
||
282 | ], |
||
283 | [ |
||
284 | 'postalCode' => '/^973\d{2,}$/', |
||
285 | 'code' => 'FR', |
||
286 | 'name' => 'Guyane', |
||
287 | ], |
||
288 | [ |
||
289 | 'postalCode' => '/^974\d{2,}$/', |
||
290 | 'code' => 'FR', |
||
291 | 'name' => 'Reunion', |
||
292 | ], |
||
293 | [ |
||
294 | 'postalCode' => '/^976\d{2,}$/', |
||
295 | 'code' => 'FR', |
||
296 | 'name' => 'Mayotte', |
||
297 | ], |
||
298 | ], |
||
299 | 'GB' => [ |
||
300 | // Akrotiri |
||
301 | [ |
||
302 | 'postalCode' => '/^BFPO57|BF12AT$/', |
||
303 | 'code' => 'CY', |
||
304 | ], |
||
305 | // Dhekelia |
||
306 | [ |
||
307 | 'postalCode' => '/^BFPO58|BF12AU$/', |
||
308 | 'code' => 'CY', |
||
309 | ], |
||
310 | ], |
||
311 | 'GR' => [ |
||
312 | [ |
||
313 | 'postalCode' => '/^63086$/', |
||
314 | 'code' => 'GR', |
||
315 | 'name' => 'Mount Athos', |
||
316 | ], |
||
317 | ], |
||
318 | 'IT' => [ |
||
319 | [ |
||
320 | 'postalCode' => '/^22060$/', |
||
321 | 'city' => '/\bcampione\b/i', |
||
322 | 'code' => 'IT', |
||
323 | 'name' => "Campione d'Italia", |
||
324 | ], |
||
325 | [ |
||
326 | 'postalCode' => '/^23030$/', |
||
327 | 'city' => '/\blivigno\b/i', |
||
328 | 'code' => 'IT', |
||
329 | 'name' => 'Livigno', |
||
330 | ], |
||
331 | ], |
||
332 | 'PT' => [ |
||
333 | [ |
||
334 | 'postalCode' => '/^9[0-4]\d{2,}$/', |
||
335 | 'code' => 'PT', |
||
336 | 'name' => 'Madeira', |
||
337 | ], |
||
338 | [ |
||
339 | 'postalCode' => '/^9[5-9]\d{2,}$/', |
||
340 | 'code' => 'PT', |
||
341 | 'name' => 'Azores', |
||
342 | ], |
||
343 | ], |
||
344 | ]; |
||
345 | |||
346 | /** |
||
347 | * @var float |
||
348 | */ |
||
349 | protected $netPrice = 0.0; |
||
350 | |||
351 | /** |
||
352 | * @var string |
||
353 | */ |
||
354 | protected $countryCode; |
||
355 | |||
356 | /** |
||
357 | * @var string |
||
358 | */ |
||
359 | protected $postalCode; |
||
360 | |||
361 | /** |
||
362 | * @var Repository |
||
363 | */ |
||
364 | protected $config; |
||
365 | |||
366 | /** |
||
367 | * @var float |
||
368 | */ |
||
369 | protected $taxValue = 0; |
||
370 | |||
371 | /** |
||
372 | * @var float |
||
373 | */ |
||
374 | protected $taxRate = 0; |
||
375 | |||
376 | /** |
||
377 | * The calculate net + tax value. |
||
378 | * |
||
379 | * @var float |
||
380 | */ |
||
381 | protected $value = 0; |
||
382 | |||
383 | /** |
||
384 | * @var bool |
||
385 | */ |
||
386 | protected $company = false; |
||
387 | |||
388 | /** |
||
389 | * @var string |
||
390 | */ |
||
391 | protected $businessCountryCode; |
||
392 | |||
393 | /** |
||
394 | * @var \DateTimeImmutable |
||
395 | */ |
||
396 | private $now; |
||
397 | |||
398 | /** |
||
399 | * @param \Illuminate\Contracts\Config\Repository |
||
400 | */ |
||
401 | public function __construct($config = null) |
||
412 | |||
413 | /** |
||
414 | * Finds the client IP address. |
||
415 | * |
||
416 | * @return mixed |
||
417 | */ |
||
418 | private function getClientIP() |
||
430 | |||
431 | /** |
||
432 | * Returns the ISO 3166-1 alpha-2 two letter |
||
433 | * country code for the client IP. If the |
||
434 | * IP can't be resolved it returns false. |
||
435 | * |
||
436 | * @return bool|string |
||
437 | */ |
||
438 | public function getIPBasedCountry() |
||
453 | |||
454 | /** |
||
455 | * Determines if you need to collect VAT for the given country code. |
||
456 | * |
||
457 | * @param $countryCode |
||
458 | * |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function shouldCollectVAT($countryCode) |
||
467 | |||
468 | /** |
||
469 | * Calculate the VAT based on the net price, country code and indication if the |
||
470 | * customer is a company or not. |
||
471 | * |
||
472 | * @param int|float $netPrice The net price to use for the calculation |
||
473 | * @param null|string $countryCode The country code to use for the rate lookup |
||
474 | * @param null|string $postalCode The postal code to use for the rate exception lookup |
||
475 | * @param null|bool $company |
||
476 | * @param null|string $type The type can be low or high |
||
477 | * @param \DateTimeInterface|null $date Date to use the VAT rate for, null for current date |
||
478 | * |
||
479 | * @return float |
||
480 | */ |
||
481 | public function calculate($netPrice, $countryCode = null, $postalCode = null, $company = null, $type = null, $date = null) |
||
499 | |||
500 | /** |
||
501 | * Calculate the net price on the gross price, country code and indication if the |
||
502 | * customer is a company or not. |
||
503 | * |
||
504 | * @param int|float $gross The gross price to use for the calculation |
||
505 | * @param null|string $countryCode The country code to use for the rate lookup |
||
506 | * @param null|string $postalCode The postal code to use for the rate exception lookup |
||
507 | * @param null|bool $company |
||
508 | * @param null|string $type The type can be low or high |
||
509 | * @param \DateTimeInterface|null $date Date to use the VAT rate for, null for current date |
||
510 | * |
||
511 | * @return float |
||
512 | */ |
||
513 | public function calculateNet($gross, $countryCode = null, $postalCode = null, $company = null, $type = null, $date = null) |
||
532 | |||
533 | /** |
||
534 | * @return float |
||
535 | */ |
||
536 | public function getNetPrice() |
||
540 | |||
541 | /** |
||
542 | * @return string |
||
543 | */ |
||
544 | public function getCountryCode() |
||
548 | |||
549 | /** |
||
550 | * @param mixed $countryCode |
||
551 | */ |
||
552 | public function setCountryCode($countryCode) |
||
556 | |||
557 | /** |
||
558 | * @return string |
||
559 | */ |
||
560 | public function getPostalCode() |
||
564 | |||
565 | /** |
||
566 | * @param mixed $postalCode |
||
567 | */ |
||
568 | public function setPostalCode($postalCode) |
||
572 | |||
573 | /** |
||
574 | * @return float |
||
575 | */ |
||
576 | public function getTaxRate() |
||
580 | |||
581 | /** |
||
582 | * @return bool |
||
583 | */ |
||
584 | public function isCompany() |
||
588 | |||
589 | /** |
||
590 | * @param bool $company |
||
591 | */ |
||
592 | public function setCompany($company) |
||
596 | |||
597 | /** |
||
598 | * @param string $businessCountryCode |
||
599 | */ |
||
600 | public function setBusinessCountryCode($businessCountryCode) |
||
604 | |||
605 | /** |
||
606 | * Returns the tax rate for the given country code. |
||
607 | * This method is used to allow backwards compatibility. |
||
608 | * |
||
609 | * @param $countryCode |
||
610 | * @param bool $company |
||
611 | * @param string $type |
||
612 | * |
||
613 | * @return float |
||
614 | */ |
||
615 | public function getTaxRateForCountry($countryCode, $company = false, $type = null) |
||
619 | |||
620 | /** |
||
621 | * Returns the tax rate for the given country code. |
||
622 | * If a postal code is provided, it will try to lookup the different |
||
623 | * postal code exceptions that are possible. |
||
624 | * |
||
625 | * @param string $countryCode |
||
626 | * @param string|null $postalCode |
||
627 | * @param bool|false $company |
||
628 | * @param string|null $type |
||
629 | * @param \DateTimeInterface|null $date Date to use the VAT rate for, null for current date |
||
630 | * |
||
631 | * @return float |
||
632 | */ |
||
633 | public function getTaxRateForLocation($countryCode, $postalCode = null, $company = false, $type = null, $date = null) |
||
662 | |||
663 | private function getRules($countryCode, $date): array |
||
677 | |||
678 | /** |
||
679 | * @return float |
||
680 | */ |
||
681 | public function getTaxValue() |
||
685 | |||
686 | /** |
||
687 | * @param $vatNumber |
||
688 | * |
||
689 | * @throws VATCheckUnavailableException |
||
690 | * |
||
691 | * @return bool |
||
692 | */ |
||
693 | public function isValidVATNumber($vatNumber) |
||
703 | |||
704 | /** |
||
705 | * @param $vatNumber |
||
706 | * |
||
707 | * @throws VATCheckUnavailableException |
||
708 | * |
||
709 | * @return object|false |
||
710 | */ |
||
711 | public function getVATDetails($vatNumber) |
||
735 | |||
736 | /** |
||
737 | * @throws VATCheckUnavailableException |
||
738 | * |
||
739 | * @return void |
||
740 | */ |
||
741 | public function initSoapClient() |
||
756 | |||
757 | /** |
||
758 | * @param SoapClient $soapClient |
||
759 | */ |
||
760 | public function setSoapClient($soapClient) |
||
764 | } |
||
765 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.