Completed
Push — master ( 4a413b...ad9281 )
by Gytis
03:47
created

getRealFilePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
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