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 ;
11+ use function is_dir ;
12+ use function is_file ;
813use function sha1_file ;
914
1015final class FileMonitor
@@ -14,39 +19,52 @@ final class FileMonitor
1419 private ?array $ fileHashes = null ;
1520
1621 /** @var array<string>|null */
17- private ?array $ paths = null ;
22+ private ?array $ filePaths = null ;
1823
19- public function __construct (private FileFinder $ fileFinder )
24+ /**
25+ * @param string[] $analysedPaths
26+ * @param string[] $analysedPathsFromConfig
27+ * @param string[] $scanFiles
28+ * @param string[] $scanDirectories
29+ */
30+ public function __construct (
31+ private FileFinder $ analyseFileFinder ,
32+ private FileFinder $ scanFileFinder ,
33+ private array $ analysedPaths ,
34+ private array $ analysedPathsFromConfig ,
35+ private array $ scanFiles ,
36+ private array $ scanDirectories ,
37+ )
2038 {
2139 }
2240
2341 /**
24- * @param array<string> $paths
42+ * @param array<string> $filePaths
2543 */
26- public function initialize (array $ paths ): void
44+ public function initialize (array $ filePaths ): void
2745 {
28- $ finderResult = $ this ->fileFinder ->findFiles ($ paths );
46+ $ finderResult = $ this ->analyseFileFinder ->findFiles ($ this -> analysedPaths );
2947 $ fileHashes = [];
30- foreach ($ finderResult ->getFiles () as $ filePath ) {
48+ foreach (array_merge ( $ finderResult ->getFiles (), $ filePaths , $ this -> getScannedFiles ( $ finderResult -> getFiles ()) ) as $ filePath ) {
3149 $ fileHashes [$ filePath ] = $ this ->getFileHash ($ filePath );
3250 }
3351
3452 $ this ->fileHashes = $ fileHashes ;
35- $ this ->paths = $ paths ;
53+ $ this ->filePaths = $ filePaths ;
3654 }
3755
3856 public function getChanges (): FileMonitorResult
3957 {
40- if ($ this ->fileHashes === null || $ this ->paths === null ) {
58+ if ($ this ->fileHashes === null || $ this ->filePaths === null ) {
4159 throw new ShouldNotHappenException ();
4260 }
43- $ finderResult = $ this ->fileFinder ->findFiles ($ this ->paths );
61+ $ finderResult = $ this ->analyseFileFinder ->findFiles ($ this ->analysedPaths );
4462 $ oldFileHashes = $ this ->fileHashes ;
4563 $ fileHashes = [];
4664 $ newFiles = [];
4765 $ changedFiles = [];
4866 $ deletedFiles = [];
49- foreach ($ finderResult ->getFiles () as $ filePath ) {
67+ foreach (array_merge ( $ finderResult ->getFiles (), $ this -> filePaths , $ this -> getScannedFiles ( $ finderResult -> getFiles ()) ) as $ filePath ) {
5068 if (!array_key_exists ($ filePath , $ oldFileHashes )) {
5169 $ newFiles [] = $ filePath ;
5270 $ fileHashes [$ filePath ] = $ this ->getFileHash ($ filePath );
@@ -88,4 +106,32 @@ private function getFileHash(string $filePath): string
88106 return $ hash ;
89107 }
90108
109+ /**
110+ * @param string[] $allAnalysedFiles
111+ * @return array<string>
112+ */
113+ private function getScannedFiles (array $ allAnalysedFiles ): array
114+ {
115+ $ scannedFiles = $ this ->scanFiles ;
116+ $ analysedDirectories = [];
117+ foreach (array_merge ($ this ->analysedPaths , $ this ->analysedPathsFromConfig ) as $ analysedPath ) {
118+ if (is_file ($ analysedPath )) {
119+ continue ;
120+ }
121+
122+ if (!is_dir ($ analysedPath )) {
123+ continue ;
124+ }
125+
126+ $ analysedDirectories [] = $ analysedPath ;
127+ }
128+
129+ $ directories = array_unique (array_merge ($ analysedDirectories , $ this ->scanDirectories ));
130+ foreach ($ this ->scanFileFinder ->findFiles ($ directories )->getFiles () as $ file ) {
131+ $ scannedFiles [] = $ file ;
132+ }
133+
134+ return array_diff ($ scannedFiles , $ allAnalysedFiles );
135+ }
136+
91137}
0 commit comments