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 | * Creates a new instance of the SyncerCommand. |
||
| 50 | * @param FileCollector $fileCollector File Collector. |
||
| 51 | * @param EnvironmentFinder $envFinder Environment Variable Filder. |
||
| 52 | * @param TableBuilder $tableBuilder Table Builder. |
||
| 53 | */ |
||
| 54 | public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Runs the command. |
||
| 63 | * @param array $arguments Command line arguments. |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function handle(array $arguments) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets a list of the environment variables defined in the |
||
| 95 | * source code, .env and .env.example file. |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | protected function getEnvironmentVariables() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Takes the list of environment variables defined and creates |
||
| 115 | * a results array showing each variable and where it is or is not |
||
| 116 | * defined. |
||
| 117 | * @param array $envData Environment Variable Data. |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | protected function getResults(array $envData) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Looks through all the current env variables from all |
||
| 138 | * sources and makes a master list of all of them. |
||
| 139 | * @param array $currentEnvs Current Env Variables. |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | protected function mergeEnvs(array $currentEnvs) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * If a variable is defined in the source code but |
||
| 153 | * not defined in the .env.example file we write |
||
| 154 | * to the message and set response to 1 |
||
| 155 | * @param array $results Environment Variable Results. |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | View Code Duplication | protected function checkForCiBuild(array $results) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * If a variable is defined in the source code but |
||
| 169 | * not defined in the .env file we write |
||
| 170 | * to the message and set response to 1 |
||
| 171 | * @param array $results Environment Variable Results. |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | View Code Duplication | protected function checkForDeploy(array $results) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Collects the command line arguments. |
||
| 185 | * @param array $arguments Command line arguments. |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | protected function argumentsValid(array $arguments) |
||
| 214 | } |
||
| 215 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: