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, 'If specified, 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 | $this->name = $input->getArgument('name'); |
||
|
|
|||
| 47 | $this->force = $input->getOption('force'); |
||
| 48 | $this->open = $input->getOption('open'); |
||
| 49 | |||
| 50 | try { |
||
| 51 | // file name (without extension) |
||
| 52 | if (false !== $extPos = strripos($this->name, '.md')) { |
||
| 53 | $this->name = substr($this->name, 0, $extPos); |
||
| 54 | } |
||
| 55 | // path |
||
| 56 | $fileRelativePath = $this->getBuilder($output)->getConfig()->get('content.dir').'/'.$this->name.'.md'; |
||
| 57 | $filePath = $this->getPath().'/'.$fileRelativePath; |
||
| 58 | |||
| 59 | // file already exists? |
||
| 60 | View Code Duplication | if ($this->fs->exists($filePath) && !$this->force) { |
|
| 61 | $helper = $this->getHelper('question'); |
||
| 62 | $question = new ConfirmationQuestion(sprintf('This page already exists. Do you want to override it? [y/n]', $this->getpath()), false); |
||
| 63 | if (!$helper->ask($input, $output, $question)) { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | // create new file |
||
| 69 | $title = $this->name; |
||
| 70 | if (false !== strrchr($this->name, '/')) { |
||
| 71 | $title = substr(strrchr($this->name, '/'), 1); |
||
| 72 | } |
||
| 73 | $date = date('Y-m-d'); |
||
| 74 | $fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findModel($this->name)); |
||
| 75 | $this->fs->dumpFile($filePath, $fileContent); |
||
| 76 | |||
| 77 | $output->writeln(sprintf('File "%s" created.', $fileRelativePath)); |
||
| 78 | |||
| 79 | // open editor? |
||
| 80 | if ($this->open) { |
||
| 81 | if (!$this->hasEditor()) { |
||
| 82 | $output->writeln('<comment>No editor configured.</comment>'); |
||
| 83 | } |
||
| 84 | $this->openEditor($filePath); |
||
| 85 | } |
||
| 86 | } catch (\Exception $e) { |
||
| 87 | throw new \Exception(sprintf($e->getMessage())); |
||
| 88 | } |
||
| 89 | |||
| 90 | return 0; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Find the page model and return its content. |
||
| 95 | * |
||
| 96 | * @param string $name |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | protected function findModel($name) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Editor is configured? |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | protected function hasEditor() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Open new file in editor (if configured). |
||
| 137 | * |
||
| 138 | * @param string $filePath |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | protected function openEditor($filePath) |
||
| 162 | } |
||
| 163 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.