8
8
use PHPStan \Analyser \NameScope ;
9
9
use PHPStan \BetterReflection \Util \GetLastDocComment ;
10
10
use PHPStan \Broker \AnonymousClassNameHelper ;
11
- use PHPStan \Cache \Cache ;
12
11
use PHPStan \File \FileHelper ;
13
12
use PHPStan \Parser \Parser ;
14
- use PHPStan \Php \PhpVersion ;
15
13
use PHPStan \PhpDoc \PhpDocNodeResolver ;
16
14
use PHPStan \PhpDoc \PhpDocStringResolver ;
17
15
use PHPStan \PhpDoc \ResolvedPhpDocBlock ;
23
21
use PHPStan \Type \Generic \TemplateTypeFactory ;
24
22
use PHPStan \Type \Generic \TemplateTypeHelper ;
25
23
use PHPStan \Type \Generic \TemplateTypeMap ;
26
- use ReflectionClass ;
27
24
use function array_key_exists ;
28
25
use function array_keys ;
29
26
use function array_map ;
30
27
use function array_merge ;
31
28
use function array_pop ;
32
29
use function array_slice ;
33
30
use function count ;
34
- use function filemtime ;
35
- use function implode ;
36
31
use function is_array ;
37
32
use function is_callable ;
38
33
use function is_file ;
41
36
use function sprintf ;
42
37
use function strpos ;
43
38
use function strtolower ;
44
- use function time ;
45
- use function trait_exists ;
46
39
47
40
class FileTypeMapper
48
41
{
@@ -63,17 +56,12 @@ class FileTypeMapper
63
56
64
57
private int $ resolvedPhpDocBlockCacheCount = 0 ;
65
58
66
- /** @var array<string, bool> */
67
- private array $ alreadyProcessedDependentFiles = [];
68
-
69
59
public function __construct (
70
60
private ReflectionProviderProvider $ reflectionProviderProvider ,
71
61
private Parser $ phpParser ,
72
62
private PhpDocStringResolver $ phpDocStringResolver ,
73
63
private PhpDocNodeResolver $ phpDocNodeResolver ,
74
- private Cache $ cache ,
75
64
private AnonymousClassNameHelper $ anonymousClassNameHelper ,
76
- private PhpVersion $ phpVersion ,
77
65
private FileHelper $ fileHelper ,
78
66
)
79
67
{
@@ -180,15 +168,7 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode
180
168
private function getNameScopeMap (string $ fileName ): array
181
169
{
182
170
if (!isset ($ this ->memoryCache [$ fileName ])) {
183
- $ cacheKey = sprintf ('%s-phpdocstring-v22-trait-bug ' , $ fileName );
184
- $ variableCacheKey = sprintf ('%s-%s ' , implode (', ' , array_map (static fn (array $ file ): string => sprintf ('%s-%d ' , $ file ['filename ' ], $ file ['modifiedTime ' ]), $ this ->getCachedDependentFilesWithTimestamps ($ fileName ))), $ this ->phpVersion ->getVersionString ());
185
- $ map = $ this ->cache ->load ($ cacheKey , $ variableCacheKey );
186
-
187
- if ($ map === null ) {
188
- $ map = $ this ->createResolvedPhpDocMap ($ fileName );
189
- $ this ->cache ->save ($ cacheKey , $ variableCacheKey , $ map );
190
- }
191
-
171
+ $ map = $ this ->createResolvedPhpDocMap ($ fileName );
192
172
if ($ this ->memoryCacheCount >= 512 ) {
193
173
$ this ->memoryCache = array_slice (
194
174
$ this ->memoryCache ,
@@ -607,126 +587,4 @@ private function getNameScopeKey(
607
587
return md5 (sprintf ('%s-%s-%s-%s ' , $ file , $ class , $ trait , $ function ));
608
588
}
609
589
610
- /**
611
- * @return array<array{filename: string, modifiedTime: int}>
612
- */
613
- private function getCachedDependentFilesWithTimestamps (string $ fileName ): array
614
- {
615
- $ cacheKey = sprintf ('dependentFilesTimestamps-%s-v3-filter-ast ' , $ fileName );
616
- $ fileModifiedTime = filemtime ($ fileName );
617
- if ($ fileModifiedTime === false ) {
618
- $ fileModifiedTime = time ();
619
- }
620
- $ variableCacheKey = sprintf ('%d-%s ' , $ fileModifiedTime , $ this ->phpVersion ->getVersionString ());
621
- /** @var array<array{filename: string, modifiedTime: int}>|null $cachedFilesTimestamps */
622
- $ cachedFilesTimestamps = $ this ->cache ->load ($ cacheKey , $ variableCacheKey );
623
- if ($ cachedFilesTimestamps !== null ) {
624
- $ useCached = true ;
625
- foreach ($ cachedFilesTimestamps as $ cachedFile ) {
626
- $ cachedFilename = $ cachedFile ['filename ' ];
627
- $ cachedTimestamp = $ cachedFile ['modifiedTime ' ];
628
-
629
- if (!is_file ($ cachedFilename )) {
630
- $ useCached = false ;
631
- break ;
632
- }
633
-
634
- $ currentTimestamp = filemtime ($ cachedFilename );
635
- if ($ currentTimestamp === false ) {
636
- $ useCached = false ;
637
- break ;
638
- }
639
-
640
- if ($ currentTimestamp !== $ cachedTimestamp ) {
641
- $ useCached = false ;
642
- break ;
643
- }
644
- }
645
-
646
- if ($ useCached ) {
647
- return $ cachedFilesTimestamps ;
648
- }
649
- }
650
-
651
- $ filesTimestamps = [];
652
- foreach ($ this ->getDependentFiles ($ fileName ) as $ dependentFile ) {
653
- $ dependentFileModifiedTime = filemtime ($ dependentFile );
654
- if ($ dependentFileModifiedTime === false ) {
655
- $ dependentFileModifiedTime = time ();
656
- }
657
-
658
- $ filesTimestamps [] = [
659
- 'filename ' => $ dependentFile ,
660
- 'modifiedTime ' => $ dependentFileModifiedTime ,
661
- ];
662
- }
663
-
664
- $ this ->cache ->save ($ cacheKey , $ variableCacheKey , $ filesTimestamps );
665
-
666
- return $ filesTimestamps ;
667
- }
668
-
669
- /**
670
- * @return string[]
671
- */
672
- private function getDependentFiles (string $ fileName ): array
673
- {
674
- $ dependentFiles = [$ fileName ];
675
-
676
- if (isset ($ this ->alreadyProcessedDependentFiles [$ fileName ])) {
677
- return $ dependentFiles ;
678
- }
679
-
680
- $ this ->alreadyProcessedDependentFiles [$ fileName ] = true ;
681
-
682
- $ this ->processNodes (
683
- $ this ->phpParser ->parseFile ($ fileName ),
684
- function (Node $ node ) use (&$ dependentFiles ) {
685
- if ($ node instanceof Node \Stmt \Declare_) {
686
- return null ;
687
- }
688
- if ($ node instanceof Node \Stmt \Namespace_) {
689
- return null ;
690
- }
691
-
692
- if (!$ node instanceof Node \Stmt \Class_ && !$ node instanceof Node \Stmt \Trait_ && !$ node instanceof Node \Stmt \Enum_) {
693
- return null ;
694
- }
695
-
696
- foreach ($ node ->stmts as $ stmt ) {
697
- if (!$ stmt instanceof Node \Stmt \TraitUse) {
698
- continue ;
699
- }
700
-
701
- foreach ($ stmt ->traits as $ traitName ) {
702
- $ traitName = (string ) $ traitName ;
703
- if (!trait_exists ($ traitName )) {
704
- continue ;
705
- }
706
-
707
- $ traitReflection = new ReflectionClass ($ traitName );
708
- if ($ traitReflection ->getFileName () === false ) {
709
- continue ;
710
- }
711
- if (!is_file ($ traitReflection ->getFileName ())) {
712
- continue ;
713
- }
714
-
715
- foreach ($ this ->getDependentFiles ($ traitReflection ->getFileName ()) as $ traitFileName ) {
716
- $ dependentFiles [] = $ traitFileName ;
717
- }
718
- }
719
- }
720
-
721
- return null ;
722
- },
723
- static function (): void {
724
- },
725
- );
726
-
727
- unset($ this ->alreadyProcessedDependentFiles [$ fileName ]);
728
-
729
- return $ dependentFiles ;
730
- }
731
-
732
590
}
0 commit comments