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 Environment 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 Environment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Environment |
||
14 | { |
||
15 | protected $config; |
||
16 | protected $fileSystem; |
||
17 | protected $env; |
||
18 | protected $input; |
||
19 | protected $output; |
||
20 | |||
21 | const OS_WINDOWS = 'windows'; |
||
22 | const OS_LINUX = 'linux'; |
||
23 | const OS_MAC = 'mac'; |
||
24 | const OS_TYPE_64BIT = '64bit'; |
||
25 | const OS_TYPE_32BIT = '32bit'; |
||
26 | |||
27 | public function __construct( |
||
37 | |||
38 | // @todo Move to public methods into SeleniumSetup\Environment. |
||
39 | public function test() |
||
40 | { |
||
41 | // Pre-requisites. |
||
42 | $canInstall = true; |
||
43 | $writeln = []; |
||
44 | |||
45 | // Start checking. |
||
46 | $javaVersion = $this->getJavaVersion(); |
||
47 | |||
48 | if (empty($javaVersion)) { |
||
49 | $writeln[] = '<error>[ ] Java is not installed.</error>'; |
||
50 | $canInstall = false; |
||
51 | } else { |
||
52 | $writeln[] = '<info>[x] Java is installed.</info>'; |
||
53 | if ($this->isJavaVersionDeprecated($javaVersion)) { |
||
54 | $writeln[] = '<error>[ ] Your Java version needs to be >= 1.6</error>'; |
||
55 | $canInstall = false; |
||
56 | } else { |
||
57 | $writeln[] = '<info>[x] Your Java version ' . $javaVersion . ' seems up to date.</info>'; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | if ($this->isPHPVersionDeprecated()) { |
||
62 | $writeln[] = '<error>[ ] Your PHP version ' . $this->getPHPVersion() . ' should be >= 5.3</error>'; |
||
63 | $canInstall = false; |
||
64 | } else { |
||
65 | $writeln[] = '<info>[x] Your PHP version is ' . $this->getPHPVersion() . '</info>'; |
||
66 | } |
||
67 | |||
68 | if (!$this->hasPHPCurlExtInstalled()) { |
||
69 | $writeln[] = '<error>[ ] cURL extension for PHP is missing.</error>'; |
||
70 | $canInstall = false; |
||
71 | } else { |
||
72 | $writeln[] = '<info>[x] cURL ' . $this->getPHPCurlExtVersion() . ' extension is installed.</info>'; |
||
73 | } |
||
74 | |||
75 | if (!$this->hasPHPOpenSSLExtInstalled()) { |
||
76 | $writeln[] = '<error>[ ] OpenSSL extension for PHP is missing.</error>'; |
||
77 | $canInstall = false; |
||
78 | } else { |
||
79 | $writeln[] = '<info>[x] ' . $this->getPHPOpenSSLExtVersion() . ' extension is installed.</info>'; |
||
80 | } |
||
81 | |||
82 | $this->output->writeln($writeln); |
||
83 | |||
84 | return $canInstall; |
||
85 | } |
||
86 | |||
87 | // @todo Fine-tune the Windows and Mac detection if possible. |
||
88 | public function getOsName() |
||
102 | |||
103 | public function getOsVersion() |
||
107 | |||
108 | public function getOsType() |
||
117 | |||
118 | public function isWindows() |
||
122 | |||
123 | public function isMac() |
||
127 | |||
128 | public function isLinux() |
||
132 | |||
133 | public function isAdmin() |
||
134 | { |
||
135 | if ($this->isWindows()) { |
||
136 | $cmd = 'NET SESSION'; |
||
137 | $lookForNegative = '^System error'; |
||
138 | |||
139 | } else { |
||
140 | $cmd = 'sudo -n true'; |
||
141 | $lookForNegative = '^sudo\: a password is required'; |
||
142 | } |
||
143 | |||
144 | $output = new BufferedOutput(); |
||
145 | |||
146 | $process = new Process($cmd, SeleniumSetup::$APP_ROOT_PATH, SeleniumSetup::$APP_PROCESS_ENV, null, null); |
||
147 | $process->run(function($type, $line) use ($output) { |
||
148 | $output->write($line); |
||
149 | }); |
||
150 | |||
151 | return !(preg_match('/' . $lookForNegative . '/is', $output->fetch())); |
||
152 | } |
||
153 | |||
154 | public function getJavaVersion() |
||
170 | |||
171 | public function isJavaVersionDeprecated($javaVersion) |
||
175 | |||
176 | public function hasJavaCli() |
||
180 | |||
181 | public function hasPHPInstalled() |
||
185 | |||
186 | public function getPHPVersion() |
||
190 | |||
191 | public function isPHPVersionDeprecated() |
||
195 | |||
196 | public function canUseTheLatestPHPUnitVersion() |
||
200 | |||
201 | public function hasPHPCurlExtInstalled() |
||
205 | |||
206 | public function getPHPCurlExtVersion() |
||
210 | |||
211 | public function hasPHPOpenSSLExtInstalled() |
||
215 | |||
216 | public function getPHPOpenSSLExtVersion() |
||
220 | |||
221 | // @todo Decide if this is still neded. |
||
222 | public function hasCurlCli() |
||
231 | |||
232 | public function getEnvVar($varName) |
||
236 | |||
237 | public function setEnvVar($varName, $varValue = '') |
||
245 | |||
246 | public function addPathToGlobalPath($path) |
||
257 | |||
258 | public function download($from, $to) |
||
300 | |||
301 | public function getCurlVersion() |
||
312 | |||
313 | View Code Duplication | public function killProcessByPid($pid) |
|
330 | |||
331 | View Code Duplication | public function killProcessByName($processName) |
|
348 | |||
349 | View Code Duplication | public function listenToPort($port) |
|
369 | |||
370 | public function getPidFromListeningToPort($port) |
||
383 | |||
384 | public function makeExecutable($file) |
||
402 | |||
403 | public function startSeleniumProcess() |
||
404 | { |
||
405 | // @todo Refactor this in 5.0; split binaries and drivers; Add Opera. |
||
406 | // @see https://github.com/bogdananton/Selenium-Setup/issues/12 |
||
407 | $cmdExtra = ''; |
||
408 | View Code Duplication | if ($binary = $this->config->getBinary('chromedriver.' . $this->getOsName() . '.' . $this->getOsType())) { |
|
409 | $cmdExtra .= sprintf(' -Dwebdriver.chrome.driver=%s', $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName()); |
||
410 | } |
||
411 | View Code Duplication | if ($binary = $this->config->getBinary('iedriver.' . $this->getOsName() . '.' . $this->getOsType())) { |
|
412 | $cmdExtra .= sprintf(' -Dwebdriver.ie.driver=%s', $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName()); |
||
413 | } |
||
414 | View Code Duplication | if ($binary = $this->config->getBinary('phantomjs.' . $this->getOsName() . '.' . $this->getOsType())) { |
|
415 | $cmdExtra .= sprintf(' -Dphantomjs.binary.path=%s', $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $binary->getBinName()); |
||
416 | } |
||
417 | |||
418 | if ($this->isWindows()) { |
||
419 | $cmd = 'start /b java -jar %s -port %s -Dhttp.proxyHost=%s -Dhttp.proxyPort=%s -log %s %s'; |
||
420 | } else { |
||
421 | $cmd = 'java -jar %s -port %s -Dhttp.proxyHost=%s -Dhttp.proxyPort=%s -log %s %s >/dev/null 2>&1 &'; |
||
422 | |||
423 | } |
||
424 | |||
425 | $cmd = vsprintf($cmd, [ |
||
426 | 'binary' => $this->config->getBuildPath() . DIRECTORY_SEPARATOR . $this->config->getBinary('selenium')->getBinName(), |
||
427 | 'port' => $this->config->getPort(), |
||
428 | 'proxyHost' => $this->config->getProxyHost(), |
||
429 | 'proxyPort' => $this->config->getProxyPort(), |
||
430 | 'log' => $this->config->getLogsPath() . DIRECTORY_SEPARATOR . 'selenium.log', |
||
431 | 'cmdExtra' => $cmdExtra |
||
432 | ]); |
||
433 | |||
434 | //var_dump($cmd); |
||
435 | |||
436 | $process = new Process($cmd, SeleniumSetup::$APP_ROOT_PATH, SeleniumSetup::$APP_PROCESS_ENV, null, null); |
||
437 | $process->start(); |
||
438 | // $process->getOutput(); |
||
439 | return $process->getPid(); |
||
440 | } |
||
441 | |||
442 | public function hasXvfb() |
||
450 | |||
451 | public function startDisplayProcess() |
||
470 | |||
471 | View Code Duplication | public function getChromeVersion() |
|
492 | |||
493 | View Code Duplication | public function getFirefoxVersion() |
|
513 | |||
514 | } |
||
515 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.