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 |
||
| 13 | class Hydrate extends Command |
||
| 14 | { |
||
| 15 | use FilterInputVariable; |
||
| 16 | |||
| 17 | const |
||
| 18 | ENV_DEV = 'dev', |
||
| 19 | OPTION_ASSIGNMENT = '='; |
||
| 20 | |||
| 21 | private |
||
| 22 | $dryRun, |
||
|
|
|||
| 23 | $isBackupEnabled, |
||
| 24 | $environment; |
||
| 25 | |||
| 26 | public function __construct(Application $app) |
||
| 35 | |||
| 36 | protected function configure() |
||
| 37 | { |
||
| 38 | parent::configure(); |
||
| 39 | |||
| 40 | $this |
||
| 41 | ->setName('hydrate') |
||
| 42 | ->setDescription('Hydrate dist files') |
||
| 43 | |||
| 44 | ->addArgument('sourcePath', InputArgument::OPTIONAL, 'source path to hydrate') |
||
| 45 | |||
| 46 | ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_DEV) |
||
| 47 | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Simulation mode') |
||
| 48 | ->addOption('backup', 'b', InputOption::VALUE_NONE, 'Backup overwritten files') |
||
| 49 | ->addOption('override', 'o', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Override variable values', array()) |
||
| 50 | ->addOption('data', 'd', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Custom data values', array()) |
||
| 51 | ; |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 55 | { |
||
| 56 | parent::execute($input, $output); |
||
| 57 | |||
| 58 | $this->processInputs($input); |
||
| 59 | $this->launchHydration(); |
||
| 60 | } |
||
| 61 | |||
| 62 | private function processInputs(InputInterface $input) |
||
| 106 | |||
| 107 | private function launchHydration() |
||
| 123 | |||
| 124 | private function parseOptionWithAssignments(InputInterface $input, $optionName) |
||
| 159 | |||
| 160 | private function processOverridenVariables(array $overrides) |
||
| 161 | { |
||
| 178 | |||
| 179 | private function processCustomData(array $data) |
||
| 195 | } |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.