Passed
Push — master ( 5f6d74...e83163 )
by
unknown
14:33 queued 01:29
created

checkPhpDocTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.9
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(): int
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 (!empty($errors)) {
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
26
        return 1;
27
    }
28
29
    return 0;
30
}
31
32
if (checkPhpDocTypes()) {
33
    exit(1);
34
}
35