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 |
||
20 | class NewPage extends Command |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | protected function configure() |
||
26 | { |
||
27 | $this |
||
28 | ->setName('new:page') |
||
29 | ->setDescription('Create a new page') |
||
30 | ->setDefinition( |
||
31 | new InputDefinition([ |
||
32 | new InputArgument('name', InputArgument::REQUIRED, 'New page name'), |
||
33 | new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'), |
||
34 | new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override the file if already exist'), |
||
35 | new InputOption('open', 'o', InputOption::VALUE_NONE, 'Open editor automatically'), |
||
36 | ]) |
||
37 | ) |
||
38 | ->setHelp('Create a new page file (with a default title and the current date).'); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | protected function execute(InputInterface $input, OutputInterface $output) |
||
45 | { |
||
46 | $name = (string) $input->getArgument('name'); |
||
47 | $force = $input->getOption('force'); |
||
48 | $open = $input->getOption('open'); |
||
49 | |||
50 | try { |
||
51 | // file name (without extension) |
||
52 | if (false !== $extPos = strripos($name, '.md')) { |
||
53 | $name = substr($name, 0, $extPos); |
||
54 | } |
||
55 | if (null === $name) { |
||
56 | throw new \Exception('New page\'s name can\'t be empty'); |
||
57 | } |
||
58 | |||
59 | // path |
||
60 | $fileRelativePath = $this->getBuilder($output)->getConfig()->get('content.dir').'/'.$name.'.md'; |
||
61 | $filePath = $this->getPath().'/'.$fileRelativePath; |
||
62 | |||
63 | // file already exists? |
||
64 | View Code Duplication | if ($this->fs->exists($filePath) && !$force) { |
|
|
|||
65 | $helper = $this->getHelper('question'); |
||
66 | $question = new ConfirmationQuestion(sprintf( |
||
67 | 'This page already exists. Do you want to override it? [y/n]', |
||
68 | $this->getpath()), |
||
69 | false |
||
70 | ); |
||
71 | if (!$helper->ask($input, $output, $question)) { |
||
72 | return; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | // create new file |
||
77 | $title = $name; |
||
78 | if (false !== strrchr($name, '/')) { |
||
79 | $title = substr(strrchr($name, '/'), 1); |
||
80 | } |
||
81 | $date = date('Y-m-d'); |
||
82 | $fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findModel($name)); |
||
83 | $this->fs->dumpFile($filePath, $fileContent); |
||
84 | |||
85 | $output->writeln(sprintf('File "%s" created.', $fileRelativePath)); |
||
86 | |||
87 | // open editor? |
||
88 | if ($open) { |
||
89 | if (!$this->hasEditor()) { |
||
90 | $output->writeln('<comment>No editor configured.</comment>'); |
||
91 | } |
||
92 | $this->openEditor($filePath); |
||
93 | } |
||
94 | } catch (\Exception $e) { |
||
95 | throw new \Exception(sprintf($e->getMessage())); |
||
96 | } |
||
97 | |||
98 | return 0; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Find the page model and return its content. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function findModel(string $name): string |
||
128 | |||
129 | /** |
||
130 | * Editor is configured? |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | protected function hasEditor(): bool |
||
142 | |||
143 | /** |
||
144 | * Open new file in editor (if configured). |
||
145 | * |
||
146 | * @param string $filePath |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | protected function openEditor(string $filePath) |
||
170 | } |
||
171 |
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.