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 |
||
| 17 | class CreateDataObjectCommand extends Command |
||
| 18 | { |
||
| 19 | const ARGUMENTS_NAME = 'name'; |
||
| 20 | const ARGUMENTS_NAMESPACE = 'namespace'; |
||
| 21 | const ARGUMENTS_PATH = 'path'; |
||
| 22 | const OPTIONS_HAS_ONE = 'withHasOne'; |
||
| 23 | const OPTIONS_HAS_MANY = 'withHasMany'; |
||
| 24 | const OPTIONS_MANY_MANY = 'withManyMany'; |
||
| 25 | const OPTIONS_TRADITIONAL_ARRAY = 'withTraditionalArray'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $name; |
||
|
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $namespace; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $modulePath; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Filesystem |
||
| 44 | */ |
||
| 45 | private $fileSystem; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string The target frameowkr version |
||
| 49 | */ |
||
| 50 | private $frameworkVersion = 'ss4'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $separator = DIRECTORY_SEPARATOR; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Whether or not to use traditional array syntax |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $useTraditionalArraySyntax = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Configures the command |
||
| 65 | */ |
||
| 66 | View Code Duplication | protected function configure() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Executes this command |
||
| 82 | * @param InputInterface $input |
||
| 83 | * @param OutputInterface $output |
||
| 84 | * @return int|null|void |
||
| 85 | */ |
||
| 86 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the argument values to their respective properties |
||
| 103 | * @param InputInterface $input |
||
| 104 | * @throws RuntimeException |
||
| 105 | */ |
||
| 106 | View Code Duplication | protected function setArguments(InputInterface $input) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param InputInterface $input |
||
| 118 | */ |
||
| 119 | protected function preCopyOptions(InputInterface $input) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Checks for and actions all actions |
||
| 128 | * @param InputInterface $input |
||
| 129 | * @param OutputInterface $output |
||
| 130 | */ |
||
| 131 | protected function postCopyOptions(InputInterface $input) |
||
| 132 | { |
||
| 133 | $source = $this->getSourcePath('options'); |
||
| 134 | $target = $this->getTargetPath(); |
||
| 135 | $uri = function ($folder, $endPoint) { |
||
| 136 | return "{$folder}{$this->separator}{$endPoint}"; |
||
| 137 | }; |
||
| 138 | if ($input->getOption(self::OPTIONS_HAS_ONE)) { |
||
| 139 | // $this->moduleType = 'silverstripe-module'; |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($ss3 = $input->getOption(self::OPTIONS_SS3)) { |
||
| 143 | $this->frameworkVersion = 'ss3'; |
||
| 144 | } |
||
| 145 | |||
| 146 | View Code Duplication | if ($withTravis = $input->getOption(self::OPTIONS_HAS_MANY)) { |
|
| 147 | $file = '.travis.yml'; |
||
| 148 | $this->getFilesystem()->copy( |
||
| 149 | $uri($source, $file), |
||
| 150 | $uri($target, $file) |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | View Code Duplication | if ($withCircleCI = $input->getOption(self::OPTIONS_MANY_MANY)) { |
|
| 155 | $folder = '.circleci'; |
||
| 156 | $this->getFilesystem()->mirror( |
||
| 157 | $uri($source, $folder), |
||
| 158 | $uri($target, $folder) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Copies the skeleton to the root directory |
||
| 165 | */ |
||
| 166 | protected function copySkeleton() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Copies the configured values to the composer.json file |
||
| 176 | */ |
||
| 177 | protected function setupComposerJson() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets or sets the file system property |
||
| 196 | * @return Filesystem |
||
| 197 | */ |
||
| 198 | protected function getFilesystem() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the path to the given sub-dir. Defaults to assets skeleton |
||
| 209 | * @param string $subDir |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function getSourcePath($subDir = 'assets') |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Gets destination path for the module skeleton |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | protected function getTargetPath() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function isTraditionalSyntax() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param bool $traditionalSyntax |
||
| 238 | * @return CreateDataObjectCommand |
||
| 239 | */ |
||
| 240 | public function setTraditionalSyntax($traditionalSyntax) |
||
| 245 | } |
||
| 246 |