Skip to content

Commit 8c5f3a4

Browse files
committed
Outdated: fix detecting outdated with parallel > 1
1 parent 7f8cc1b commit 8c5f3a4

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

phpstan.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ parameters:
2222
count: 1
2323
path: src/File.php
2424

25-
-
26-
message: "#^Strict comparison using \\=\\=\\= between bool and 1 will always evaluate to false\\.$#"
27-
count: 1
28-
path: src/OutdatedFiles.php
29-
3025
-
3126
message: '#^Parameter \$phpcsFile of method Forrest79\\PhpCsIgnores\\BaselineReport\:\:generateFileReport\(\) has invalid type PHP_CodeSniffer\\File\.$#'
3227
count: 1

src/File.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ final class File extends PHP_CodeSniffer\Files\LocalFile
2222
private int $ignoredFixableCount = 0;
2323

2424

25-
/**
26-
* @param string $path
27-
*/
28-
public function __construct($path, PHP_CodeSniffer\Ruleset $ruleset, PHP_CodeSniffer\Config $config)
25+
public function __construct(
26+
int $numFiles,
27+
string $path,
28+
PHP_CodeSniffer\Ruleset $ruleset,
29+
PHP_CodeSniffer\Config $config,
30+
)
2931
{
3032
parent::__construct($path, $ruleset, $config);
3133

3234
$this->originalIgnoreErrors = Ignores::getInstance()->getRemainingIgnoreErrorsForFileAndClean($path);
3335
$this->ignoreErrors = $this->originalIgnoreErrors;
36+
37+
OutdatedFiles::getInstance()?->setRealParallelCount($numFiles);
3438
}
3539

3640

src/OutdatedFiles.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@ final class OutdatedFiles
1212

1313
private Ignores $ignores;
1414

15-
private PHP_CodeSniffer\Config $config;
16-
1715
private string $outdatedVirtualFile;
1816

17+
private int $originalParallelCount;
18+
19+
private int|NULL $realParallelCount = NULL;
20+
1921
private string|NULL $outdatedDataFile = NULL;
2022

2123

2224
public function __construct(Ignores $ignores, PHP_CodeSniffer\Config $config, string $outdatedVirtualFile)
2325
{
2426
$this->ignores = $ignores;
25-
$this->config = $config;
2627
$this->outdatedVirtualFile = $outdatedVirtualFile;
28+
$this->originalParallelCount = (int) $config->parallel; // PHPStan fix bad annotation hack
2729

2830
$files = $config->files;
2931
$files[] = $outdatedVirtualFile; // this must be last file to check - it's file that perform check what ignored files was not matched
3032
$config->files = $files;
3133

32-
if ($config->parallel === 1) {
34+
if ($this->originalParallelCount === 1) {
3335
$this->outdatedDataFile = NULL;
3436
} else {
3537
$outdatedDataFile = tempnam(sys_get_temp_dir(), 'phpcs-ignores-outdated');
3638
if ($outdatedDataFile === FALSE) {
3739
throw new \RuntimeException('Can\'t create phpcs-ignores-outdated temp file.');
3840
}
3941

40-
file_put_contents($outdatedDataFile, json_encode(['processCount' => $config->parallel, 'completedCount' => 0, 'remainingIgnoreErrors' => []]));
42+
file_put_contents($outdatedDataFile, json_encode(['completedCount' => 0, 'remainingIgnoreErrors' => []]));
4143
$this->outdatedDataFile = $outdatedDataFile;
4244
}
4345
}
@@ -65,9 +67,9 @@ public function __destruct()
6567
$json = $this->loadOutdatedDataFile();
6668
$json['completedCount']++;
6769

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)
6971
$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)
7173
: array_intersect_key($json['remainingIgnoreErrors'], $this->ignores->getRemainingIgnoreErrors());
7274

7375
file_put_contents($this->outdatedDataFile, json_encode($json));
@@ -92,13 +94,14 @@ public function checkOutdatedFiles(): array
9294
$outdatedFiles = [];
9395

9496
$remainingOutdatedErrors = $this->ignores->getRemainingIgnoreErrors();
95-
if ($this->config->parallel > 1) {
97+
98+
if ($this->outdatedDataFile !== NULL) {
9699
// wait to complete all processes
97100
$start = microtime(TRUE);
98101
do {
99102
try {
100103
$json = $this->loadOutdatedDataFile();
101-
if ($json['processCount'] === ($json['completedCount'] + 1)) {
104+
if (($this->realParallelCount ?? $this->originalParallelCount) === ($json['completedCount'] + 1)) {
102105
$remainingOutdatedErrors = array_intersect_key($json['remainingIgnoreErrors'], $remainingOutdatedErrors);
103106
break;
104107
}
@@ -159,7 +162,7 @@ private function getOutdatedDataFileLock(): string
159162

160163

161164
/**
162-
* @return array{processCount: int, completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>}
165+
* @return array{completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>}
163166
*/
164167
private function loadOutdatedDataFile(): array
165168
{
@@ -172,7 +175,7 @@ private function loadOutdatedDataFile(): array
172175
throw new \RuntimeException('Can\'t load outdated data file.');
173176
}
174177

175-
/** @phpstan-var array{processCount: int, completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>} */
178+
/** @phpstan-var array{completedCount: int, remainingIgnoreErrors: array<array<string, mixed>>} */
176179
return json_decode($data, NULL, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
177180
}
178181

@@ -202,6 +205,20 @@ public static function formatOutdatedMessage(
202205
}
203206

204207

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+
205222
public function setInstance(): static
206223
{
207224
if (self::$instance !== NULL) {

src/PhpCsInjections.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function register(): void
3737
return $code;
3838
}
3939

40-
$injectCode = 'new \\' . File::class . '(';
40+
$injectCode = 'new \\' . File::class . '($this->numFiles, ';
4141

4242
return str_replace($search, $injectCode, $code);
4343
}

0 commit comments

Comments
 (0)