Test Failed
Pull Request — master (#9)
by Yo
02:48 queued 55s
created

InitCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
crap 12
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Command;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Yoanm\DefaultPhpRepository\Processor\InitCommandProcessor;
10
use Yoanm\DefaultPhpRepository\Processor\ListCommandProcessor;
11
use Yoanm\DefaultPhpRepository\Factory\TemplateListFactory;
12
use Yoanm\DefaultPhpRepository\Factory\VarFactory;
13
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
14
use Yoanm\DefaultPhpRepository\Model\Template;
15
use Yoanm\DefaultPhpRepository\Resolver\NamespaceResolver;
16
17
class InitCommand extends Command
18
{
19
    const TYPE_INIT = 'template.init';
20
    const TYPE_GIT = 'template.git';
21
    const TYPE_COMPOSER = 'template.composer';
22
    const TYPE_TEST = 'template.test';
23
    const TYPE_CI = 'template.ci';
24
25
    const OUTPUT_LEVEL_SPACE = '    ';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        $this->setName('init')
33
            ->setDescription('Will init the current github repository with default file templates')
34
            ->addArgument(
35
                'repository_type',
36
                InputArgument::OPTIONAL,
37
                'Repository type (library/project)',
38
                RepositoryType::PROJECT
39
            )
40
            ->addOption(
41
                'symfony',
42
                null,
43
                InputOption::VALUE_NONE,
44
                'If symfony sub type'
45
            )
46
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'List template ids')
47
            ->addOption(
48
                'id',
49
                null,
50
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
51
                'process only given templates ids'
52
            )
53
            ->addOption(
54
                'ask-before-override',
55
                'a',
56
                InputOption::VALUE_NONE,
57
                'Will ask before overriding an existing file'
58
            )
59
            ->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default')
60
        ;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $repositoryType = $input->getArgument('repository_type');
69
        $repositorySubType = true === $input->getOption('symfony')
70
            ? RepositorySubType::SYMFONY
71
            : RepositorySubType::PHP
72
        ;
73
74
        if (!$this->validateRepositoryType($output, $repositoryType)) {
75
            return 1;
76
        }
77
78
        $templateList = (new TemplateListFactory())->create($repositoryType);
79
        (new NamespaceResolver())->resolve($templateList, $repositoryType, $repositorySubType);
80
81
        $this->getProcessor($input, $output, $templateList, $repositoryType)->process();
82
    }
83
84
    /**
85
     * @param OutputInterface $output
86
     * @param string          $repositoryType
87
     *
88
     * @return bool
89
     */
90
    protected function validateRepositoryType(OutputInterface $output, $repositoryType)
91
    {
92
        // Check type
93
        $availableTypeList = [RepositoryType::LIBRARY, RepositoryType::PROJECT];
94
        if (!in_array($repositoryType, $availableTypeList)) {
95
            $output->writeln(sprintf('<error>Unexpected type "%s" !</error>', $repositoryType));
96
            $output->writeln(sprintf(
97
                '<info>Allowed type : %s </info>',
98
                implode(
99
                    ' / ',
100
                    array_map(
101
                        function ($availableMode) {
102
                            return sprintf('<comment>%s</comment>', $availableMode);
103
                        },
104
                        $availableTypeList
105
                    )
106
                )
107
            ));
108
109
            return false;
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * @param InputInterface  $input
117
     * @param OutputInterface $output
118
     * @param Template[]      $templateList
119
     * @param string          $repositoryType
120
     *
121
     * @return InitCommandProcessor|ListCommandProcessor
122
     *
123
     * @throws \Twig_Error_Loader
124
     */
125
    protected function getProcessor(InputInterface $input, OutputInterface $output, array $templateList, $repositoryType)
126
    {
127
        // Default processor
128
        $processor = new ListCommandProcessor($this->getHelper('question'), $input, $output, $templateList);
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...
129
130
        if (false === $input->getOption('list')) {
131
132
            $forceOverride = $input->getOption('force-override');
133
            $skipExisting = true === $forceOverride
134
                ? false
135
                : false === $input->getOption('ask-before-override')
136
            ;
137
            $templateHelper = new TemplateHelper(
138
                new \Twig_Environment(null, ['autoescape' => false]),
139
                (new VarFactory())->create($repositoryType)
140
            );
141
142
            $processor = new InitCommandProcessor(
143
                $this->getHelper('question'),
144
                $input,
145
                $output,
146
                $templateHelper,
147
                $templateList,
148
                $skipExisting,
149
                $forceOverride,
150
                $input->getOption('id')
0 ignored issues
show
Documentation introduced by
$input->getOption('id') is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
            );
152
        }
153
154
        return $processor;
155
    }
156
}
157