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:
| 1 | <?php | ||
| 9 | class SyncerCommand | ||
| 10 | { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Will be returned when program exits. | ||
| 14 | * @var integer | ||
| 15 | */ | ||
| 16 | protected $exitCode = 0; | ||
| 17 | |||
| 18 | /** | ||
| 19 | * The mode the program is running in. | ||
| 20 | * @var string | ||
| 21 | */ | ||
| 22 | protected $mode; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Instance of the FileCollector class. | ||
| 26 | * @var FileCollector | ||
| 27 | */ | ||
| 28 | protected $fileCollector; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Instance of the EnvironmentFilder class. | ||
| 32 | * @var EnvironmentFinder | ||
| 33 | */ | ||
| 34 | protected $envFinder; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * Instance of the TableBuilder class. | ||
| 38 | * @var TableBuilder | ||
| 39 | */ | ||
| 40 | protected $tableBuilder; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * The source colde folder that will be inspected. | ||
| 44 | * @var string | ||
| 45 | */ | ||
| 46 | protected $folder; | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Holds an error message if command runs into an error. | ||
| 50 | * @var string | ||
| 51 | */ | ||
| 52 | protected $error; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Creates a new instance of the SyncerCommand. | ||
| 56 | * @param FileCollector $fileCollector File Collector. | ||
| 57 | * @param EnvironmentFinder $envFinder Environment Variable Filder. | ||
| 58 | * @param TableBuilder $tableBuilder Table Builder. | ||
| 59 | */ | ||
| 60 | public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder) | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Runs the command. | ||
| 69 | * @param array $arguments Command line arguments. | ||
| 70 | * @return void | ||
| 71 | */ | ||
| 72 | public function handle(array $arguments) | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Gets a list of the environment variables defined in the | ||
| 101 | * source code, .env and .env.example file. | ||
| 102 | * @return array | ||
| 103 | */ | ||
| 104 | protected function getEnvironmentVariables() | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Takes the list of environment variables defined and creates | ||
| 121 | * a results array showing each variable and where it is or is not | ||
| 122 | * defined. | ||
| 123 | * @param array $envData Environment Variable Data. | ||
| 124 | * @return array | ||
| 125 | */ | ||
| 126 | protected function getResults(array $envData) | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Looks through all the current env variables from all | ||
| 144 | * sources and makes a master list of all of them. | ||
| 145 | * @param array $currentEnvs Current Env Variables. | ||
| 146 | * @return array | ||
| 147 | */ | ||
| 148 | protected function mergeEnvs(array $currentEnvs) | ||
| 156 | |||
| 157 | /** | ||
| 158 | * If a variable is defined in the source code but | ||
| 159 | * not defined in the .env.example file we write | ||
| 160 | * to the message and set response to 1 | ||
| 161 | * @param array $results Environment Variable Results. | ||
| 162 | * @return void | ||
| 163 | */ | ||
| 164 | View Code Duplication | protected function checkForCiBuild(array $results) | |
| 172 | |||
| 173 | /** | ||
| 174 | * If a variable is defined in the source code but | ||
| 175 | * not defined in the .env file we write | ||
| 176 | * to the message and set response to 1 | ||
| 177 | * @param array $results Environment Variable Results. | ||
| 178 | * @return void | ||
| 179 | */ | ||
| 180 | View Code Duplication | protected function checkForDeploy(array $results) | |
| 188 | |||
| 189 | /** | ||
| 190 | * Collects the command line arguments. | ||
| 191 | * @param array $arguments Command line arguments. | ||
| 192 | * @return boolean | ||
| 193 | */ | ||
| 194 | protected function argumentsValid(array $arguments) | ||
| 220 | } | ||
| 221 | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.