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

InitCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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('id', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'process only given ids')
37
            ->addOption('ask-before-override', 'a', InputOption::VALUE_NONE, 'Will ask before overriding an existing file')
38
            ->addOption('force-override', 'f', InputOption::VALUE_NONE, 'Override existing files by default')
39
        ;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $outputLevelSpace = '    ';
48
49
        $mode = $input->getArgument('type');
50
51
        if (!in_array($mode, [Mode::PHP_LIBRARY, Mode::SYMFONY_LIBRARY, Mode::PROJECT])) {
52
            $output->writeln(sprintf('<error>Unexpected mode "%s" !</error>', $mode));
53
            $output->writeln('<info>Allowed mode :</info>');
54
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::PHP_LIBRARY));
55
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::SYMFONY_LIBRARY));
56
            $output->writeln(sprintf('%s<comment>%s</comment>', $outputLevelSpace, Mode::PROJECT));
57
58
            return 1;
59
        }
60
61
        $variableBag = (new VariableBagFactory())->load($mode);
62
        $templatePathList = (new TemplatePathBagFactory())->load($mode);
63
64
        $skipExistingFile = false === $input->getOption('ask-before-override');
65
        $forceOverride = $input->getOption('force-override');
66
        if (true === $forceOverride) {
67
            $skipExistingFile = false;
68
        }
69
70
        $commandTemplateHelper = new CommandTemplateHelper(
71
            $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...
72
            $input,
73
            $output,
74
            $variableBag->all(),
75
            $skipExistingFile,
76
            $forceOverride
77
        );
78
        $commandProcessor = new CommandTemplateProcessor($commandTemplateHelper);
79
80
        $output->writeln(sprintf('<comment>Creating default files for : </comment><info>%s</info>', ucwords($mode)));
81
        if (true === $forceOverride) {
82
            $output->writeln('<fg=red>WARNING :  Existing files will be overriden by default</fg=red>');
83
        } elseif (true === $skipExistingFile) {
84
            $output->writeln('<comment>INFO : Existing files will be skipped !</comment>');
85
        }
86
        try {
87
            $currentType = null;
88
            foreach ($templatePathList as $templateKey => $templatePath) {
89
90
                if (count($input->getOption('id')) && !in_array($templateKey, $input->getOption('id'))) {
91
                    continue;
92
                }
93
94
                if (null === $currentType || !preg_match(sprintf('#%s#', preg_quote($currentType)), $templateKey)) {
95
                    preg_match('#(template\.[^\.]+)#', $templateKey, $matches);
96
                    $currentType = isset($matches[1]) ? $matches[1] : $templateKey;
97
                    $header = ucwords(str_replace('template.', '', $currentType));
98
                    if ('Init' === $header) {
99
                        $header = 'Init repository';
100
                    } elseif ('Ci' === $header) {
101
                        $header = 'Continuous integration';
102
                    }
103
                    $output->writeln(sprintf('<info>%s%s</info>', $outputLevelSpace, $header));
104
                }
105
106
                $output->writeln(sprintf(
107
                    '%s* %s : ',
108
                    str_repeat($outputLevelSpace, 2),
109
                    ucwords(str_replace('template.', '', str_replace($currentType.'.', '', $templateKey)))
110
                ));
111
                if (true === $input->getOption('list')) {
112
                    $output->writeln(sprintf(
113
                        '%s<comment>Id   : </comment><info>%s</info>',
114
                        str_repeat($outputLevelSpace, 3),
115
                        $templateKey
116
                    ));
117
                    $output->writeln(sprintf(
118
                        '%s<comment>File : </comment><info>%s</info>',
119
                        str_repeat($outputLevelSpace, 3),
120
                        $templatePath
121
                    ));
122
                } else {
123
                    $commandProcessor->process($templatePath);
124
                }
125
            }
126
            return 0;
127
        } catch (\Exception $e) {
128
            $output->writeln(sprintf('<error>Error -> %s</error>', $e->getMessage()));
129
            throw $e;
130
        }
131
    }
132
}
133