Skip to content

Commit 7e3639b

Browse files
committed
PHPStan Pro - refresh errors when scanned file is changed
1 parent e8b46c6 commit 7e3639b

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

conf/config.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,12 @@ services:
732732
-
733733
class: PHPStan\File\FileMonitor
734734
arguments:
735-
fileFinder: @fileFinderAnalyse
735+
analyseFileFinder: @fileFinderAnalyse
736+
scanFileFinder: @fileFinderScan
737+
analysedPaths: %analysedPaths%
738+
analysedPathsFromConfig: %analysedPathsFromConfig%
739+
scanFiles: %scanFiles%
740+
scanDirectories: %scanDirectories%
736741

737742
-
738743
class: PHPStan\Parser\DeclarePositionVisitor

src/Command/FixerApplication.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public function run(
147147
});
148148

149149
$this->fileMonitor->initialize(array_merge(
150-
$this->analysedPaths,
151150
$this->getComposerLocks(),
152151
$this->getComposerInstalled(),
153152
$this->getExecutedFiles(),

src/File/FileMonitor.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace PHPStan\File;
44

55
use PHPStan\ShouldNotHappenException;
6+
use function array_diff;
67
use function array_key_exists;
78
use function array_keys;
9+
use function array_merge;
10+
use function array_unique;
811
use function count;
12+
use function is_dir;
13+
use function is_file;
914
use function sha1_file;
1015

1116
final class FileMonitor
@@ -15,39 +20,52 @@ final class FileMonitor
1520
private ?array $fileHashes = null;
1621

1722
/** @var array<string>|null */
18-
private ?array $paths = null;
23+
private ?array $filePaths = null;
1924

20-
public function __construct(private FileFinder $fileFinder)
25+
/**
26+
* @param string[] $analysedPaths
27+
* @param string[] $analysedPathsFromConfig
28+
* @param string[] $scanFiles
29+
* @param string[] $scanDirectories
30+
*/
31+
public function __construct(
32+
private FileFinder $analyseFileFinder,
33+
private FileFinder $scanFileFinder,
34+
private array $analysedPaths,
35+
private array $analysedPathsFromConfig,
36+
private array $scanFiles,
37+
private array $scanDirectories,
38+
)
2139
{
2240
}
2341

2442
/**
25-
* @param array<string> $paths
43+
* @param array<string> $filePaths
2644
*/
27-
public function initialize(array $paths): void
45+
public function initialize(array $filePaths): void
2846
{
29-
$finderResult = $this->fileFinder->findFiles($paths);
47+
$finderResult = $this->analyseFileFinder->findFiles($this->analysedPaths);
3048
$fileHashes = [];
31-
foreach ($finderResult->getFiles() as $filePath) {
49+
foreach (array_merge($finderResult->getFiles(), $filePaths, $this->getScannedFiles($finderResult->getFiles())) as $filePath) {
3250
$fileHashes[$filePath] = $this->getFileHash($filePath);
3351
}
3452

3553
$this->fileHashes = $fileHashes;
36-
$this->paths = $paths;
54+
$this->filePaths = $filePaths;
3755
}
3856

3957
public function getChanges(): FileMonitorResult
4058
{
41-
if ($this->fileHashes === null || $this->paths === null) {
59+
if ($this->fileHashes === null || $this->filePaths === null) {
4260
throw new ShouldNotHappenException();
4361
}
44-
$finderResult = $this->fileFinder->findFiles($this->paths);
62+
$finderResult = $this->analyseFileFinder->findFiles($this->analysedPaths);
4563
$oldFileHashes = $this->fileHashes;
4664
$fileHashes = [];
4765
$newFiles = [];
4866
$changedFiles = [];
4967
$deletedFiles = [];
50-
foreach ($finderResult->getFiles() as $filePath) {
68+
foreach (array_merge($finderResult->getFiles(), $this->filePaths, $this->getScannedFiles($finderResult->getFiles())) as $filePath) {
5169
if (!array_key_exists($filePath, $oldFileHashes)) {
5270
$newFiles[] = $filePath;
5371
$fileHashes[$filePath] = $this->getFileHash($filePath);
@@ -90,4 +108,32 @@ private function getFileHash(string $filePath): string
90108
return $hash;
91109
}
92110

111+
/**
112+
* @param string[] $allAnalysedFiles
113+
* @return array<string>
114+
*/
115+
private function getScannedFiles(array $allAnalysedFiles): array
116+
{
117+
$scannedFiles = $this->scanFiles;
118+
$analysedDirectories = [];
119+
foreach (array_merge($this->analysedPaths, $this->analysedPathsFromConfig) as $analysedPath) {
120+
if (is_file($analysedPath)) {
121+
continue;
122+
}
123+
124+
if (!is_dir($analysedPath)) {
125+
continue;
126+
}
127+
128+
$analysedDirectories[] = $analysedPath;
129+
}
130+
131+
$directories = array_unique(array_merge($analysedDirectories, $this->scanDirectories));
132+
foreach ($this->scanFileFinder->findFiles($directories)->getFiles() as $file) {
133+
$scannedFiles[] = $file;
134+
}
135+
136+
return array_diff($scannedFiles, $allAnalysedFiles);
137+
}
138+
93139
}

0 commit comments

Comments
 (0)