Completed
Push — symfony-console ( 505007...6e7652 )
by Arnaud
02:41 queued 10s
created

NewSite   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 13.73 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 9
dl 7
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
B execute() 7 28 6

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Command;
10
11
use Cecil\Util\Plateform;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
class NewSite extends Command
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('new:site')
28
            ->setDescription('Create a new website')
29
            ->setDefinition(
30
                new InputDefinition([
31
                    new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'),
32
                    new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override the directory if already exist'),
33
                ])
34
            )
35
            ->setHelp('Create a new website in the current directory, or in <path> if provided.');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $this->force = $input->getOption('force');
0 ignored issues
show
Bug introduced by
The property force does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
45
        try {
46 View Code Duplication
            if ($this->fs->exists($this->getPath().'/'.self::CONFIG_FILE) && !$this->force) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
47
                $helper = $this->getHelper('question');
48
                $question = new ConfirmationQuestion('Website already exists. Do you want to override it? [y/n]', false);
49
                if (!$helper->ask($input, $output, $question)) {
50
                    return;
51
                }
52
            }
53
            $root = __DIR__.'/../../';
54
            if (Plateform::isPhar()) {
55
                $root = Plateform::getPharPath().'/';
56
            }
57
            $output->writeln('<info>Creating a new website...</info>');
58
            $this->fs->copy($root.'res/skeleton/config.yml', $this->getPath().'/'.self::CONFIG_FILE, true);
59
            $this->fs->mirror($root.'res/skeleton/content', $this->getPath().'/content');
60
            $this->fs->mirror($root.'res/skeleton/layouts', $this->getPath().'/layouts');
61
            $this->fs->mirror($root.'res/skeleton/static', $this->getPath().'/static');
62
            $output->writeln('Done!');
63
        } catch (\Exception $e) {
64
            throw new \Exception(sprintf($e->getMessage()));
65
        }
66
67
        return 0;
68
    }
69
}
70