Failed Conditions
Pull Request — master (#1)
by Yo
01:56
created

InitCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 26
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
crap 2
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\Command\Helper\CommandTemplateHelper;
10
use Yoanm\DefaultPhpRepository\Command\Processor\CommandTemplateProcessor;
11
use Yoanm\DefaultPhpRepository\Factory\TemplatePathBagFactory;
12
use Yoanm\DefaultPhpRepository\Factory\VariableBagFactory;
13
14
class InitCommand extends Command
15
{
16
    const TYPE_INIT = 'template.init';
17
    const TYPE_GIT = 'template.git';
18
    const TYPE_COMPOSER = 'template.composer';
19
    const TYPE_TEST = 'template.test';
20
    const TYPE_CI = 'template.ci';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this->setName('init')
28
            ->setDescription('Will init the current github repository with default file templates')
29
            ->addArgument(
30
                'type',
31
                InputArgument::OPTIONAL,
32
                'type of repository (library/symfony/project)',
33
                Mode::PHP_LIBRARY
34
            )
35
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'List template file instead of creation them')
36
            ->addOption(
37
                'id',
38
                null,
39
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
40
                'process only given ids'
41
            )
42
            ->addOption(
43
                'ask-before-override',
44
                'a',
45
                InputOption::VALUE_NONE,
46
                'Will ask before overriding an existing file'
47
            )
48
            ->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $outputLevelSpace = '    ';
58
59
        $mode = $input->getArgument('type');
60
61
        if (!in_array($mode, [Mode::PHP_LIBRARY, Mode::SYMFONY_LIBRARY, Mode::PROJECT])) {
62
            $output->writeln(sprintf('<error>Unexpected mode "%s" !</error>', $mode));
63
            $output->writeln('<info>Allowed mode :</info>');
64
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::PHP_LIBRARY));
65
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::SYMFONY_LIBRARY));
66
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::PROJECT));
67
68
            return 1;
69
        }
70
71
        $variableBag = (new VariableBagFactory())->load($mode);
72
        $templatePathList = (new TemplatePathBagFactory())->load($mode);
73
74
        $skipExistingFile = false === $input->getOption('ask-before-override');
75
        $forceOverride = $input->getOption('force-override');
76
        if (true === $forceOverride) {
77
            $skipExistingFile = false;
78
        }
79
80
        $commandTemplateHelper = new CommandTemplateHelper(
81
            $this->getHelper('question'),
1 ignored issue
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...
82
            $input,
83
            $output,
84
            $variableBag->all(),
85
            $skipExistingFile,
86
            $forceOverride
87
        );
88
        $commandProcessor = new CommandTemplateProcessor($commandTemplateHelper);
89
90
        $output->writeln(sprintf('<comment>Creating default files for : </comment><info>%s</info>', ucwords($mode)));
91
        if (true === $forceOverride) {
92
            $output->writeln('<fg=red>WARNING :  Existing files will be overriden by default</fg=red>');
93
        } elseif (true === $skipExistingFile) {
94
            $output->writeln('<comment>INFO : Existing files will be skipped !</comment>');
95
        }
96
        try {
97
            $currentType = null;
98
            foreach ($templatePathList as $templateKey => $templatePath) {
99
100
                if (count($input->getOption('id')) && !in_array($templateKey, $input->getOption('id'))) {
101
                    continue;
102
                }
103
104
                if (null === $currentType || !preg_match(sprintf('#%s#', preg_quote($currentType)), $templateKey)) {
105
                    preg_match('#(template\.[^\.]+)#', $templateKey, $matches);
106
                    $currentType = isset($matches[1]) ? $matches[1] : $templateKey;
107
                    $header = ucwords(str_replace('template.', '', $currentType));
108
                    if ('Init' === $header) {
109
                        $header = 'Init repository';
110
                    } elseif ('Ci' === $header) {
111
                        $header = 'Continuous integration';
112
                    }
113
                    $output->writeln(sprintf('<info>%s%s</info>', $outputLevelSpace, $header));
114
                }
115
116
                $output->writeln(sprintf(
117
                    '%s* %s : ',
118
                    str_repeat($outputLevelSpace, 2),
119
                    ucwords(str_replace('template.', '', str_replace($currentType.'.', '', $templateKey)))
120
                ));
121
                if (true === $input->getOption('list')) {
122
                    $output->writeln(sprintf(
123
                        '%s<comment>Id   : </comment><info>%s</info>',
124
                        str_repeat($outputLevelSpace, 3),
125
                        $templateKey
126
                    ));
127
                    $output->writeln(sprintf(
128
                        '%s<comment>File : </comment><info>%s</info>',
129
                        str_repeat($outputLevelSpace, 3),
130
                        $templatePath
131
                    ));
132
                } else {
133
                    $commandProcessor->process($templatePath);
134
                }
135
            }
136
            return 0;
137
        } catch (\Exception $e) {
138
            $output->writeln(sprintf('<error>Error -> %s</error>', $e->getMessage()));
139
            throw $e;
140
        }
141
    }
142
}
143