Failed Conditions
Pull Request — master (#4217)
by Owen
25:22 queued 14:30
created

findPolyfill()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 32
rs 9.2728
1
#!/usr/bin/env php
2
<?php
3
4
function findPolyfill(string $directory): int
5
{
6
    // See issue #4215 - code which should have erred in unit test
7
    // succeeded because a dev package required polyfills.
8
    $retCode = 0;
9
    $polyfill81 = 'MYSQLI_REFRESH_REPLICA\\b'
10
        . '|fdiv[(]'
11
        . '|array_is_list[(]'
12
        . '|enum_exists[(]';
13
    $polyfill = '/\\b(?:'
14
        . $polyfill81
15
        . '})/';
16
17
    $it = new RecursiveIteratorIterator(
18
        new RecursiveDirectoryIterator($directory, FilesystemIterator::UNIX_PATHS)
19
    );
20
21
    foreach ($it as $file) {
22
        if ($file->getExtension() === 'php') {
23
            $fullname = $it->getPath() . '/' . $it->getBaseName();
2 ignored issues
show
Bug introduced by
The method getPath() does not exist on RecursiveIteratorIterator. ( Ignorable by Annotation )

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

23
            $fullname = $it->/** @scrutinizer ignore-call */ getPath() . '/' . $it->getBaseName();

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...
Bug introduced by
The method getBaseName() does not exist on RecursiveIteratorIterator. ( Ignorable by Annotation )

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

23
            $fullname = $it->getPath() . '/' . $it->/** @scrutinizer ignore-call */ getBaseName();

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...
24
            $contents = file_get_contents($fullname);
25
            if ($contents === false) {
26
                echo "failed to read $fullname\n";
27
                ++$retCode;
28
            } elseif (preg_match_all($polyfill, $contents, $matches)) {
29
                var_dump($fullname, $matches);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($fullname, $matches) looks like debug code. Are you sure you do not want to remove it?
Loading history...
30
                ++$retCode;
31
            }
32
        }
33
    }
34
35
    return $retCode;
36
}
37
38
// Don't care if tests use polyfill
39
$errors = findPolyfill(__DIR__ . '/../src') + findPolyfill(__DIR__ . '/../samples');
40
if ($errors !== 0) {
41
    echo "Found $errors files that might require polyfills\n";
42
    exit(1);
43
}
44
echo "No polyfills needed\n";
45