FixCommand::initConfiguration()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 22
rs 8.8333
cc 7
nc 12
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Command;
5
6
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Configuration;
7
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\GrumphpConfigurationReader;
8
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\YamlConfigurationReader;
9
use Pluswerk\TypoScriptAutoFixer\Fixer\IssueFixer;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
final class FixCommand extends Command
17
{
18
    /**
19
     * @var IssueFixer
20
     */
21
    private $issueFixer;
22
23
    public function __construct(string $name = null)
24
    {
25
        $name = $name ?? 'fix';
26
        parent::__construct($name);
27
        $this->issueFixer = new IssueFixer();
28
    }
29
30
    protected function configure()
31
    {
32
        $this->addArgument('files', InputArgument::IS_ARRAY, 'files to fix', []);
33
        $this->addOption(
34
            'typoscript-linter-configuration',
35
            't',
36
            InputOption::VALUE_NONE,
37
            'if set the configuration file style is the typoscript-lint.yml file style'
38
        );
39
        $this->addOption(
40
            'grumphp-configuration',
41
            'g',
42
            InputOption::VALUE_NONE,
43
            'if set the configuration file style is the grumphp.yml file style'
44
        );
45
        $this->addOption(
46
            'configuration-file',
47
            'c',
48
            InputOption::VALUE_REQUIRED,
49
            'if set the configuration file of given path is used!',
50
            ''
51
        );
52
    }
53
54
    /**
55
     * @param InputInterface  $input
56
     * @param OutputInterface $output
57
     *
58
     * @return int|null
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output): ?int
61
    {
62
        $this->initConfiguration($input);
63
        $files = $input->getArgument('files');
64
65
        if (count($files) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type string; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        if (count(/** @scrutinizer ignore-type */ $files) > 0) {
Loading history...
66
            foreach ($files as $file) {
67
                if (is_file($file)) {
68
                    $this->issueFixer->fixIssuesForFile($file);
69
                }
70
            }
71
        }
72
        return 0;
73
    }
74
75
    /**
76
     * @param InputInterface $input
77
     */
78
    private function initConfiguration(InputInterface $input): void
79
    {
80
        $configuration = Configuration::getInstance();
81
        $configReader = null;
82
83
        if ($input->getOption('typoscript-linter-configuration')) {
84
            $configReader = new YamlConfigurationReader();
85
        } elseif ($input->getOption('grumphp-configuration')) {
86
            $configReader = new GrumphpConfigurationReader();
87
        }
88
89
        $configurationFile = $input->getOption('configuration-file');
90
91
        if ($configurationFile !== '' && $input->getOption('typoscript-linter-configuration')) {
92
            $configReader = new YamlConfigurationReader($configurationFile);
93
        }
94
95
        if ($configurationFile !== '' && $input->getOption('grumphp-configuration')) {
96
            $configReader = new GrumphpConfigurationReader($configurationFile);
97
        }
98
99
        $configuration->init($configReader);
100
    }
101
}
102