Completed
Push — master ( 866a9a...e93a4a )
by Théo
02:11
created

check.php ➔ echo_style()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 2
nop 3
dl 0
loc 20
ccs 0
cts 5
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the humbug/php-scoper package.
5
 *
6
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
7
 *                    Pádraic Brady <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Humbug\PhpScoper;
14
15
use Humbug\PhpScoper\Autoload\Requirements;
16
use Symfony\Requirements\Requirement;
17
18
//
19
// Code in this file must be PHP 5.3+ compatible as is used to know if PHP-Scoper can be run or not.
20
//
21
22
/**
23
 * @param string $composerJson
24
 * @param bool   $verbose
25
 *
26
 * @return bool
27
 */
28
function check_requirements($composerJson, $verbose)
29
{
30
    $lineSize = 70;
31
    $requirements = new Requirements($composerJson);
32
    $iniPath = $requirements->getPhpIniPath();
33
34
    $checkPassed = array_reduce(
35
        $requirements->getRequirements(),
36
        /**
37
         * @param bool        $checkPassed
38
         * @param Requirement $requirement
39
         *
40
         * @return bool
41
         */
42
        function ($checkPassed, Requirement $requirement) use ($lineSize) {
43
            return $checkPassed || null === get_error_message($requirement, $lineSize);
44
        },
45
        false
46
    );
47
48
    if (false === $checkPassed) {
49
        // Override the default verbosity to output errors regardless of the verbosity asked by the user
50
        $verbose = true;
51
    }
52
53
    echo_title('PHP-Scoper Requirements Checker', null, $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_title() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
55
    vecho('> PHP is using the following php.ini file:'.PHP_EOL, $verbose);
56
57
    if ($iniPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $iniPath of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
        echo_style('green', '  '.$iniPath, $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
    } else {
60
        echo_style('yellow', '  WARNING: No configuration file (php.ini) used by PHP!', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
    }
62
63
    vecho(PHP_EOL.PHP_EOL, $verbose);
64
    vecho('> Checking PHP-Scoper requirements:'.PHP_EOL.'  ', $verbose);
65
66
    $messages = [];
67
68
    foreach ($requirements->getRequirements() as $requirement) {
69 View Code Duplication
        if ($helpText = get_error_message($requirement, $lineSize)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            echo_style('red', 'E', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
            $messages['error'][] = $helpText;
72
        } else {
73
            echo_style('green', '.', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
74
        }
75
    }
76
77
    foreach ($requirements->getRecommendations() as $requirement) {
78 View Code Duplication
        if ($helpText = get_error_message($requirement, $lineSize)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            echo_style('yellow', 'W', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
            $messages['warning'][] = $helpText;
81
        } else {
82
            echo_style('green', '.', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
        }
84
    }
85
86
    if ($checkPassed) {
87
        echo_block('success', 'OK', 'Your system is ready to run PHP-Scoper.', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_block() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
88
    } else {
89
        echo_block('error', 'ERROR', 'Your system is not ready to run PHP-Scoper', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_block() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
90
91
        echo_title('Fix the following mandatory requirements', 'red', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_title() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
92
93
        foreach ($messages['error'] as $helpText) {
94
            vecho(' * '.$helpText.PHP_EOL, $verbose);
95
        }
96
    }
97
98
    if (!empty($messages['warning'])) {
99
        echo_title('Optional recommendations to improve your setup', 'yellow', $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_title() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
101
        foreach ($messages['warning'] as $helpText) {
102
            vecho(' * '.$helpText.PHP_EOL, $verbose);
103
        }
104
    }
105
106
    return $checkPassed;
107
}
108
109
function vecho($message, $verbose)
110
{
111
    if (false === $verbose) {
112
        return;
113
    }
114
115
    echo $message;
116
}
117
118
/**
119
 * @param Requirement $requirement
120
 * @param int         $lineSize
121
 *
122
 * @return string|null
123
 */
124
function get_error_message(Requirement $requirement, $lineSize)
125
{
126
    if ($requirement->isFulfilled()) {
127
        return null;
128
    }
129
130
    $errorMessage = wordwrap($requirement->getTestMessage(), $lineSize - 3, PHP_EOL.'   ').PHP_EOL;
131
132
    if ('' !== $requirement->getHelpText()) {
133
        $errorMessage .= '   > '.wordwrap($requirement->getHelpText(), $lineSize - 5, PHP_EOL.'   > ').PHP_EOL;
134
    }
135
136
    return $errorMessage;
137
}
138
139
/**
140
 * @param string      $title
141
 * @param string|null $style
142
 * @param bool        $verbose
143
 */
144
function echo_title($title, $style = null, $verbose)
145
{
146
    if (false === $verbose) {
147
        return;
148
    }
149
150
    $style = $style ?: 'title';
151
152
    echo PHP_EOL;
153
    echo_style($style, $title.PHP_EOL, $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
154
    echo_style($style, str_repeat('~', strlen($title)).PHP_EOL, $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
155
    echo PHP_EOL;
156
}
157
158
/**
159
 * @param string $style
160
 * @param string $message
161
 * @param bool   $verbose
162
 */
163
function echo_style($style, $message, $verbose)
164
{
165
    if (false === $verbose) {
166
        return;
167
    }
168
169
    // ANSI color codes
170
    $styles = array(
171
        'reset' => "\033[0m",
172
        'red' => "\033[31m",
173
        'green' => "\033[32m",
174
        'yellow' => "\033[33m",
175
        'error' => "\033[37;41m",
176
        'success' => "\033[37;42m",
177
        'title' => "\033[34m",
178
    );
179
    $supports = has_color_support();
180
181
    echo($supports ? $styles[$style] : '').$message.($supports ? $styles['reset'] : '');
182
}
183
184
/**
185
 * @param string $style
186
 * @param string $title
187
 * @param string $message
188
 * @param bool   $verbose
189
 */
190
function echo_block($style, $title, $message, $verbose)
191
{
192
    if (false === $verbose) {
193
        return;
194
    }
195
196
    $message = ' '.trim($message).' ';
197
    $width = strlen($message);
198
199
    echo PHP_EOL.PHP_EOL;
200
201
    echo_style($style, str_repeat(' ', $width), $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
202
    echo PHP_EOL;
203
    echo_style($style, str_pad(' ['.$title.']', $width, ' ', STR_PAD_RIGHT), $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
204
    echo PHP_EOL;
205
    echo_style($style, $message, $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
206
    echo PHP_EOL;
207
    echo_style($style, str_repeat(' ', $width), $verbose);
0 ignored issues
show
Unused Code introduced by
The call to echo_style() has too many arguments starting with $verbose.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
208
    echo PHP_EOL;
209
}
210
211
/**
212
 * @return bool
213
 */
214
function has_color_support()
215
{
216
    static $support;
217
218
    if (null === $support) {
219
        if (DIRECTORY_SEPARATOR == '\\') {
220
            $support = false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
221
        } else {
222
            $support = function_exists('posix_isatty') && @posix_isatty(STDOUT);
223
        }
224
    }
225
226
    return $support;
227
}
228