3
3
namespace PHPStan \File ;
4
4
5
5
use PHPStan \ShouldNotHappenException ;
6
+ use function array_diff ;
6
7
use function array_key_exists ;
7
8
use function array_keys ;
9
+ use function array_merge ;
10
+ use function array_unique ;
8
11
use function count ;
12
+ use function is_dir ;
13
+ use function is_file ;
9
14
use function sha1_file ;
10
15
11
16
final class FileMonitor
@@ -15,39 +20,52 @@ final class FileMonitor
15
20
private ?array $ fileHashes = null ;
16
21
17
22
/** @var array<string>|null */
18
- private ?array $ paths = null ;
23
+ private ?array $ filePaths = null ;
19
24
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
+ )
21
39
{
22
40
}
23
41
24
42
/**
25
- * @param array<string> $paths
43
+ * @param array<string> $filePaths
26
44
*/
27
- public function initialize (array $ paths ): void
45
+ public function initialize (array $ filePaths ): void
28
46
{
29
- $ finderResult = $ this ->fileFinder ->findFiles ($ paths );
47
+ $ finderResult = $ this ->analyseFileFinder ->findFiles ($ this -> analysedPaths );
30
48
$ fileHashes = [];
31
- foreach ($ finderResult ->getFiles () as $ filePath ) {
49
+ foreach (array_merge ( $ finderResult ->getFiles (), $ filePaths , $ this -> getScannedFiles ( $ finderResult -> getFiles ()) ) as $ filePath ) {
32
50
$ fileHashes [$ filePath ] = $ this ->getFileHash ($ filePath );
33
51
}
34
52
35
53
$ this ->fileHashes = $ fileHashes ;
36
- $ this ->paths = $ paths ;
54
+ $ this ->filePaths = $ filePaths ;
37
55
}
38
56
39
57
public function getChanges (): FileMonitorResult
40
58
{
41
- if ($ this ->fileHashes === null || $ this ->paths === null ) {
59
+ if ($ this ->fileHashes === null || $ this ->filePaths === null ) {
42
60
throw new ShouldNotHappenException ();
43
61
}
44
- $ finderResult = $ this ->fileFinder ->findFiles ($ this ->paths );
62
+ $ finderResult = $ this ->analyseFileFinder ->findFiles ($ this ->analysedPaths );
45
63
$ oldFileHashes = $ this ->fileHashes ;
46
64
$ fileHashes = [];
47
65
$ newFiles = [];
48
66
$ changedFiles = [];
49
67
$ deletedFiles = [];
50
- foreach ($ finderResult ->getFiles () as $ filePath ) {
68
+ foreach (array_merge ( $ finderResult ->getFiles (), $ this -> filePaths , $ this -> getScannedFiles ( $ finderResult -> getFiles ()) ) as $ filePath ) {
51
69
if (!array_key_exists ($ filePath , $ oldFileHashes )) {
52
70
$ newFiles [] = $ filePath ;
53
71
$ fileHashes [$ filePath ] = $ this ->getFileHash ($ filePath );
@@ -90,4 +108,32 @@ private function getFileHash(string $filePath): string
90
108
return $ hash ;
91
109
}
92
110
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
+
93
139
}
0 commit comments