33namespace PHPStan \File ;
44
55use PHPStan \ShouldNotHappenException ;
6+ use function array_diff ;
67use function array_key_exists ;
78use function array_keys ;
9+ use function array_merge ;
10+ use function array_unique ;
811use function count ;
12+ use function is_dir ;
13+ use function is_file ;
914use function sha1_file ;
1015
1116final 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