@@ -12,32 +12,34 @@ final class OutdatedFiles
12
12
13
13
private Ignores $ ignores ;
14
14
15
- private PHP_CodeSniffer \Config $ config ;
16
-
17
15
private string $ outdatedVirtualFile ;
18
16
17
+ private int $ originalParallelCount ;
18
+
19
+ private int |NULL $ realParallelCount = NULL ;
20
+
19
21
private string |NULL $ outdatedDataFile = NULL ;
20
22
21
23
22
24
public function __construct (Ignores $ ignores , PHP_CodeSniffer \Config $ config , string $ outdatedVirtualFile )
23
25
{
24
26
$ this ->ignores = $ ignores ;
25
- $ this ->config = $ config ;
26
27
$ this ->outdatedVirtualFile = $ outdatedVirtualFile ;
28
+ $ this ->originalParallelCount = (int ) $ config ->parallel ; // PHPStan fix bad annotation hack
27
29
28
30
$ files = $ config ->files ;
29
31
$ files [] = $ outdatedVirtualFile ; // this must be last file to check - it's file that perform check what ignored files was not matched
30
32
$ config ->files = $ files ;
31
33
32
- if ($ config -> parallel === 1 ) {
34
+ if ($ this -> originalParallelCount === 1 ) {
33
35
$ this ->outdatedDataFile = NULL ;
34
36
} else {
35
37
$ outdatedDataFile = tempnam (sys_get_temp_dir (), 'phpcs-ignores-outdated ' );
36
38
if ($ outdatedDataFile === FALSE ) {
37
39
throw new \RuntimeException ('Can \'t create phpcs-ignores-outdated temp file. ' );
38
40
}
39
41
40
- file_put_contents ($ outdatedDataFile , json_encode (['processCount ' => $ config -> parallel , ' completedCount ' => 0 , 'remainingIgnoreErrors ' => []]));
42
+ file_put_contents ($ outdatedDataFile , json_encode (['completedCount ' => 0 , 'remainingIgnoreErrors ' => []]));
41
43
$ this ->outdatedDataFile = $ outdatedDataFile ;
42
44
}
43
45
}
@@ -65,9 +67,9 @@ public function __destruct()
65
67
$ json = $ this ->loadOutdatedDataFile ();
66
68
$ json ['completedCount ' ]++;
67
69
68
- // we know that one file is only in one process, so valid remaining ignore errors are these it's in all processes
70
+ // we know that one checked file is only in one process, so valid remaining ignore errors are these it's in all processes (array_intersect_key)
69
71
$ json ['remainingIgnoreErrors ' ] = $ json ['completedCount ' ] === 1
70
- ? $ this ->ignores ->getRemainingIgnoreErrors ()
72
+ ? $ this ->ignores ->getRemainingIgnoreErrors () // for first process we need to fill array, so in next proceses we can make intersect (array_intersect_key)
71
73
: array_intersect_key ($ json ['remainingIgnoreErrors ' ], $ this ->ignores ->getRemainingIgnoreErrors ());
72
74
73
75
file_put_contents ($ this ->outdatedDataFile , json_encode ($ json ));
@@ -92,13 +94,14 @@ public function checkOutdatedFiles(): array
92
94
$ outdatedFiles = [];
93
95
94
96
$ remainingOutdatedErrors = $ this ->ignores ->getRemainingIgnoreErrors ();
95
- if ($ this ->config ->parallel > 1 ) {
97
+
98
+ if ($ this ->outdatedDataFile !== NULL ) {
96
99
// wait to complete all processes
97
100
$ start = microtime (TRUE );
98
101
do {
99
102
try {
100
103
$ json = $ this ->loadOutdatedDataFile ();
101
- if ($ json [ ' processCount ' ] === ($ json ['completedCount ' ] + 1 )) {
104
+ if (( $ this -> realParallelCount ?? $ this -> originalParallelCount ) === ($ json ['completedCount ' ] + 1 )) {
102
105
$ remainingOutdatedErrors = array_intersect_key ($ json ['remainingIgnoreErrors ' ], $ remainingOutdatedErrors );
103
106
break ;
104
107
}
@@ -159,7 +162,7 @@ private function getOutdatedDataFileLock(): string
159
162
160
163
161
164
/**
162
- * @return array{processCount: int, completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>}
165
+ * @return array{completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>}
163
166
*/
164
167
private function loadOutdatedDataFile (): array
165
168
{
@@ -172,7 +175,7 @@ private function loadOutdatedDataFile(): array
172
175
throw new \RuntimeException ('Can \'t load outdated data file. ' );
173
176
}
174
177
175
- /** @phpstan-var array{processCount: int, completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>} */
178
+ /** @phpstan-var array{completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>} */
176
179
return json_decode ($ data , NULL , 512 , JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY );
177
180
}
178
181
@@ -202,6 +205,20 @@ public static function formatOutdatedMessage(
202
205
}
203
206
204
207
208
+ public function setRealParallelCount (int $ numFiles ): void
209
+ {
210
+ if (($ this ->realParallelCount === NULL ) && ($ this ->outdatedDataFile !== NULL )) {
211
+ $ batch = ceil ($ numFiles / $ this ->originalParallelCount );
212
+ for ($ i = 2 ; $ i <= $ this ->originalParallelCount ; $ i ++) {
213
+ if (($ batch * $ i ) >= $ numFiles ) {
214
+ $ this ->realParallelCount = $ i ;
215
+ break ;
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+
205
222
public function setInstance (): static
206
223
{
207
224
if (self ::$ instance !== NULL ) {
0 commit comments