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 ValidatorV1 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 ValidatorV1, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ValidatorV1 |
||
10 | { |
||
11 | private static $dataTypes = array( |
||
12 | 'billing_address' => 'string', |
||
13 | 'billing_city' => 'string', |
||
14 | 'billing_country' => 'string', |
||
15 | 'billing_firstname' => 'string', |
||
16 | 'billing_lastname' => 'string', |
||
17 | 'billing_fullname' => 'string', |
||
18 | 'billing_state' => 'string', |
||
19 | 'billing_zip' => 'string', |
||
20 | 'card_id' => 'string', |
||
21 | 'card_last4' => 'string', |
||
22 | 'country' => 'string', |
||
23 | 'cpu_class' => 'string', |
||
24 | 'device_fingerprint' => 'string', |
||
25 | 'firstname' => 'string', |
||
26 | 'gender' => 'string', |
||
27 | 'language' => 'string', |
||
28 | 'language_browser' => 'string', |
||
29 | 'language_system' => 'string', |
||
30 | 'language_user' => 'string', |
||
31 | 'languages' => 'string', |
||
32 | 'lastname' => 'string', |
||
33 | 'login_user_agent' => 'string', |
||
34 | 'os' => 'string', |
||
35 | 'payment_method' => 'string', |
||
36 | 'payment_mid' => 'string', |
||
37 | 'payment_system' => 'string', |
||
38 | 'product_description' => 'string', |
||
39 | 'product_name' => 'string', |
||
40 | 'registration_useragent' => 'string', |
||
41 | 'screen_orientation' => 'string', |
||
42 | 'screen_resolution' => 'string', |
||
43 | 'social_type' => 'string', |
||
44 | 'transaction_currency' => 'string', |
||
45 | 'transaction_id' => 'string', |
||
46 | 'transaction_mode' => 'string', |
||
47 | 'transaction_type' => 'string', |
||
48 | 'user_agent' => 'string', |
||
49 | 'user_merchant_id' => 'string', |
||
50 | 'user_name' => 'string', |
||
51 | 'website_url' => 'string', |
||
52 | 'transaction_source' => 'string', |
||
53 | 'ip' => 'string', |
||
54 | 'merchant_ip' => 'string', |
||
55 | 'real_ip' => 'string', |
||
56 | 'email' => 'string', |
||
57 | 'phone' => 'string', |
||
58 | 'age' => 'int', |
||
59 | 'card_bin' => 'int', |
||
60 | 'confirmation_timestamp' => 'int', |
||
61 | 'expiration_month' => 'int', |
||
62 | 'expiration_year' => 'int', |
||
63 | 'login_timestamp' => 'int', |
||
64 | 'registration_timestamp' => 'int', |
||
65 | 'timezone_offset' => 'int', |
||
66 | 'transaction_timestamp' => 'int', |
||
67 | 'product_quantity' => 'float', |
||
68 | 'transaction_amount' => 'float', |
||
69 | 'transaction_amount_converted' => 'float', |
||
70 | 'ajax_validation' => 'bool', |
||
71 | 'cookie_enabled' => 'bool', |
||
72 | 'do_not_track' => 'bool', |
||
73 | 'email_confirmed' => 'bool', |
||
74 | 'login_failed' => 'bool', |
||
75 | 'phone_confirmed' => 'bool', |
||
76 | ); |
||
77 | |||
78 | private static $types = array( |
||
79 | 'confirmation' => array( |
||
80 | 'mandatory' => array('confirmation_timestamp', 'user_merchant_id'), |
||
81 | 'optional' => array('email_confirmed', 'phone_confirmed'), |
||
82 | ), |
||
83 | 'login' => array( |
||
84 | 'mandatory' => array('login_timestamp', 'user_merchant_id'), |
||
85 | 'optional' => array('email', 'login_failed', 'phone') |
||
86 | ), |
||
87 | 'registration' => array( |
||
88 | 'mandatory' => array('registration_timestamp', 'user_merchant_id'), |
||
89 | 'optional' => array( |
||
90 | 'age', |
||
91 | 'country', |
||
92 | 'email', |
||
93 | 'firstname', |
||
94 | 'gender', |
||
95 | 'lastname', |
||
96 | 'phone', |
||
97 | 'social_type', |
||
98 | 'user_name', |
||
99 | ), |
||
100 | ), |
||
101 | 'transaction' => array( |
||
102 | 'mandatory' => array( |
||
103 | 'transaction_amount', |
||
104 | 'transaction_currency', |
||
105 | 'transaction_id', |
||
106 | 'transaction_mode', |
||
107 | 'transaction_timestamp', |
||
108 | 'transaction_type', |
||
109 | 'card_bin', |
||
110 | 'card_id', |
||
111 | 'card_last4', |
||
112 | 'expiration_month', |
||
113 | 'expiration_year', |
||
114 | 'user_merchant_id', |
||
115 | ), |
||
116 | 'optional' => array( |
||
117 | 'age', |
||
118 | 'country', |
||
119 | 'email', |
||
120 | 'firstname', |
||
121 | 'gender', |
||
122 | 'lastname', |
||
123 | 'phone', |
||
124 | 'user_name', |
||
125 | 'payment_method', |
||
126 | 'payment_mid', |
||
127 | 'payment_system', |
||
128 | 'transaction_amount_converted', |
||
129 | 'transaction_source', |
||
130 | 'billing_address', |
||
131 | 'billing_city', |
||
132 | 'billing_country', |
||
133 | 'billing_firstname', |
||
134 | 'billing_lastname', |
||
135 | 'billing_fullname', |
||
136 | 'billing_state', |
||
137 | 'billing_zip', |
||
138 | 'product_description', |
||
139 | 'product_name', |
||
140 | 'product_quantity', |
||
141 | 'website_url', |
||
142 | 'merchant_ip', |
||
143 | ) |
||
144 | ), |
||
145 | ); |
||
146 | |||
147 | /** |
||
148 | * Analyzes SequenceID |
||
149 | * |
||
150 | * @param string $sequenceId |
||
151 | * @return string[] |
||
152 | */ |
||
153 | public function analyzeSequenceId($sequenceId) |
||
168 | |||
169 | /** |
||
170 | * Analyzes identities from envelope |
||
171 | * |
||
172 | * @param IdentityNodeInterface[] $identities |
||
173 | * @return string |
||
174 | */ |
||
175 | public function analyzeIdentities(array $identities) |
||
190 | |||
191 | /** |
||
192 | * Analyzes envelope type and mandatory fields |
||
193 | * |
||
194 | * @param EnvelopeInterface $envelope |
||
195 | * @return string[] |
||
196 | */ |
||
197 | public function analyzeTypeAndMandatoryFields(EnvelopeInterface $envelope) |
||
240 | |||
241 | /** |
||
242 | * Analyzes field types |
||
243 | * |
||
244 | * @param EnvelopeInterface $envelope |
||
245 | * @return array |
||
246 | */ |
||
247 | public function analyzeFieldTypes(EnvelopeInterface $envelope) |
||
322 | |||
323 | /** |
||
324 | * Checks envelope validity and throws an exception on error |
||
325 | * |
||
326 | * @param EnvelopeInterface $envelope |
||
327 | * @throws EnvelopeValidationException |
||
328 | */ |
||
329 | public function validate(EnvelopeInterface $envelope) |
||
342 | |||
343 | /** |
||
344 | * Returns true if provided key belongs to custom fields family |
||
345 | * |
||
346 | * @param string $key |
||
347 | * @return bool |
||
348 | */ |
||
349 | public function isCustom($key) |
||
353 | } |
||
354 |
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.