Failed Conditions
Pull Request — master (#4439)
by Owen
17:04 queued 03:26
created

checkPhpDocTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
#!/usr/bin/env php
2
<?php
3
4
/**
5
 * This will check that the "current patch" does not add or modify lines that contain types as PHPDoc when we can express
6
 * them with PHP native types. The "current patch" is either the file about to be committed, the non-committed file, or
7
 * the latest commit.
8
 *
9
 * This will help us slowly migrate away from PHPDoc typing to PHP native typing.
10
 */
11
function checkPhpDocTypes(): void
12
{
13
    $content = shell_exec('git diff --cached') ?? shell_exec('git diff') ?? shell_exec('git show HEAD');
14
    preg_match_all('~^\+ +\* @(param|var) (mixed|string|int|float|bool|null|array|\?|\|)+( \$\w+)?$~m', "$content", $parameters);
15
    preg_match_all('~^\+ +\* @return (mixed|string|int|float|bool|null|array|void|\?|\|)+$~m', "$content", $returns);
16
17
    $errors = [
18
        ...$parameters[0],
19
        ...$returns[0],
20
    ];
21
22
    if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
$errors is a non-empty array, thus is always true.
Loading history...
23
        echo 'PHP native types must be used instead of PHPDoc types (without comments), for the following lines:' . PHP_EOL . PHP_EOL;
24
        echo implode(PHP_EOL, $errors) . PHP_EOL;
25
        exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
26
    }
27
}
28
29
checkPhpDocTypes();
30