-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathTabCreator.php
More file actions
133 lines (109 loc) · 3.13 KB
/
TabCreator.php
File metadata and controls
133 lines (109 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php declare(strict_types = 1);
namespace PHPStan\IssueBot\Playground;
// phpcs:ignoreFile
use function array_filter;
use function array_map;
use function array_values;
use function count;
use function floor;
use function ksort;
use function sprintf;
use function str_starts_with;
use function usort;
use const SORT_NUMERIC;
class TabCreator
{
/**
* @param array<int, list<PlaygroundError>> $versionedErrors
* @return list<PlaygroundResultTab>
*/
public function create(array $versionedErrors): array
{
ksort($versionedErrors, SORT_NUMERIC);
$versions = [];
$last = null;
foreach ($versionedErrors as $phpVersion => $errors) {
$errors = array_filter($errors, static fn (PlaygroundError $error) => $error->getIdentifier() !== 'phpstanPlayground.configParameter')
|> array_values(...)
|> (static fn ($errors) => array_map(static function (PlaygroundError $error): PlaygroundError {
if ($error->getIdentifier() === null) {
return $error;
}
if (!str_starts_with($error->getIdentifier(), 'phpstanPlayground.')) {
return $error;
}
return new PlaygroundError(
$error->getLine(),
sprintf('Tip: %s', $error->getMessage()),
$error->getIdentifier(),
);
}, $errors));
$current = [
'versions' => [$phpVersion],
'errors' => $errors,
];
if ($last === null) {
$last = $current;
continue;
}
if (count($errors) !== count($last['errors'])) {
$versions[] = $last;
$last = $current;
continue;
}
$merge = true;
foreach ($errors as $i => $error) {
$lastError = $last['errors'][$i];
if ($error->getLine() !== $lastError->getLine()) {
$versions[] = $last;
$last = $current;
$merge = false;
break;
}
if ($error->getMessage() !== $lastError->getMessage()) {
$versions[] = $last;
$last = $current;
$merge = false;
break;
}
}
if (!$merge) {
continue;
}
$last['versions'][] = $phpVersion;
}
if ($last !== null) {
$versions[] = $last;
}
usort($versions, static function ($a, $b): int {
$aVersion = $a['versions'][count($a['versions']) - 1];
$bVersion = $b['versions'][count($b['versions']) - 1];
return $bVersion - $aVersion;
});
$tabs = [];
foreach ($versions as $version) {
$title = 'PHP ';
if (count($version['versions']) > 1) {
$title .= $this->versionNumberToString($version['versions'][0]);
$title .= ' – ';
$title .= $this->versionNumberToString($version['versions'][count($version['versions']) - 1]);
} else {
$title .= $this->versionNumberToString($version['versions'][0]);
}
if (count($version['errors']) === 1) {
$title .= ' (1 error)';
} elseif (count($version['errors']) > 0) {
$title .= ' (' . count($version['errors']) . ' errors)';
}
$tabs[] = new PlaygroundResultTab($title, $version['errors']);
}
return $tabs;
}
private function versionNumberToString(int $versionId): string
{
$first = (int) floor($versionId / 10000);
$second = (int) floor(($versionId % 10000) / 100);
$third = (int) floor($versionId % 100);
return $first . '.' . $second . ($third !== 0 ? '.' . $third : '');
}
}