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 |
||
10 | abstract class AbstractGermanyPlate |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $plate; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $prefix; |
||
21 | |||
22 | /** |
||
23 | * @var string[] |
||
24 | */ |
||
25 | protected static $name = [ |
||
26 | '', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @var int[] |
||
31 | */ |
||
32 | protected static $type = [ |
||
33 | GermanyDetector::PLATE_TYPE_DEFAULT, |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * @var string[] |
||
38 | */ |
||
39 | protected static $regexes = [ |
||
40 | '/^(?:[A-ZÄÖÜ]{1,2} [A-Z]{1,2} [1-9]\d{0,3}[EH]?|[A-ZÄÖÜ]{3} (?:[A-Z] [1-9]\d{0,3}[EH]?|[A-Z]{2} [1-9]\d{0,2}[EH]?))$/', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @var string[] |
||
45 | */ |
||
46 | protected static $globalName = [ |
||
47 | 'Konsularische Korps', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var int[] |
||
52 | */ |
||
53 | protected static $globalType = [ |
||
54 | GermanyDetector::PLATE_TYPE_DIPLOMATIC_CORPS, |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * @var string[] |
||
59 | */ |
||
60 | protected static $globalRegex = [ |
||
61 | '/^[A-ZÄÖÜ]{1,2} 9\d{2,4}$/', |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * AbstractGermanyPlate constructor. |
||
66 | * |
||
67 | * @param string $plate |
||
68 | */ |
||
69 | public function __construct($plate) |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | View Code Duplication | public function getDescription() |
|
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | View Code Duplication | public function getType() |
|
105 | |||
106 | /** |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function isValid() |
||
110 | { |
||
111 | return $this->getIndex() !== false; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return int|bool |
||
116 | */ |
||
117 | protected function getIndex() |
||
127 | } |
||
128 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.