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

Checker::printCheck()   D

Complexity

Conditions 13
Paths 160

Size

Total Lines 74
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 4.9869
c 0
b 0
f 0
cc 13
eloc 44
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
/*
4
 * This file is part of the box project.
5
 *
6
 * (c) Kevin Herrera <[email protected]>
7
 *     Théo Fidry <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace KevinGH\RequirementChecker;
14
15
/**
16
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
17
 *
18
 * @private
19
 */
20
final class Checker
21
{
22
    /** @private */
23
    const REQUIREMENTS_CONFIG = __DIR__.'/../.requirements.php';
24
25
    /**
26
     * @return bool
27
     */
28
    public static function checkRequirements()
29
    {
30
        $requirements = self::retrieveRequirements();
31
32
        $checkPassed = $requirements->evaluateRequirements();
33
34
        $io = new IO();
35
36
        self::printCheck(
37
            $checkPassed,
38
            new Printer(
39
                $io->getVerbosity(),
40
                $io->hasColorSupport()
41
            ),
42
            $requirements
43
        );
44
45
        return $checkPassed;
46
    }
47
48
    public static function printCheck($checkPassed, Printer $printer, RequirementCollection $requirements)
49
    {
50
        if (false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $printer->getVerbosity()) {
51
            // Override the default verbosity to output errors regardless of the verbosity asked by the user
52
            $printer->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
53
        }
54
55
        $verbosity = IO::VERBOSITY_VERY_VERBOSE;
56
57
        $iniPath = $requirements->getPhpIniPath();
58
59
        $printer->title('Box Requirements Checker', $verbosity);
60
61
        $printer->printv('> Using PHP ', $verbosity);
62
        $printer->printvln(PHP_VERSION, $verbosity, 'green');
63
64
        $printer->printvln('> PHP is using the following php.ini file:', $verbosity);
65
66
        if ($iniPath) {
67
            $printer->printvln('  '.$iniPath, $verbosity, 'green');
68
        } else {
69
            $printer->printvln('  WARNING: No configuration file (php.ini) used by PHP!', $verbosity, 'yellow');
70
        }
71
72
        $printer->printvln('', $verbosity);
73
74
        if (\count($requirements) > 0) {
75
            $printer->printvln('> Checking Box requirements:', $verbosity);
76
            $printer->printv('  ', $verbosity);
77
        } else {
78
            $printer->printvln('> No requirements found.', $verbosity);
79
        }
80
81
        $errorMessages = array();
82
83
        foreach ($requirements->getRequirements() as $requirement) {
84
            if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
85
                if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
86
                    $printer->printvln('✘ '.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
87
                    $printer->printv('  ', IO::VERBOSITY_DEBUG);
88
                    $errorMessages[] = $errorMessage;
89
                } else {
90
                    $printer->printv('E', $verbosity, 'red');
91
                    $errorMessages[] = $errorMessage;
92
                }
93
94
                continue;
95
            }
96
97
            if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
98
                $printer->printvln('✔ '.$requirement->getHelpText(), IO::VERBOSITY_DEBUG, 'green');
99
                $printer->printv('  ', IO::VERBOSITY_DEBUG);
100
            } else {
101
                $printer->printv('.', $verbosity, 'green');
102
            }
103
        }
104
105
        if (IO::VERBOSITY_DEBUG !== $printer->getVerbosity() && \count($requirements) > 0) {
106
            $printer->printvln('', $verbosity);
107
        }
108
109
        if ($requirements->evaluateRequirements()) {
110
            $printer->block('OK', 'Your system is ready to run the application.', $verbosity, 'success');
111
        } else {
112
            $printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
113
114
            $printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
115
116
            foreach ($errorMessages as $errorMessage) {
117
                $printer->printv(' * '.$errorMessage, $verbosity);
118
            }
119
        }
120
121
        $printer->printvln('', $verbosity);
122
    }
123
124
    /**
125
     * @return RequirementCollection
126
     */
127
    private static function retrieveRequirements()
128
    {
129
        $config = require self::REQUIREMENTS_CONFIG;
130
131
        $requirements = new RequirementCollection();
132
133
        foreach ($config as $constraint) {
134
            \call_user_func_array(array($requirements, 'addRequirement'), $constraint);
135
        }
136
137
        return $requirements;
138
    }
139
}
140