Total Complexity | 57 |
Total Lines | 323 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like EAP 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.
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 EAP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class EAP extends Entity { |
||
47 | |||
48 | /** |
||
49 | * some EAP-related constants. |
||
50 | */ |
||
51 | const PEAP = 25; |
||
52 | const MSCHAP2 = 26; |
||
53 | const TTLS = 21; |
||
54 | const TLS = 13; |
||
55 | const NONE = 0; |
||
56 | const GTC = 6; |
||
57 | const FAST = 43; |
||
58 | const PWD = 52; |
||
59 | const NE_PAP = 1; |
||
60 | const NE_MSCHAP = 2; |
||
61 | const NE_MSCHAP2 = 3; |
||
62 | const NE_SILVERBULLET = 999; |
||
63 | const INTEGER_TTLS_PAP = 1; |
||
64 | const INTEGER_PEAP_MSCHAP2 = 2; |
||
65 | const INTEGER_TLS = 3; |
||
66 | const INTEGER_FAST_GTC = 4; |
||
67 | const INTEGER_TTLS_GTC = 5; |
||
68 | const INTEGER_TTLS_MSCHAP2 = 6; |
||
69 | const INTEGER_EAP_PWD = 7; |
||
70 | const INTEGER_SILVERBULLET = 8; |
||
71 | |||
72 | // PHP7 allows to define constants with arrays as value. Hooray! This makes |
||
73 | // lots of public static members of the EAP class obsolete |
||
74 | |||
75 | /** |
||
76 | * PEAP-MSCHAPv2: Outer EAP Type = 25, Inner EAP Type = 26 |
||
77 | */ |
||
78 | const EAPTYPE_PEAP_MSCHAP2 = ["OUTER" => EAP::PEAP, "INNER" => EAP::MSCHAP2]; |
||
79 | |||
80 | /** |
||
81 | * EAP-TLS: Outer EAP Type = 13, no inner EAP |
||
82 | */ |
||
83 | const EAPTYPE_TLS = ["OUTER" => EAP::TLS, "INNER" => EAP::NONE]; |
||
84 | |||
85 | /** |
||
86 | * EAP-TLS: Outer EAP Type = 13, no inner EAP |
||
87 | */ |
||
88 | const EAPTYPE_SILVERBULLET = ["OUTER" => EAP::TLS, "INNER" => EAP::NE_SILVERBULLET]; |
||
89 | |||
90 | /** |
||
91 | * TTLS-PAP: Outer EAP type = 21, no inner EAP, inner non-EAP = 1 |
||
92 | */ |
||
93 | const EAPTYPE_TTLS_PAP = ["OUTER" => EAP::TTLS, "INNER" => EAP::NONE]; |
||
94 | |||
95 | /** |
||
96 | * TTLS-MSCHAP-v2: Outer EAP type = 21, no inner EAP, inner non-EAP = 3 |
||
97 | */ |
||
98 | const EAPTYPE_TTLS_MSCHAP2 = ["OUTER" => EAP::TTLS, "INNER" => EAP::MSCHAP2]; |
||
99 | |||
100 | /** |
||
101 | * TTLS-GTC: Outer EAP type = 21, Inner EAP Type = 6 |
||
102 | */ |
||
103 | const EAPTYPE_TTLS_GTC = ["OUTER" => EAP::TTLS, "INNER" => EAP::GTC]; |
||
104 | |||
105 | /** |
||
106 | * EAP-FAST (GTC): Outer EAP type = 43, Inner EAP Type = 6 |
||
107 | */ |
||
108 | const EAPTYPE_FAST_GTC = ["OUTER" => EAP::FAST, "INNER" => EAP::GTC]; |
||
109 | |||
110 | /** |
||
111 | * PWD: Outer EAP type = 52, no inner EAP |
||
112 | */ |
||
113 | const EAPTYPE_PWD = ["OUTER" => EAP::PWD, "INNER" => EAP::NONE]; |
||
114 | |||
115 | /** |
||
116 | * NULL: no outer EAP, no inner EAP |
||
117 | */ |
||
118 | const EAPTYPE_NONE = ["OUTER" => EAP::NONE, "INNER" => EAP::NONE]; |
||
119 | |||
120 | /** |
||
121 | * ANY: not really an EAP method, but the term to use when needing to express "any EAP method we know" |
||
122 | */ |
||
123 | const EAPTYPE_ANY = ["OUTER" => 255, "INNER" => 255]; |
||
124 | |||
125 | /** |
||
126 | * conversion table between array and integer representations |
||
127 | */ |
||
128 | const EAPTYPES_CONVERSION = [ |
||
129 | EAP::INTEGER_FAST_GTC => EAP::EAPTYPE_FAST_GTC, |
||
130 | EAP::INTEGER_PEAP_MSCHAP2 => EAP::EAPTYPE_PEAP_MSCHAP2, |
||
131 | EAP::INTEGER_EAP_PWD => EAP::EAPTYPE_PWD, |
||
132 | EAP::INTEGER_TLS => EAP::EAPTYPE_TLS, |
||
133 | EAP::INTEGER_TTLS_GTC => EAP::EAPTYPE_TTLS_GTC, |
||
134 | EAP::INTEGER_TTLS_MSCHAP2 => EAP::EAPTYPE_TTLS_MSCHAP2, |
||
135 | EAP::INTEGER_TTLS_PAP => EAP::EAPTYPE_TTLS_PAP, |
||
136 | EAP::INTEGER_SILVERBULLET => EAP::EAPTYPE_SILVERBULLET, |
||
137 | ]; |
||
138 | |||
139 | /** |
||
140 | * The array representation of the EAP type |
||
141 | * @var array |
||
142 | */ |
||
143 | private $arrayRep; |
||
144 | |||
145 | /** |
||
146 | * The integer representation of the EAP type |
||
147 | * @var integer |
||
148 | */ |
||
149 | private $intRep; |
||
150 | |||
151 | /** |
||
152 | * Instantiates the EAP class for a concrete EAP type. Only call it to |
||
153 | * instantiate *real* EAP types, i.e. not EAPTYPE::ANY or EAPTYPE::NONE |
||
154 | * |
||
155 | * @param mixed $eapType the EAP type, either in its integer or array representation |
||
156 | */ |
||
|
|||
157 | public function __construct($eapType) { |
||
158 | if (is_numeric($eapType) && array_key_exists($eapType, EAP::EAPTYPES_CONVERSION)) { |
||
159 | $key = array_keys(EAP::EAPTYPES_CONVERSION, EAP::EAPTYPES_CONVERSION[$eapType]); |
||
160 | $this->intRep = $key[0]; |
||
161 | $this->arrayRep = EAP::EAPTYPES_CONVERSION[$this->intRep]; |
||
162 | return; |
||
163 | } |
||
164 | if (is_array($eapType)) { |
||
165 | $key = array_search($eapType, EAP::EAPTYPES_CONVERSION); |
||
166 | if ($key !== FALSE) { |
||
167 | // add a type cast to int to make Scrutinizer realise that the key found is always an integer |
||
168 | $this->intRep = (int)$key; // array index is always an integer |
||
169 | $this->arrayRep = EAP::EAPTYPES_CONVERSION[(int)$key]; |
||
170 | return; |
||
171 | } |
||
172 | } |
||
173 | throw new Exception("Unable to instantiate the EAP class - the EAP type is bogus."); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Is this a password-based EAP method? |
||
178 | * @return boolean |
||
179 | * @throws Exception |
||
180 | */ |
||
181 | public function isPasswordRequired() { |
||
182 | switch ($this->intRep) { |
||
183 | case EAP::INTEGER_EAP_PWD: |
||
184 | case EAP::INTEGER_FAST_GTC: |
||
185 | case EAP::INTEGER_PEAP_MSCHAP2: |
||
186 | case EAP::INTEGER_TTLS_GTC: |
||
187 | case EAP::INTEGER_TTLS_MSCHAP2: |
||
188 | case EAP::INTEGER_TTLS_PAP: |
||
189 | return TRUE; |
||
190 | case EAP::INTEGER_TLS: |
||
191 | case EAP::INTEGER_SILVERBULLET: |
||
192 | return FALSE; |
||
193 | default: |
||
194 | throw new Exception("Unable to determine if the EAP type required a password or not!"); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * There could be EAP methods which have an optional need for a password. |
||
200 | * Not aware of any, so this is a simple function :-) |
||
201 | * @return boolean |
||
202 | */ |
||
203 | public function isPasswordOptional() { |
||
204 | return FALSE; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Is this a certificate-based EAP method? |
||
209 | * @return boolean |
||
210 | * @throws Exception |
||
211 | */ |
||
212 | public function isClientCertRequired() { |
||
213 | switch ($this->intRep) { |
||
214 | case EAP::INTEGER_EAP_PWD: |
||
215 | case EAP::INTEGER_FAST_GTC: |
||
216 | case EAP::INTEGER_PEAP_MSCHAP2: |
||
217 | case EAP::INTEGER_TTLS_GTC: |
||
218 | case EAP::INTEGER_TTLS_MSCHAP2: |
||
219 | case EAP::INTEGER_TTLS_PAP: |
||
220 | return FALSE; |
||
221 | case EAP::INTEGER_TLS: |
||
222 | case EAP::INTEGER_SILVERBULLET: |
||
223 | return TRUE; |
||
224 | default: |
||
225 | throw new Exception("Unable to determine if the EAP type requires client-certificates or not!"); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Does an EAP type optionally allow to send a client certificate? |
||
231 | * @return boolean |
||
232 | * @throws Exception |
||
233 | */ |
||
234 | public function isClientCertOptional() { |
||
235 | switch ($this->intRep) { |
||
236 | case EAP::INTEGER_EAP_PWD: |
||
237 | case EAP::INTEGER_TLS: |
||
238 | case EAP::INTEGER_SILVERBULLET: |
||
239 | return FALSE; |
||
240 | case EAP::INTEGER_FAST_GTC: |
||
241 | case EAP::INTEGER_PEAP_MSCHAP2: |
||
242 | case EAP::INTEGER_TTLS_GTC: |
||
243 | case EAP::INTEGER_TTLS_MSCHAP2: |
||
244 | case EAP::INTEGER_TTLS_PAP: |
||
245 | return TRUE; |
||
246 | default: |
||
247 | throw new Exception("Unable to determine if the EAP type has optional client-certificates or not!"); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Does the EAP type require the specification of trusted CAs to be secure? |
||
253 | * @return boolean |
||
254 | * @throws Exception |
||
255 | */ |
||
256 | public function needsServerCACert() { |
||
257 | switch ($this->intRep) { |
||
258 | case EAP::INTEGER_EAP_PWD: |
||
259 | return FALSE; |
||
260 | case EAP::INTEGER_FAST_GTC: |
||
261 | case EAP::INTEGER_PEAP_MSCHAP2: |
||
262 | case EAP::INTEGER_TTLS_GTC: |
||
263 | case EAP::INTEGER_TTLS_MSCHAP2: |
||
264 | case EAP::INTEGER_TTLS_PAP: |
||
265 | case EAP::INTEGER_TLS: |
||
266 | case EAP::INTEGER_SILVERBULLET: |
||
267 | return TRUE; |
||
268 | default: |
||
269 | throw new Exception("Unable to determine if the EAP type requires a CA trust base for secure functioning or not!"); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Does the EAP type require the specification of a server name to be secure? |
||
275 | * EAP-pwd has one, but it is not really required. |
||
276 | * @return boolean |
||
277 | * @throws Exception |
||
278 | */ |
||
279 | public function needsServerName() { |
||
280 | switch ($this->intRep) { |
||
281 | case EAP::INTEGER_FAST_GTC: |
||
282 | case EAP::INTEGER_PEAP_MSCHAP2: |
||
283 | case EAP::INTEGER_TTLS_GTC: |
||
284 | case EAP::INTEGER_TTLS_MSCHAP2: |
||
285 | case EAP::INTEGER_TTLS_PAP: |
||
286 | case EAP::INTEGER_TLS: |
||
287 | case EAP::INTEGER_SILVERBULLET: |
||
288 | return TRUE; |
||
289 | case EAP::INTEGER_EAP_PWD: |
||
290 | return FALSE; |
||
291 | default: |
||
292 | throw new Exception("Unable to determine if the EAP type requires a server name trust base for secure functioning or not!"); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Returns the Array representation of the EAP type. |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getArrayRep() { |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Returns the int representation of the EAP type. |
||
307 | * |
||
308 | * @return int |
||
309 | */ |
||
310 | public function getIntegerRep() { |
||
311 | return $this->intRep; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * This function takes the EAP method in array representation (OUTER/INNER) and returns it in a custom format for the |
||
316 | * Linux installers (not numbers, but strings as values). |
||
317 | * @param array $eap EAP method in array representation (OUTER/INNER) |
||
318 | * @return array EAP method in array representation (OUTER as string/INNER as string) |
||
319 | */ |
||
320 | public static function eapDisplayName($eap) { |
||
333 | } |
||
334 | |||
335 | |||
336 | /** |
||
337 | * This function enumerates all known EAP types and returns them as array |
||
338 | * |
||
339 | * @return array of all EAP types the CAT knows about, as objects |
||
340 | */ |
||
341 | public static function listKnownEAPTypes() { |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * returns a printable ("pretty-print") version of the EAP type |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getPrintableRep() { |
||
372 |