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 |
||
9 | abstract class Base implements Rule |
||
10 | { |
||
11 | protected $country; |
||
12 | protected $subject; |
||
13 | |||
14 | 9 | function __construct(string $country = null) |
|
29 | |||
30 | 9 | protected function isFull($value, string $country = null): bool |
|
34 | |||
35 | 9 | protected function isAbbr($value, string $country = null): bool |
|
39 | |||
40 | protected $states = [ |
||
41 | "US" => [ |
||
42 | ["abbr" => 'AL', "name" => 'Alabama'], |
||
43 | ["abbr" => 'AK', "name" => 'Alaska'], |
||
44 | ["abbr" => 'AZ', "name" => 'Arizona'], |
||
45 | ["abbr" => 'AR', "name" => 'Arkansas'], |
||
46 | ["abbr" => 'CA', "name" => 'California'], |
||
47 | ["abbr" => 'CO', "name" => 'Colorado'], |
||
48 | ["abbr" => 'CT', "name" => 'Connecticut'], |
||
49 | ["abbr" => 'DC', "name" => 'District Of Columbia'], |
||
50 | ["abbr" => 'DE', "name" => 'Delaware'], |
||
51 | ["abbr" => 'FL', "name" => 'Florida'], |
||
52 | ["abbr" => 'GA', "name" => 'Georgia'], |
||
53 | ["abbr" => 'HI', "name" => 'Hawaii'], |
||
54 | ["abbr" => 'ID', "name" => 'Idaho'], |
||
55 | ["abbr" => 'IL', "name" => 'Illinois'], |
||
56 | ["abbr" => 'IN', "name" => 'Indiana'], |
||
57 | ["abbr" => 'IA', "name" => 'Iowa'], |
||
58 | ["abbr" => 'KS', "name" => 'Kansas'], |
||
59 | ["abbr" => 'KY', "name" => 'Kentucky'], |
||
60 | ["abbr" => 'LA', "name" => 'Louisiana'], |
||
61 | ["abbr" => 'ME', "name" => 'Maine'], |
||
62 | ["abbr" => 'MD', "name" => 'Maryland'], |
||
63 | ["abbr" => 'MA', "name" => 'Massachusetts'], |
||
64 | ["abbr" => 'MI', "name" => 'Michigan'], |
||
65 | ["abbr" => 'MN', "name" => 'Minnesota'], |
||
66 | ["abbr" => 'MS', "name" => 'Mississippi'], |
||
67 | ["abbr" => 'MO', "name" => 'Missouri'], |
||
68 | ["abbr" => 'MT', "name" => 'Montana'], |
||
69 | ["abbr" => 'NE', "name" => 'Nebraska'], |
||
70 | ["abbr" => 'NV', "name" => 'Nevada'], |
||
71 | ["abbr" => 'NH', "name" => 'New Hampshire'], |
||
72 | ["abbr" => 'NJ', "name" => 'New Jersey'], |
||
73 | ["abbr" => 'NM', "name" => 'New Mexico'], |
||
74 | ["abbr" => 'NY', "name" => 'New York'], |
||
75 | ["abbr" => 'NC', "name" => 'North Carolina'], |
||
76 | ["abbr" => 'ND', "name" => 'North Dakota'], |
||
77 | ["abbr" => 'OH', "name" => 'Ohio'], |
||
78 | ["abbr" => 'OK', "name" => 'Oklahoma'], |
||
79 | ["abbr" => 'OR', "name" => 'Oregon'], |
||
80 | ["abbr" => 'PA', "name" => 'Pennsylvania'], |
||
81 | ["abbr" => 'RI', "name" => 'Rhode Island'], |
||
82 | ["abbr" => 'SC', "name" => 'South Carolina'], |
||
83 | ["abbr" => 'SD', "name" => 'South Dakota'], |
||
84 | ["abbr" => 'TN', "name" => 'Tennessee'], |
||
85 | ["abbr" => 'TX', "name" => 'Texas'], |
||
86 | ["abbr" => 'UT', "name" => 'Utah'], |
||
87 | ["abbr" => 'VT', "name" => 'Vermont'], |
||
88 | ["abbr" => 'VA', "name" => 'Virginia'], |
||
89 | ["abbr" => 'WA', "name" => 'Washington'], |
||
90 | ["abbr" => 'WV', "name" => 'West Virginia'], |
||
91 | ["abbr" => 'WI', "name" => 'Wisconsin'], |
||
92 | ["abbr" => 'WY', "name" => 'Wyoming'], |
||
93 | ["abbr" => 'AS', "name" => 'American Samoa'], |
||
94 | ["abbr" => 'FM', "name" => 'Federated States Of Micronesia'], |
||
95 | ["abbr" => 'GU', "name" => 'Guam'], |
||
96 | ["abbr" => 'MH', "name" => 'Marshall Islands'], |
||
97 | ["abbr" => 'MP', "name" => 'Northern Mariana Islands'], |
||
98 | ["abbr" => 'PW', "name" => 'Pala'], |
||
99 | ["abbr" => 'PR', "name" => 'Puerto Rico'], |
||
100 | ["abbr" => 'VI', "name" => 'Virgin Islands'] |
||
101 | ], |
||
102 | "CA" => [ |
||
103 | ["abbr" => 'AB', "name" => 'Alberta'], |
||
104 | ["abbr" => 'BC', "name" => 'British Columbia'], |
||
105 | ["abbr" => 'MB', "name" => 'Manitoba'], |
||
106 | ["abbr" => 'NB', "name" => 'New Brunswick'], |
||
107 | ["abbr" => 'NL', "name" => 'Newfoundland And Labrador'], |
||
108 | ["abbr" => 'NS', "name" => 'Nova Scotia'], |
||
109 | ["abbr" => 'NT', "name" => 'Northwest Territories'], |
||
110 | ["abbr" => 'NU', "name" => 'Nunavut'], |
||
111 | ["abbr" => 'ON', "name" => 'Ontario'], |
||
112 | ["abbr" => 'PE', "name" => 'Prince Edward Island'], |
||
113 | ["abbr" => 'QC', "name" => 'Quebec'], |
||
114 | ["abbr" => 'SK', "name" => 'Saskatchewan'], |
||
115 | ["abbr" => 'YT', "name" => 'Yukon'], |
||
116 | ], |
||
117 | ]; |
||
118 | |||
119 | 9 | protected function getSubject(string $country = null): string |
|
131 | |||
132 | 9 | View Code Duplication | protected function getStateAbbreviations($country = null) |
144 | |||
145 | 9 | View Code Duplication | protected function getStateNames($country = null) |
157 | |||
158 | /** |
||
159 | * Determine if the validation rule passes. |
||
160 | * |
||
161 | * @param string $attribute |
||
162 | * @param mixed $value |
||
163 | * @return bool |
||
164 | */ |
||
165 | abstract public function passes($attribute, $value); |
||
166 | |||
167 | /** |
||
168 | * Get the validation error message. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | abstract public function message(); |
||
173 | } |
||
174 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.