Completed
Push — master ( 54c8a3...343be4 )
by John
01:14
created

src/yaml-lint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * yaml-lint, a compact command line utility for checking YAML file syntax.
5
 *
6
 * Uses the parsing facility of the Symfony Yaml Component.
7
 *
8
 * For full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use J13k\YamlLint\UsageException;
13
use Symfony\Component\Yaml\Exception\ParseException;
14
use Symfony\Component\Yaml\Yaml;
15
16
define('APP_NAME', 'yaml-lint');
17
define('APP_VERSION', '1.1.2');
18
19
define('ANSI_BLD', 01);
20
define('ANSI_UDL', 04);
21
define('ANSI_RED', 31);
22
define('ANSI_GRN', 32);
23
24
// Init app name and args
25
$appStr = APP_NAME . ' ' . APP_VERSION;
26
$argQuiet = false;
27
$argPath = null;
28
29
try {
30
31
    // Composer bootstrap
32
    $pathToTry = null;
33
    foreach (array('/../../../', '/../vendor/') as $pathToTry) {
34
        if (is_readable(__DIR__ . $pathToTry . 'autoload.php')) {
35
            /** @noinspection PhpIncludeInspection */
36
            require __DIR__ . $pathToTry . 'autoload.php';
37
            break;
38
        }
39
    }
40
    if (!class_exists('\Composer\Autoload\ClassLoader')) {
41
        throw new \Exception(_msg('composer'));
42
    }
43
44
    // Extract YAML component metadata
45
    $componentsManifest = __DIR__ . $pathToTry . 'composer/installed.json';
46
    $components = json_decode(file_get_contents($componentsManifest), true);
47
    foreach ($components as $component) {
48
        if ($component['name'] == 'symfony/yaml') {
49
            $appStr .= ', symfony/yaml ' . $component['version'];
50
            break;
51
        }
52
    }
53
54
    // Process and check args
55
    $argv = $_SERVER['argv'];
56
    array_shift($argv);
57
    foreach ($argv as $arg) {
58
        switch ($arg) {
59
            case '-h':
60
            case '--help':
61
                throw new UsageException();
62
            case '-V':
63
            case '--version':
64
                fwrite(STDOUT, $appStr . "\n");
65
                exit(0);
66
            case '-q':
67
            case '--quiet':
68
                $argQuiet = true;
69
                break;
70
            default:
71
                $argPath = $arg;
72
        }
73
    }
74
75
    if (!$argPath) {
76
        throw new UsageException('no input specified', 1);
77
    }
78
79
    if ($argPath === '-') {
80
        $path = 'php://stdin';
81
    } else {
82
        // Check input file
83
        if (!file_exists($argPath)) {
84
            throw new ParseException('File does not exist');
85
        }
86
        if (!is_readable($argPath)) {
87
            throw new ParseException('File is not readable');
88
        }
89
        $path = $argPath;
90
    }
91
    $content = file_get_contents($path);
92
    if (strlen($content) < 1) {
93
        throw new ParseException('Input has no content');
94
    }
95
96
    // Do the thing
97
    Yaml::parse($content, true);
0 ignored issues
show
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
99
    // Output app string and file path if allowed
100
    if (!$argQuiet) {
101
        fwrite(STDOUT, trim($appStr . ': parsing ' . $argPath));
102
        fwrite(STDOUT, sprintf(" [ %s ]\n", _ansify('OK', ANSI_GRN)));
103
    }
104
    exit(0);
105
106
} catch (UsageException $e) {
107
108
    // Usage message
109
    fwrite($e->getCode() ? STDERR : STDOUT, $appStr);
110
    if ($e->getMessage()) {
111
        fwrite(
112
            STDERR,
113
            sprintf(": %s", _ansify($e->getMessage(), ANSI_RED))
114
        );
115
    }
116
    fwrite(STDOUT, sprintf("\n\n%s\n\n", _msg('usage')));
117
    exit($e->getCode());
118
119
} catch (ParseException $e) {
120
121
    // Syntax exception
122
    fwrite(STDERR, trim($appStr . ': parsing ' . $argPath));
123
    fwrite(STDERR, sprintf(" [ %s ]\n", _ansify('ERROR', ANSI_RED)));
124
    fwrite(STDERR, "\n" . $e->getMessage() . "\n\n");
125
    exit(1);
126
127
} catch (\Exception $e) {
128
129
    // The rest
130
    fwrite(STDERR, $appStr);
131
    fwrite(STDERR, sprintf(": %s\n", _ansify($e->getMessage(), ANSI_RED)));
132
    exit(1);
133
134
}
135
136
/**
137
 * Helper to wrap input string in ANSI colour code
138
 *
139
 * @param string $str
140
 * @param int    $colourCode
141
 *
142
 * @return string
143
 */
144
function _ansify($str, $colourCode)
145
{
146
    $colourCode = max(0, $colourCode);
147
    $colourCode = min(255, $colourCode);
148
149
    return sprintf("\e[%dm%s\e[0m", $colourCode, $str);
150
}
151
152
/**
153
 * Wrapper for heredoc messages
154
 *
155
 * @param string $str
156
 *
157
 * @return string
158
 */
159
function _msg($str)
160
{
161
    switch ($str) {
162
        case 'composer':
163
            return <<<EOD
164
Composer dependencies cannot be loaded; install Composer to remedy:
165
https://getcomposer.org/download/
166
EOD;
167
            break;
168
        case 'usage':
169
            return <<<EOD
170
usage: yaml-lint [options] [input source]
171
172
  input source    Path to file, or "-" to read from standard input
173
174
  -q, --quiet     Restrict output to syntax errors
175
  -h, --help      Display this help
176
  -V, --version   Display application version
177
EOD;
178
            break;
179
        default:
180
    }
181
182
    return '';
183
}
184