Passed
Pull Request — master (#283)
by Théo
02:18
created

Validate::execute()   F

Complexity

Conditions 13
Paths 357

Size

Total Lines 82
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 82
rs 3.8208
c 0
b 0
f 0
cc 13
nc 357
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use Exception;
18
use KevinGH\Box\Console\OutputConfigurator;
19
use KevinGH\Box\Json\JsonValidationException;
20
use Symfony\Component\Console\Exception\RuntimeException;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
/**
28
 * @private
29
 */
30
final class Validate extends Configurable
31
{
32
    private const IGNORE_MESSAGES_OPTION = 'ignore-recommendations-and-warnings';
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function configure(): void
38
    {
39
        parent::configure();
40
41
        $this->setName('validate');
42
        $this->setDescription('Validates the configuration file');
43
        $this->setHelp(
44
            <<<'HELP'
45
The <info>%command.name%</info> command will validate the configuration file
46
and report any errors found, if any.
47
<comment>
48
  This command relies on a configuration file for loading
49
  PHAR packaging settings. If a configuration file is not
50
  specified through the <info>--configuration|-c</info> option, one of
51
  the following files will be used (in order): <info>box.json,
52
  box.json.dist</info>
53
</comment>
54
HELP
55
        );
56
        $this->addArgument(
57
            'file',
58
            InputArgument::OPTIONAL,
59
            'The configuration file. (default: box.json, box.json.dist)'
60
        );
61
        $this->addOption(
62
            self::IGNORE_MESSAGES_OPTION,
63
            'i',
64
            InputOption::VALUE_NONE,
65
            'Will not return a faulty code when a recommendation or warning is found'
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output): int
73
    {
74
        $io = new SymfonyStyle($input, $output);
75
76
        OutputConfigurator::configure($output);
77
78
        try {
79
            $config = $this->getConfig($input, $output);
80
81
            $recommendations = $config->getRecommendations();
82
            $warnings = $config->getWarnings();
83
84
            if ([] === $recommendations) {
85
                $output->writeln('No recommendation found.');
86
            } else {
87
                $output->writeln('Recommendations:');
88
89
                foreach ($recommendations as $recommendation) {
90
                    $io->writeln("    - <recommendation>$recommendation</recommendation>");
91
                }
92
            }
93
94
            if ([] === $warnings) {
95
                $output->writeln('No warning found.');
96
            } else {
97
                $output->writeln('Warnings:');
98
99
                foreach ($warnings as $warning) {
100
                    $io->writeln("    - <warning>$warning</warning>");
101
                }
102
            }
103
104
            $output->writeln('');
105
            $output->writeln(
106
                '<info>The configuration file passed validation.</info>'
107
            );
108
109
            return (([] === $recommendations && [] === $warnings) || $input->getOption(self::IGNORE_MESSAGES_OPTION)) ? 0 : 1;
110
        } catch (Exception $exception) {
111
            // Continue
112
        }
113
114
        if ($output->isVerbose()) {
115
            throw new RuntimeException(
116
                sprintf(
117
                    'The configuration file failed validation: %s',
118
                    $exception->getMessage()
119
                ),
120
                $exception->getCode(),
121
                $exception
122
            );
123
        }
124
125
        if ($exception instanceof JsonValidationException) {
126
            $output->writeln(
127
                sprintf(
128
                    '<error>The configuration file failed validation: "%s" does not match the expected JSON '
129
                    .'schema:</error>',
130
                    $exception->getValidatedFile()
131
                )
132
            );
133
134
            $output->writeln('');
135
136
            foreach ($exception->getErrors() as $error) {
137
                $output->writeln("<comment>  - $error</comment>");
138
            }
139
        } else {
140
            $errorMessage = isset($exception)
141
                ? sprintf('The configuration file failed validation: %s', $exception->getMessage())
142
                : 'The configuration file failed validation.'
143
            ;
144
145
            $output->writeln(
146
                sprintf(
147
                    '<error>%s</error>',
148
                    $errorMessage
149
                )
150
            );
151
        }
152
153
        return 1;
154
    }
155
}
156