Checker::printCheck()   C
last analyzed

Complexity

Conditions 13
Paths 160

Size

Total Lines 73
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 73
rs 6.1166
c 0
b 0
f 0
cc 13
nc 160
nop 3

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\RequirementChecker;
16
17
use InvalidArgumentException;
18
use function count;
19
use function sprintf;
20
21
/**
22
 * @private
23
 *
24
 * @see bin/requirements-checker.php
25
 *
26
 * @license MIT (c) Fabien Potencier <[email protected]>
27
 */
28
final class Checker
29
{
30
    /** @var null|string */
31
    private static $requirementsConfig;
32
33
    public static function checkRequirements(): bool
34
    {
35
        $requirements = self::retrieveRequirements();
36
37
        $checkPassed = $requirements->evaluateRequirements();
38
39
        $io = new IO();
40
41
        self::printCheck(
42
            $checkPassed,
43
            new Printer(
44
                $io->getVerbosity(),
45
                $io->hasColorSupport()
46
            ),
47
            $requirements
48
        );
49
50
        return $checkPassed;
51
    }
52
53
    public static function printCheck($checkPassed, Printer $printer, RequirementCollection $requirements): void
54
    {
55
        if (false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $printer->getVerbosity()) {
56
            // Override the default verbosity to output errors regardless of the verbosity asked by the user
57
            $printer->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
58
        }
59
60
        $verbosity = IO::VERBOSITY_VERY_VERBOSE;
61
62
        $iniPath = $requirements->getPhpIniPath();
63
64
        $printer->title('Box Requirements Checker', $verbosity);
65
66
        $printer->printv('> Using PHP ', $verbosity);
67
        $printer->printvln(PHP_VERSION, $verbosity, 'green');
68
69
        if ($iniPath) {
70
            $printer->printvln('> PHP is using the following php.ini file:', $verbosity);
71
            $printer->printvln('  '.$iniPath, $verbosity, 'green');
72
        } else {
73
            $printer->printvln('> PHP is not using any php.ini file.', $verbosity, 'yellow');
74
        }
75
76
        $printer->printvln('', $verbosity);
77
78
        if (count($requirements) > 0) {
79
            $printer->printvln('> Checking Box requirements:', $verbosity);
80
            $printer->printv('  ', $verbosity);
81
        } else {
82
            $printer->printvln('> No requirements found.', $verbosity);
83
        }
84
85
        $errorMessages = [];
86
87
        foreach ($requirements->getRequirements() as $requirement) {
88
            if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
89
                if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
90
                    $printer->printvln('✘ '.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
91
                    $printer->printv('  ', IO::VERBOSITY_DEBUG);
92
                    $errorMessages[] = $errorMessage;
93
                } else {
94
                    $printer->printv('E', $verbosity, 'red');
95
                    $errorMessages[] = $errorMessage;
96
                }
97
98
                continue;
99
            }
100
101
            if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
102
                $printer->printvln('✔ '.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'green');
103
                $printer->printv('  ', IO::VERBOSITY_DEBUG);
104
            } else {
105
                $printer->printv('.', $verbosity, 'green');
106
            }
107
        }
108
109
        if (IO::VERBOSITY_DEBUG !== $printer->getVerbosity() && count($requirements) > 0) {
110
            $printer->printvln('', $verbosity);
111
        }
112
113
        if ($requirements->evaluateRequirements()) {
114
            $printer->block('OK', 'Your system is ready to run the application.', $verbosity, 'success');
115
        } else {
116
            $printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
117
118
            $printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
119
120
            foreach ($errorMessages as $errorMessage) {
121
                $printer->printv(' * '.$errorMessage, $verbosity);
122
            }
123
        }
124
125
        $printer->printvln('', $verbosity);
126
    }
127
128
    private static function retrieveRequirements(): RequirementCollection
129
    {
130
        if (null === self::$requirementsConfig) {
131
            self::$requirementsConfig = __DIR__.'/../.requirements.php';
132
        }
133
134
        /** @var list<array{type:string, condition:string, message:string, helpMessage:string}> $config */
135
        $config = require self::$requirementsConfig;
136
137
        $requirements = new RequirementCollection();
138
139
        foreach ($config as $constraint) {
140
            $requirements->addRequirement(
141
                self::createCondition(
142
                    $constraint['type'],
143
                    $constraint['condition']
144
                ),
145
                $constraint['message'],
146
                $constraint['helpMessage']
147
            );
148
        }
149
150
        return $requirements;
151
    }
152
153
    /**
154
     * @param string $type
155
     * @param string $condition
156
     */
157
    private static function createCondition($type, $condition): IsFulfilled
158
    {
159
        switch ($type) {
160
            case 'php':
161
                return new IsPhpVersionFulfilled($condition);
162
163
            case 'extension':
164
                return new IsExtensionFulfilled($condition);
165
166
            case 'extension-conflict':
167
                return new IsExtensionConflictFulfilled($condition);
168
169
            default:
170
                throw new InvalidArgumentException(
171
                    sprintf(
172
                        'Unknown requirement type "%s".',
173
                        $type
174
                    )
175
                );
176
        }
177
    }
178
}
179