1
|
|
|
<?php // phpcs:ignore SR1.Files.SideEffects.FoundWithSymbols |
2
|
|
|
|
3
|
|
|
$baselineFilePath = $argv[1] ?? null; |
4
|
|
|
$targetFilePath = $argv[2] ?? null; |
5
|
|
|
diffBaseline($baselineFilePath, $targetFilePath); |
|
|
|
|
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
|
|
|
|