| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Genesis\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class NetteTester extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	private $workingDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	private $target; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	private $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * Returns working directory. System will switch to this directory before running tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 2 |  | 	public function getWorkingDir() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  | 		return $this->workingDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Sets working directory. System will switch to this directory before running tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @param mixed $workingDir | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 2 |  | 	public function setWorkingDir($workingDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  | 		$this->workingDir = $workingDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * Returns target to be executed. Dir in working directory, dot (current dir), TestFile, etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  | 	public function getTarget() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  | 		return $this->target; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * Sets target to be executed. Dir in working directory, dot (current dir), TestFile, etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param mixed $target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  | 	public function setTarget($target) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  | 		$this->target = $target; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @return array|NULL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  | 	public function getOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  | 		return $this->options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * Possible options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * - executable (mandatory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * - iniFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * - interpreter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * - threads | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * - mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param array|NULL $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  | 	public function setOptions(array $options = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  | 		$this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  | 	public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  | 		if (!isset($this->options['executable'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			$this->error('Tester executable is not defined.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  | 		$cmd = 'php '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  | 		$cmd .= escapeshellarg($this->options['executable']) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  | 		if (isset($this->options['iniFile'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$cmd .= '-c '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			$cmd .= escapeshellarg($this->options['iniFile']) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  | 		if (isset($this->options['interpreter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$cmd .= '-p '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			$cmd .= escapeshellarg($this->options['interpreter']) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		} | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 97 | 2 | View Code Duplication | 		if (isset($this->options['threads'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  | 			$cmd .= '-j '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  | 			$cmd .= escapeshellarg(max(1, (int) $this->options['threads'])) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 | 2 | View Code Duplication | 		if (isset($this->options['mode'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$cmd .= '-o '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$cmd .= escapeshellarg($this->options['mode']) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  | 		$cmd .= '-s '; // show skipped tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  | 		$cmd .= escapeshellarg($this->target) . ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  | 		$currdir = getcwd(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  | 		$result = @chdir($this->workingDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  | 		if (!$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$this->error("Cannot change working directory to '$this->workingDir'."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  | 		$command = new Exec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  | 		$command->setCommand($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  | 		$result = $command->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  | 		if ($result->getResult() !== 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			$this->error(sprintf('Tests failed with code %d.', $result->getResult())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  | 		$result = @chdir($currdir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  | 		if (!$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$this->error("Cannot change working directory back to '$currdir'."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                        
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.