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 CreateModuleCommand extends Command |
||
18 | { |
||
19 | const ARGUMENTS_MODULE_NAME = 'module-name'; |
||
20 | const ARGUMENTS_MODULE_NAMESPACE = 'module-namespace'; |
||
21 | const ARGUMENTS_MODULE_PATH = 'module-path'; |
||
22 | const OPTIONS_SS3 = 'ss3'; |
||
23 | const OPTIONS_NON_VENDOR = 'nonVendor'; |
||
24 | const OPTIONS_TRAVIS_CI = 'withTravisCI'; |
||
25 | const OPTIONS_CIRCLE_CI = 'withCircleCI'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $moduleName; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $namespace; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $moduleType = 'silverstripe-vendormodule'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $modulePath; |
||
46 | |||
47 | /** |
||
48 | * @var Filesystem |
||
49 | */ |
||
50 | private $fileSystem; |
||
51 | |||
52 | /** |
||
53 | * @var string The target frameowkr version |
||
54 | */ |
||
55 | private $frameworkVersion = 'ss4'; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $separator = DIRECTORY_SEPARATOR; |
||
61 | |||
62 | /** |
||
63 | * Configures the command |
||
64 | */ |
||
65 | View Code Duplication | protected function configure() |
|
77 | |||
78 | /** |
||
79 | * Executes this command |
||
80 | * @param InputInterface $input |
||
81 | * @param OutputInterface $output |
||
82 | * @return int|null|void |
||
83 | */ |
||
84 | protected function execute(InputInterface $input, OutputInterface $output) |
||
85 | { |
||
86 | $this->setArguments($input); |
||
87 | $output->writeln( |
||
88 | "Creating SilverStripe module named {$this->moduleName} " |
||
89 | . "at {$this->getModulePath()}" |
||
90 | ); |
||
91 | $this->preCopyOptions($input); |
||
92 | $this->copySkeleton(); |
||
93 | $output->writeln(' - Skeleton copied'); |
||
94 | $this->setupComposerJson(); |
||
95 | $output->writeln(' - composer.json updated'); |
||
96 | $this->postCopyOptions($input); |
||
97 | $output->writeln(' - Options copied'); |
||
98 | $output->writeln(' - Done'); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Applies any pre copy options |
||
103 | * @param InputInterface $input |
||
104 | */ |
||
105 | protected function preCopyOptions(InputInterface $input) |
||
115 | |||
116 | /** |
||
117 | * Applies any post copy options |
||
118 | * @param InputInterface $input |
||
119 | */ |
||
120 | protected function postCopyOptions(InputInterface $input) |
||
144 | |||
145 | /** |
||
146 | * Sets the argument values to their respective properties |
||
147 | * @param InputInterface $input |
||
148 | * @throws RuntimeException |
||
149 | */ |
||
150 | View Code Duplication | protected function setArguments(InputInterface $input) |
|
158 | |||
159 | /** |
||
160 | * Copies the skeleton to the root directory |
||
161 | */ |
||
162 | protected function copySkeleton() |
||
169 | |||
170 | /** |
||
171 | * Copies the configured values to the composer.json file |
||
172 | */ |
||
173 | protected function setupComposerJson() |
||
189 | |||
190 | /** |
||
191 | * Returns the path to the composer file |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getComposerFilePath() |
||
198 | |||
199 | /** |
||
200 | * Returns the content of the composer file |
||
201 | * @return bool|string |
||
202 | */ |
||
203 | public function getComposerFileContents() |
||
207 | |||
208 | /** |
||
209 | * Gets or sets the file system property |
||
210 | * @return Filesystem |
||
211 | */ |
||
212 | public function getFilesystem() |
||
220 | |||
221 | /** |
||
222 | * Returns the path to the given sub-dir. Defaults to assets skeleton |
||
223 | * @param string $subDir |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getSourcePath($subDir = 'assets') |
||
231 | |||
232 | /** |
||
233 | * Gets destination path for the module skeleton |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getTargetPath() |
||
241 | |||
242 | /** |
||
243 | * Returns the module path. Defaults to use getcwd(); |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getModulePath() |
||
253 | |||
254 | /** |
||
255 | * @param string $modulePath |
||
256 | * @return CreateModuleCommand |
||
257 | */ |
||
258 | public function setModulePath($modulePath) |
||
263 | } |
||
264 |
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.