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

Checker::printCheck()   C

Complexity

Conditions 11
Paths 80

Size

Total Lines 66
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 5.9447
c 0
b 0
f 0
cc 11
eloc 40
nc 80
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
/*
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\Box\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 = '.requirements.php';
24
25
    /**
26
     * @return bool
27
     */
28
    public static function checkRequirements()
29
    {
30
        $requirements = self::retrieveRequirements();
0 ignored issues
show
Bug introduced by
The method retrieveRequirements() does not exist on KevinGH\Box\RequirementChecker\Checker. ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        $requirements = self::retrieveRequirements();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
32
        $checkPassed = $requirements->evaluateRequirements();
33
34
        $io = new IO();
35
36
        if (false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $io->getVerbosity()) {
37
            // Override the default verbosity to output errors regardless of the verbosity asked by the user
38
            $io->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
39
        }
40
41
        self::printCheck(
0 ignored issues
show
Bug introduced by
The method printCheck() does not exist on KevinGH\Box\RequirementChecker\Checker. Did you maybe mean print()? ( Ignorable by Annotation )

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

41
        self::/** @scrutinizer ignore-call */ 
42
              printCheck(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
            new Printer(
43
                $io->getVerbosity(),
44
                $io->hasColorSupport()
45
            ),
46
            $requirements
47
        );
48
49
        return $checkPassed;
50
    }
51
52
    public static function printCheck(Printer $printer, RequirementCollection $requirements)
53
    {
54
        $verbosity = IO::VERBOSITY_VERY_VERBOSE;
55
56
        $iniPath = $requirements->getPhpIniPath();
57
58
        $printer->title('Box Requirements Checker', $verbosity);
59
60
        $printer->printvln('> PHP is using the following php.ini file:', $verbosity);
61
62
        if ($iniPath) {
63
            $printer->printvln('  '.$iniPath, $verbosity, 'green');
64
        } else {
65
            $printer->printvln('  WARNING: No configuration file (php.ini) used by PHP!', $verbosity, 'yellow');
66
        }
67
68
        $printer->printvln('', $verbosity);
69
70
        if (count($requirements) > 0) {
71
            $printer->printvln('> Checking Box requirements:', $verbosity);
72
            $printer->printv('  ', $verbosity);
73
        } else {
74
            $printer->printvln('> No requirements found.', $verbosity);
75
        }
76
77
        $errorMessages = array();
78
79
        foreach ($requirements->getRequirements() as $requirement) {
80
            if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
81
                if ($printer->getVerbosity() === IO::VERBOSITY_DEBUG) {
82
                    $printer->printvln('✘ '.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
83
                    $printer->printv('  ', IO::VERBOSITY_DEBUG);
84
                    $errorMessages[] = $errorMessage;
85
                } else {
86
                    $printer->printv('E', $verbosity, 'red');
87
                    $errorMessages[] = $errorMessage;
88
                }
89
90
                continue;
91
            }
92
93
            if ($printer->getVerbosity() === IO::VERBOSITY_DEBUG) {
94
                $printer->printvln('✔ '.$requirement->getHelpText(), IO::VERBOSITY_DEBUG, 'green');
95
                $printer->printv('  ', IO::VERBOSITY_DEBUG);
96
            } else {
97
                $printer->printv('.', $verbosity, 'green');
98
            }
99
        }
100
101
        if ($printer->getVerbosity() !== IO::VERBOSITY_DEBUG && count($requirements) > 0) {
102
            $printer->printvln('', $verbosity);
103
        }
104
105
        if ($requirements->evaluateRequirements()) {
106
            $printer->block('OK', 'Your system is ready to run the application.', $verbosity, 'success');
107
        } else {
108
            $printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
109
110
            $printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
111
112
            foreach ($errorMessages as $errorMessage) {
113
                $printer->printv(' * '.$errorMessage, $verbosity);
114
            }
115
        }
116
117
        $printer->printvln('', $verbosity);
118
    }
119
120
    /**
121
     * @return RequirementCollection
122
     */
123
    private static function retrieveRequirements()
124
    {
125
        $config = require self::REQUIREMENTS_CONFIG;
0 ignored issues
show
Bug introduced by
The constant KevinGH\Box\RequirementC...er::REQUIREMENTS_CONFIG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
126
127
        $requirements = new RequirementCollection();
128
129
        foreach ($config as $constraint) {
130
            call_user_func_array(array($requirements, 'addRequirement'), $constraint);
131
        }
132
133
        return $requirements;
134
    }
135
}
136