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 |
||
7 | class Validator |
||
8 | { |
||
9 | protected $params; |
||
10 | |||
11 | protected $states = [ |
||
12 | "usa" => [ |
||
13 | ["abbr" => 'AL', "name" => 'Alabama'], |
||
14 | ["abbr" => 'AK', "name" => 'Alaska'], |
||
15 | ["abbr" => 'AZ', "name" => 'Arizona'], |
||
16 | ["abbr" => 'AR', "name" => 'Arkansas'], |
||
17 | ["abbr" => 'CA', "name" => 'California'], |
||
18 | ["abbr" => 'CO', "name" => 'Colorado'], |
||
19 | ["abbr" => 'CT', "name" => 'Connecticut'], |
||
20 | ["abbr" => 'DC', "name" => 'District Of Columbia'], |
||
21 | ["abbr" => 'DE', "name" => 'Delaware'], |
||
22 | ["abbr" => 'FL', "name" => 'Florida'], |
||
23 | ["abbr" => 'GA', "name" => 'Georgia'], |
||
24 | ["abbr" => 'HI', "name" => 'Hawaii'], |
||
25 | ["abbr" => 'ID', "name" => 'Idaho'], |
||
26 | ["abbr" => 'IL', "name" => 'Illinois'], |
||
27 | ["abbr" => 'IN', "name" => 'Indiana'], |
||
28 | ["abbr" => 'IA', "name" => 'Iowa'], |
||
29 | ["abbr" => 'KS', "name" => 'Kansas'], |
||
30 | ["abbr" => 'KY', "name" => 'Kentucky'], |
||
31 | ["abbr" => 'LA', "name" => 'Louisiana'], |
||
32 | ["abbr" => 'ME', "name" => 'Maine'], |
||
33 | ["abbr" => 'MD', "name" => 'Maryland'], |
||
34 | ["abbr" => 'MA', "name" => 'Massachusetts'], |
||
35 | ["abbr" => 'MI', "name" => 'Michigan'], |
||
36 | ["abbr" => 'MN', "name" => 'Minnesota'], |
||
37 | ["abbr" => 'MS', "name" => 'Mississippi'], |
||
38 | ["abbr" => 'MO', "name" => 'Missouri'], |
||
39 | ["abbr" => 'MT', "name" => 'Montana'], |
||
40 | ["abbr" => 'NE', "name" => 'Nebraska'], |
||
41 | ["abbr" => 'NV', "name" => 'Nevada'], |
||
42 | ["abbr" => 'NH', "name" => 'New Hampshire'], |
||
43 | ["abbr" => 'NJ', "name" => 'New Jersey'], |
||
44 | ["abbr" => 'NM', "name" => 'New Mexico'], |
||
45 | ["abbr" => 'NY', "name" => 'New York'], |
||
46 | ["abbr" => 'NC', "name" => 'North Carolina'], |
||
47 | ["abbr" => 'ND', "name" => 'North Dakota'], |
||
48 | ["abbr" => 'OH', "name" => 'Ohio'], |
||
49 | ["abbr" => 'OK', "name" => 'Oklahoma'], |
||
50 | ["abbr" => 'OR', "name" => 'Oregon'], |
||
51 | ["abbr" => 'PA', "name" => 'Pennsylvania'], |
||
52 | ["abbr" => 'RI', "name" => 'Rhode Island'], |
||
53 | ["abbr" => 'SC', "name" => 'South Carolina'], |
||
54 | ["abbr" => 'SD', "name" => 'South Dakota'], |
||
55 | ["abbr" => 'TN', "name" => 'Tennessee'], |
||
56 | ["abbr" => 'TX', "name" => 'Texas'], |
||
57 | ["abbr" => 'UT', "name" => 'Utah'], |
||
58 | ["abbr" => 'VT', "name" => 'Vermont'], |
||
59 | ["abbr" => 'VA', "name" => 'Virginia'], |
||
60 | ["abbr" => 'WA', "name" => 'Washington'], |
||
61 | ["abbr" => 'WV', "name" => 'West Virginia'], |
||
62 | ["abbr" => 'WI', "name" => 'Wisconsin'], |
||
63 | ["abbr" => 'WY', "name" => 'Wyoming'], |
||
64 | ["abbr" => 'AS', "name" => 'American Samoa'], |
||
65 | ["abbr" => 'FM', "name" => 'Federated States Of Micronesia'], |
||
66 | ["abbr" => 'GU', "name" => 'Guam'], |
||
67 | ["abbr" => 'MH', "name" => 'Marshall Islands'], |
||
68 | ["abbr" => 'MP', "name" => 'Northern Mariana Islands'], |
||
69 | ["abbr" => 'PW', "name" => 'Pala'], |
||
70 | ["abbr" => 'PR', "name" => 'Puerto Rico'], |
||
71 | ["abbr" => 'VI', "name" => 'Virgin Islands'] |
||
72 | ], |
||
73 | "canada" => [ |
||
74 | ["abbr" => 'AB', "name" => 'Alberta'], |
||
75 | ["abbr" => 'BC', "name" => 'British Columbia'], |
||
76 | ["abbr" => 'MB', "name" => 'Manitoba'], |
||
77 | ["abbr" => 'NB', "name" => 'New Brunswick'], |
||
78 | ["abbr" => 'NL', "name" => 'Newfoundland And Labrador'], |
||
79 | ["abbr" => 'NS', "name" => 'Nova Scotia'], |
||
80 | ["abbr" => 'NT', "name" => 'Northwest Territories'], |
||
81 | ["abbr" => 'NU', "name" => 'Nunavut'], |
||
82 | ["abbr" => 'ON', "name" => 'Ontario'], |
||
83 | ["abbr" => 'PE', "name" => 'Prince Edward Island'], |
||
84 | ["abbr" => 'QC', "name" => 'Quebec'], |
||
85 | ["abbr" => 'SK', "name" => 'Saskatchewan'], |
||
86 | ["abbr" => 'YT', "name" => 'Yukon'], |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | 30 | public function __construct(Parameters $params) |
|
94 | |||
95 | 27 | public function validate($value) |
|
96 | { |
||
97 | 27 | return $value === null || $value === [] || ( |
|
98 | 27 | is_string($value) && |
|
99 | 27 | Str::length($value) > 0 && |
|
100 | 27 | $this->validateCountry($value) && |
|
101 | 27 | $this->validateCase($value) && |
|
102 | 24 | $this->validateType($value) |
|
103 | 27 | ); |
|
104 | } |
||
105 | |||
106 | 24 | protected function validateCountry($value) |
|
111 | |||
112 | 24 | protected function validateCase($value) |
|
128 | |||
129 | 24 | protected function validateType($value) |
|
140 | |||
141 | 24 | protected function isFull($value, $country = null) |
|
145 | |||
146 | 24 | protected function isAbbr($value, $country = null) |
|
150 | |||
151 | 24 | View Code Duplication | protected function getStateAbbreviations($country = null) |
163 | |||
164 | 24 | View Code Duplication | protected function getStateNames($country = null) |
176 | } |
||
177 |
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.