Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

ConfigureCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 16
loc 66
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 19 3
A createProjectDirectory() 16 16 2

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
namespace PressCLI\Command;
3
4
use RuntimeException;
5
use PressCLI\Option\Configuration;
6
use PressCLI\Option\GlobalConfiguration;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ConfigureCommand extends Command
14
{
15
    /**
16
     * Configure create command.
17
     */
18
    protected function configure()
19
    {
20
        $this->setName('configure')
21
             ->setDescription('Creates a new project directory and configuration.')
22
             ->addArgument('project-name', InputArgument::OPTIONAL, 'The project name.')
23
             ->addOption(
24
                 'global',
25
                 null,
26
                 InputOption::VALUE_NONE,
27
                 'Creates the global configuration if it does not already exist.'
28
             );
29
    }
30
31
    /**
32
     * Execute create command.
33
     *
34
     * @param  InputInterface  $input
35
     * @param  OutputInterface $output
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $global = $input->getOption('global');
40
        if ($global) {
41
            GlobalConfiguration::create($output);
42
43
            return;
44
        }
45
46
        $name = $input->getArgument('project-name');
47
        if (!$name) {
48
            throw new RuntimeException("Project name is required when not using the --global option!");
49
        }
50
51
        // Handle directory creation and local configuration.
52
        $directory = getcwd() . "/{$name}";
53
        $this->createProjectDirectory($directory, $output);
54
        Configuration::create($directory, $name, $input, $output, $this->getHelper('question'));
0 ignored issues
show
Compatibility introduced by
$this->getHelper('question') of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Symfony\Component...\Helper\QuestionHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55
    }
56
57
    /**
58
     * Create project directory.
59
     *
60
     * @return InitCommand
61
     */
62 View Code Duplication
    protected function createProjectDirectory($directory, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
63
    {
64
        $output->writeln("<info>Creating project directory...</info>");
65
66
        if (file_exists($directory)) {
67
            $output->writeln("<comment>Project directory already exists at {$directory}.</comment>");
68
69
            return $this;
70
        }
71
72
        mkdir($directory, 0755, true);
73
74
        $output->writeln("<info>Project directory created at {$directory}!</info>");
75
76
        return $this;
77
    }
78
}
79