Test Failed
Push — master ( 4e8938...efe3fd )
by Peter
08:14 queued 11s
created

YamlCommand::isFileExcluded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 2
cts 2
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Yaml\Exception\ParseException;
13
use YamlStandards\Command\Service\ProcessOutputService;
14
use YamlStandards\Command\Service\ResultService;
15
use YamlStandards\Model\Component\YamlService;
16
use YamlStandards\Model\Config\YamlStandardConfigLoader;
17
use YamlStandards\Result\Result;
18
19
class YamlCommand extends Command
20
{
21
    private const COMMAND_NAME = 'yaml-standards';
22
23
    public const ARGUMENT_PATH_TO_CONFIG_FILE = 'pathToConfigFile';
24
    public const OPTION_FIX = 'fix';
25
26
    protected static $defaultName = self::COMMAND_NAME;
27
28
    protected function configure(): void
29
    {
30
        $this
31
            ->setName(self::COMMAND_NAME) // set command name for symfony/console lower version as 3.4
32
            ->setDescription('Check yaml files respect standards')
33
            ->addArgument(self::ARGUMENT_PATH_TO_CONFIG_FILE, InputArgument::OPTIONAL, 'Path to configuration file. By default configuration file is looking in root directory', './yaml-standards.yaml')
34
            ->addOption(self::OPTION_FIX, null, InputOption::VALUE_NONE, 'Automatically fix problems');
35
    }
36
37
    /**
38 5
     * @inheritDoc
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output): ?int
41 5
    {
42 5
        Reporting::startTiming();
43 5
44 5
        $inputSettingData = new InputSettingData($input);
45 5
        $yamlStandardConfigLoader = new YamlStandardConfigLoader();
46 5
        $yamlStandardConfigTotalData = $yamlStandardConfigLoader->loadFromYaml($inputSettingData->getPathToConfigFile());
47 5
48 5
        $processOutput = new ProcessOutput($yamlStandardConfigTotalData->getTotalCountOfYamlFiles());
49 5
        $results = [[]];
50 5
51 5
        foreach ($yamlStandardConfigTotalData->getYamlStandardConfigsSingleData() as $yamlStandardConfigSingleData) {
52 5
            foreach ($yamlStandardConfigSingleData->getPathToYamlFiles() as $pathToYamlFile) {
53
                $fileResults = [];
54
                if ($this->isFileExcluded($pathToYamlFile, $yamlStandardConfigSingleData->getPathToExcludedYamlFiles())) {
55
                    $output->write($processOutput->process(ProcessOutput::STATUS_CODE_SKIPP));
56
                    continue;
57 5
                }
58
59 5
                if (is_readable($pathToYamlFile) === false) {
60
                    $message = 'File is not readable.';
61 5
                    $fileResults[] = new Result($pathToYamlFile, Result::RESULT_CODE_GENERAL_ERROR, ProcessOutput::STATUS_CODE_ERROR, $message);
62
                    $output->write($processOutput->process(ProcessOutput::STATUS_CODE_ERROR));
63 5
                    continue;
64 5
                }
65 5
66
                try {
67 5
                    // check yaml is valid
68 5
                    YamlService::getYamlData($pathToYamlFile);
69 5
70
                    foreach ($yamlStandardConfigSingleData->getYamlStandardConfigsSingleStandardData() as $yamlStandardConfigSingleCheckerData) {
71 5
                        $standardParametersData = $yamlStandardConfigSingleCheckerData->getStandardParametersData();
72 5
                        $fixer = $yamlStandardConfigSingleCheckerData->getFixer();
73 5
                        if ($fixer !== null && $inputSettingData->isFixEnabled()) {
74
                            $fileResults[] = $fixer->fix($pathToYamlFile, $pathToYamlFile, $standardParametersData);
75
                        } else {
76
                            $fileResults[] = $yamlStandardConfigSingleCheckerData->getChecker()->check($pathToYamlFile, $standardParametersData);
77
                        }
78 5
                    }
79
                } catch (ParseException $e) {
80
                    $message = sprintf('Unable to parse the YAML string: %s', $e->getMessage());
81
                    $fileResults[] = new Result($pathToYamlFile, Result::RESULT_CODE_GENERAL_ERROR, ProcessOutput::STATUS_CODE_ERROR, $message);
82
                }
83
84
                $results[] = $fileResults;
85
                $output->write($processOutput->process(ProcessOutputService::getWorstStatusCodeByResults($fileResults)));
86
            }
87 5
        }
88
        $output->writeln($processOutput->getLegend());
89 5
        $results = array_merge(...$results); // add all results to one array instead of multidimensional array with results for every file
90
91
        return $this->printOutput($output, $results);
92 5
    }
93 5
94
    /**
95
     * @param \Symfony\Component\Console\Output\OutputInterface $output
96
     * @param \YamlStandards\Result\Result[] $results
97
     * @return int
98
     */
99
    private function printOutput(OutputInterface $output, array $results): int
100 5
    {
101 5
        foreach ($results as $result) {
102
            if ($result->getStatusCode() !== ProcessOutput::STATUS_CODE_OK) {
103 5
                $output->writeln(sprintf('FILE: %s', $result->getPathToFile()));
104 5
                $output->writeln('-------------------------------------------------');
105
                $output->writeln($result->getMessage() . PHP_EOL);
106 5
107
                if ($result->canBeFixedByFixer()) {
108
                    $output->writeln('<fg=red>This can be fixed by `--fix` option</fg=red>' . PHP_EOL);
109
                }
110
            }
111
        }
112
113
        $output->writeln(Reporting::printRunTime());
114 5
115
        return ResultService::getResultCodeByResults($results);
116 5
    }
117 4
118
    /**
119
     * @param string $pathToYamlFile
120
     * @param string[] $pathToExcludedYamlFiles
121
     * @return bool
122
     */
123 4
    private function isFileExcluded($pathToYamlFile, array $pathToExcludedYamlFiles): bool
124
    {
125
        if (in_array($pathToYamlFile, $pathToExcludedYamlFiles, true)) {
126
            return true;
127
        }
128 5
129
        return false;
130 5
    }
131
}
132