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 WindowsCommon 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 WindowsCommon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | abstract class WindowsCommon extends \core\DeviceConfig { |
||
28 | |||
29 | public function copyBasicFiles() { |
||
30 | if (!($this->copyFile('wlan_test.exe') && |
||
31 | $this->copyFile('check_wired.cmd') && |
||
32 | $this->copyFile('install_wired.cmd') && |
||
33 | $this->copyFile('cat_bg.bmp') && |
||
34 | $this->copyFile('base64.nsh'))) { |
||
35 | throw new Exception("Copying needed files (part 1) failed for at least one file!"); |
||
36 | } |
||
37 | |||
38 | if (!($this->copyFile('cat32.ico') && |
||
39 | $this->copyFile('cat_150.bmp') && |
||
40 | $this->copyFile('WLANSetEAPUserData/WLANSetEAPUserData32.exe', 'WLANSetEAPUserData32.exe') && |
||
41 | $this->copyFile('WLANSetEAPUserData/WLANSetEAPUserData64.exe', 'WLANSetEAPUserData64.exe'))) { |
||
42 | throw new Exception("Copying needed files (part 2) failed for at least one file!"); |
||
43 | } |
||
44 | if (!$this->translateFile('common.inc', 'common.nsh', $this->codePage)) { |
||
45 | throw new Exception("Translating needed file common.inc failed!"); |
||
46 | } |
||
47 | return; |
||
48 | } |
||
49 | |||
50 | public function copyPwdFiles() { |
||
51 | if (!($this->copyFile('Aruba_Networks_EAP-pwd_x32.msi') && |
||
52 | $this->copyFile('Aruba_Networks_EAP-pwd_x64.msi'))) { |
||
53 | throw new Exception("Copying needed files (EAP-pwd) failed for at least one file!"); |
||
54 | } |
||
55 | if (!$this->translateFile('pwd.inc', 'cat.NSI', $this->codePage)) { |
||
56 | throw new Exception("Translating needed file pwd.inc failed!"); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public function copyGeantLinkFiles() { |
||
61 | if (!($this->copyFile('GEANTLink/GEANTLink32.msi', 'GEANTLink32.msi') && |
||
62 | $this->copyFile('GEANTLink/GEANTLink64.msi', 'GEANTLink64.msi') && |
||
63 | $this->copyFile('GEANTLink/CredWrite.exe', 'CredWrite.exe') && |
||
64 | $this->copyFile('GEANTLink/MsiUseFeature.exe', 'MsiUseFeature.exe'))) { |
||
65 | throw new Exception("Copying needed files (GEANTLink) failed for at least one file!"); |
||
66 | } |
||
67 | if (!$this->translateFile('geant_link.inc', 'cat.NSI', $this->codePage)) { |
||
68 | throw new Exception("Translating needed file geant_link.inc failed!"); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * function to escape double quotes in a special NSI-compatible way |
||
75 | * |
||
76 | * @param string $in input string |
||
77 | * @return string |
||
78 | */ |
||
79 | public static function echo_nsi($in) { |
||
80 | echo preg_replace('/"/', '$\"', $in); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $input input string |
||
85 | * @return string |
||
86 | */ |
||
87 | public static function sprint_nsi($input) { |
||
88 | return preg_replace('/"/', '$\"', $input); |
||
89 | } |
||
90 | |||
91 | public function __construct() { |
||
92 | parent::__construct(); |
||
93 | $this->useGeantLink = (isset($this->options['args']) && $this->options['args'] == 'gl') ? 1 : 0; |
||
94 | } |
||
95 | |||
96 | protected function prepareInstallerLang() { |
||
106 | |||
107 | public function writeDeviceInfo() { |
||
108 | $ssidCount = count($this->attributes['internal:SSID']); |
||
109 | $out = "<p>"; |
||
110 | $out .= sprintf(_("%s installer will be in the form of an EXE file. It will configure %s on your device, by creating wireless network profiles.<p>When you click the download button, the installer will be saved by your browser. Copy it to the machine you want to configure and execute."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']); |
||
111 | $out .= "<p>"; |
||
112 | if ($ssidCount > 1) { |
||
113 | if ($ssidCount > 2) { |
||
114 | $out .= sprintf(_("In addition to <strong>%s</strong> the installer will also configure access to the following networks:"), implode(', ', CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) . " "; |
||
115 | } else { |
||
116 | $out .= sprintf(_("In addition to <strong>%s</strong> the installer will also configure access to:"), implode(', ', CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) . " "; |
||
117 | } |
||
118 | $out .= '<strong>' . join('</strong>, <strong>', array_diff(array_keys($this->attributes['internal:SSID']), CONFIG_CONFASSISTANT['CONSORTIUM']['ssid'])) . '</strong>'; |
||
119 | $out .= "<p>"; |
||
120 | } |
||
121 | // TODO - change this below |
||
122 | if ($this->selectedEapObject->isClientCertRequired()) { |
||
123 | $out .= sprintf(_("In order to connect to the network you will need an a personal certificate in the form of a p12 file. You should obtain this certificate from your %s. Consult the support page to find out how this certificate can be obtained. Such certificate files are password protected. You should have both the file and the password available during the installation process."), $this->nomenclature_inst); |
||
124 | return($out); |
||
125 | } |
||
126 | // not EAP-TLS |
||
127 | $out .= sprintf(_("In order to connect to the network you will need an account from your %s. You should consult the support page to find out how this account can be obtained. It is very likely that your account is already activated."), $this->nomenclature_inst); |
||
128 | |||
129 | if (!$this->useGeantLink && $this->selectedEap['OUTER'] == \core\common\EAP::TTLS) { |
||
130 | $out .= "<p>"; |
||
131 | $out .= _("When you are connecting to the network for the first time, Windows will pop up a login box, where you should enter your user name and password. This information will be saved so that you will reconnect to the network automatically each time you are in the range."); |
||
132 | if ($ssidCount > 1) { |
||
133 | $out .= "<p>"; |
||
134 | $out .= _("You will be required to enter the same credentials for each of the configured notworks:") . " "; |
||
135 | $out .= '<strong>' . join('</strong>, <strong>', array_keys($this->attributes['internal:SSID'])) . '</strong>'; |
||
136 | } |
||
137 | } |
||
138 | return($out); |
||
139 | } |
||
140 | |||
141 | private function scaleLogo($imagePath, $maxSize) { |
||
160 | |||
161 | protected function combineLogo($logos = NULL, $fedLogo = NULL) { |
||
203 | |||
204 | protected function signInstaller() { |
||
217 | |||
218 | protected function compileNSIS() { |
||
228 | |||
229 | private function getSupport($attr, $type) { |
||
230 | $supportString = [ |
||
231 | 'email' => 'SUPPORT', |
||
232 | 'url' => 'URL', |
||
233 | ]; |
||
234 | $s = "support_" . $type . "_substitute"; |
||
235 | $substitute = $this->translateString($this->$s, $this->codePage); |
||
236 | $returnValue = !empty($attr['support:' . $type][0]) ? $attr['support:' . $type][0] : $substitute; |
||
237 | return('!define ' . $supportString[$type] . ' "' . $returnValue . '"' . "\n"); |
||
238 | } |
||
239 | |||
240 | |||
241 | protected function writeNsisDefines($eap, $attr) { |
||
|
|||
242 | $fcontents = "\n" . '!define NSIS_MAJOR_VERSION ' . CONFIG_CONFASSISTANT['NSIS_VERSION']; |
||
243 | if ($attr['internal:profile_count'][0] > 1) { |
||
244 | $fcontents .= "\n" . '!define USER_GROUP "' . $this->translateString(str_replace('"', '$\\"', $attr['profile:name'][0]), $this->codePage) . '" |
||
245 | '; |
||
246 | } |
||
247 | $fcontents .= ' |
||
248 | Caption "' . $this->translateString(sprintf(WindowsCommon::sprint_nsi(_("%s installer for %s")), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $attr['general:instname'][0]), $this->codePage) . '" |
||
249 | !define APPLICATION "' . $this->translateString(sprintf(WindowsCommon::sprint_nsi(_("%s installer for %s")), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $attr['general:instname'][0]), $this->codePage) . '" |
||
250 | !define VERSION "' . \core\CAT::VERSION_MAJOR . '.' . \core\CAT::VERSION_MINOR . '" |
||
251 | !define INSTALLER_NAME "installer.exe" |
||
252 | !define LANG "' . $this->lang . '" |
||
253 | !define LOCALE "' . preg_replace('/\..*$/', '', CONFIG['LANGUAGES'][$this->languageInstance->getLang()]['locale']) . '" |
||
254 | ;-------------------------------- |
||
255 | !define ORGANISATION "' . $this->translateString($attr['general:instname'][0], $this->codePage) . '" |
||
256 | '; |
||
257 | $fcontents .= $this->getSupport($attr, 'email'); |
||
258 | $fcontents .= $this->getSupport($attr, 'url'); |
||
259 | if (\core\common\Entity::getAttributeValue($attr, 'media:wired', 0) == 'on') { |
||
260 | $fcontents .= '!define WIRED |
||
261 | '; |
||
262 | } |
||
263 | $fcontents .= '!define PROVIDERID "urn:UUID:' . $this->deviceUUID . '" |
||
264 | '; |
||
265 | if (!empty($attr['internal:realm'][0])) { |
||
266 | $fcontents .= '!define REALM "' . $attr['internal:realm'][0] . '" |
||
267 | '; |
||
268 | } |
||
269 | View Code Duplication | if(!empty($attr['internal:hint_userinput_suffix'][0]) && $attr['internal:hint_userinput_suffix'][0] == 1) { |
|
270 | $fcontents .= '!define HINT_USER_INPUT "' . $attr['internal:hint_userinput_suffix'][0] . '" |
||
271 | '; |
||
272 | } |
||
273 | View Code Duplication | if(!empty($attr['internal:verify_userinput_suffix'][0]) && $attr['internal:verify_userinput_suffix'][0] == 1) { |
|
274 | $fcontents .= '!define VERIFY_USER_REALM_INPUT "' . $attr['internal:verify_userinput_suffix'][0] . '" |
||
275 | '; |
||
276 | } |
||
277 | $fcontents .= $this->msInfoFile($attr); |
||
278 | return($fcontents); |
||
279 | |||
280 | } |
||
281 | |||
282 | protected function msInfoFile($attr) { |
||
312 | |||
313 | protected function writeAdditionalDeletes($profiles) { |
||
327 | |||
328 | protected function writeClientP12File() { |
||
334 | |||
335 | protected function writeTlsUserProfile() { |
||
338 | |||
339 | public $LANGS = [ |
||
340 | 'fr' => ['nsis' => "French", 'cp' => '1252'], |
||
341 | 'de' => ['nsis' => "German", 'cp' => '1252'], |
||
342 | 'es' => ['nsis' => "SpanishInternational", 'cp' => '1252'], |
||
343 | 'it' => ['nsis' => "Italian", 'cp' => '1252'], |
||
344 | 'nl' => ['nsis' => "Dutch", 'cp' => '1252'], |
||
345 | 'sv' => ['nsis' => "Swedish", 'cp' => '1252'], |
||
346 | 'fi' => ['nsis' => "Finnish", 'cp' => '1252'], |
||
347 | 'pl' => ['nsis' => "Polish", 'cp' => '1250'], |
||
348 | 'ca' => ['nsis' => "Catalan", 'cp' => '1252'], |
||
349 | 'sr' => ['nsis' => "SerbianLatin", 'cp' => '1250'], |
||
350 | 'hr' => ['nsis' => "Croatian", 'cp' => '1250'], |
||
351 | 'sl' => ['nsis' => "Slovenian", 'cp' => '1250'], |
||
352 | 'da' => ['nsis' => "Danish", 'cp' => '1252'], |
||
353 | 'nb' => ['nsis' => "Norwegian", 'cp' => '1252'], |
||
354 | 'nn' => ['nsis' => "NorwegianNynorsk", 'cp' => '1252'], |
||
355 | 'el' => ['nsis' => "Greek", 'cp' => '1253'], |
||
356 | 'ru' => ['nsis' => "Russian", 'cp' => '1251'], |
||
357 | 'pt' => ['nsis' => "Portuguese", 'cp' => '1252'], |
||
358 | 'uk' => ['nsis' => "Ukrainian", 'cp' => '1251'], |
||
359 | 'cs' => ['nsis' => "Czech", 'cp' => '1250'], |
||
360 | 'sk' => ['nsis' => "Slovak", 'cp' => '1250'], |
||
361 | 'bg' => ['nsis' => "Bulgarian", 'cp' => '1251'], |
||
362 | 'hu' => ['nsis' => "Hungarian", 'cp' => '1250'], |
||
363 | 'ro' => ['nsis' => "Romanian", 'cp' => '1250'], |
||
364 | 'lv' => ['nsis' => "Latvian", 'cp' => '1257'], |
||
365 | 'mk' => ['nsis' => "Macedonian", 'cp' => '1251'], |
||
366 | 'et' => ['nsis' => "Estonian", 'cp' => '1257'], |
||
367 | 'tr' => ['nsis' => "Turkish", 'cp' => '1254'], |
||
368 | 'lt' => ['nsis' => "Lithuanian", 'cp' => '1257'], |
||
369 | 'ar' => ['nsis' => "Arabic", 'cp' => '1256'], |
||
370 | 'he' => ['nsis' => "Hebrew", 'cp' => '1255'], |
||
371 | 'id' => ['nsis' => "Indonesian", 'cp' => '1252'], |
||
372 | 'mn' => ['nsis' => "Mongolian", 'cp' => '1251'], |
||
373 | 'sq' => ['nsis' => "Albanian", 'cp' => '1252'], |
||
374 | 'br' => ['nsis' => "Breton", 'cp' => '1252'], |
||
375 | 'be' => ['nsis' => "Belarusian", 'cp' => '1251'], |
||
376 | 'is' => ['nsis' => "Icelandic", 'cp' => '1252'], |
||
377 | 'ms' => ['nsis' => "Malay", 'cp' => '1252'], |
||
378 | 'bs' => ['nsis' => "Bosnian", 'cp' => '1250'], |
||
379 | 'ga' => ['nsis' => "Irish", 'cp' => '1250'], |
||
380 | 'uz' => ['nsis' => "Uzbek", 'cp' => '1251'], |
||
381 | 'gl' => ['nsis' => "Galician", 'cp' => '1252'], |
||
382 | 'af' => ['nsis' => "Afrikaans", 'cp' => '1252'], |
||
383 | 'ast' => ['nsis' => "Asturian", 'cp' => '1252'], |
||
384 | ]; |
||
385 | public $codePage; |
||
386 | public $lang; |
||
387 | public $useGeantLink; |
||
388 | private $background; |
||
389 | |||
390 | } |
||
391 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.