GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch 0.16.0-dev (b16ca7)
by Gytis
04:00
created

diffBaseline()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 38
c 3
b 0
f 0
nc 7
nop 2
dl 0
loc 51
rs 8.0675

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php // phpcs:ignore SR1.Files.SideEffects.FoundWithSymbols
2
3
$baselineFilePath = $argv[1] ?? null;
4
$targetFilePath = $argv[2] ?? null;
5
diffBaseline($baselineFilePath, $targetFilePath);
0 ignored issues
show
Bug introduced by
It seems like $targetFilePath can also be of type null; however, parameter $targetFilePath of diffBaseline() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

5
diffBaseline($baselineFilePath, /** @scrutinizer ignore-type */ $targetFilePath);
Loading history...
Bug introduced by
It seems like $baselineFilePath can also be of type null; however, parameter $baselineFilePath of diffBaseline() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

5
diffBaseline(/** @scrutinizer ignore-type */ $baselineFilePath, $targetFilePath);
Loading history...
6
7
function diffBaseline(string $baselineFilePath, string $targetFilePath): void
8
{
9
    $realBaselineFilePath = getRealFilePath($baselineFilePath);
10
    $realTargetFilePath = getRealFilePath($targetFilePath);
11
12
    $baselineXml = file_get_contents($realBaselineFilePath);
13
14
    $matches = [];
15
    preg_match_all('#\[(\w{16})\]#', $baselineXml, $matches);
16
17
    $ignoredHashMap = array_flip($matches[1]);
18
    unset($matches);
19
20
    $remainingWarningCount = 0;
21
    $removedWarningCount = 0;
22
    $newFileLines = [];
23
    $errorFileLine = null;
24
    $errorLines = [];
25
    $realTargetFile = fopen($realTargetFilePath, 'r');
26
    while (!feof($realTargetFile)) {
27
        $line = fgets($realTargetFile);
28
29
        if (false !== strpos($line, '<file')) {
30
            $errorFileLine = $line;
31
        } elseif (false !== strpos($line, '</file')) {
32
            if (!empty($errorLines)) {
33
                array_push($newFileLines, $errorFileLine, ...$errorLines);
34
                array_push($newFileLines, $line);
35
            }
36
            $errorFileLine = null;
37
            $errorLines = [];
38
        } elseif (false !== strpos($line, '<error')) {
39
            $matches = [];
40
            preg_match('#\[(\w{16})\]#', $line, $matches);
41
            $errorHash = $matches[1] ?? null;
42
            if (null !== $errorHash && key_exists($errorHash, $ignoredHashMap)) {
43
                $removedWarningCount++;
44
            } else {
45
                $remainingWarningCount++;
46
                $errorLines[] = $line;
47
            }
48
        } else {
49
            $newFileLines[] = $line;
50
        }
51
    }
52
    fclose($realTargetFile);
53
54
    echo sprintf('Found %s warning(s) in %s.%s', count($ignoredHashMap), $baselineFilePath, PHP_EOL);
55
    echo sprintf('Removed %s warning(s) from %s, %s warning(s) remain.%s', $removedWarningCount, $targetFilePath, $remainingWarningCount, PHP_EOL);
56
57
    file_put_contents($realTargetFilePath, implode('', $newFileLines));
58
}
59
60
function getRealFilePath(string $filePath): string
61
{
62
    if ('.' === $filePath[0]) {
63
        $realFilePath = realpath(getcwd() . '/' . $filePath);
64
    } else {
65
        $realFilePath = $filePath;
66
    }
67
68
    if (!file_exists($realFilePath)) {
69
        throw new Exception(sprintf('File "%s" does not exist!', $filePath));
70
    }
71
72
    return $realFilePath;
73
}
74