@@ -24,6 +24,12 @@ final class NonExistingBladeTemplateSniff implements Sniff
24
24
/** @var array<string, bool> */
25
25
private array $ checkedFiles = [];
26
26
27
+ /** @var array<string, bool> Cache for template existence checks */
28
+ private array $ templateExistsCache = [];
29
+
30
+ /** @var string|null Cached base directory */
31
+ private ?string $ resolvedBaseDir = null ;
32
+
27
33
private BladeTemplateExtractor $ bladeExtractor ;
28
34
private PhpViewExtractor $ phpExtractor ;
29
35
@@ -42,16 +48,20 @@ public function register(): array
42
48
/** @inheritDoc */
43
49
public function process (File $ phpcsFile , $ stackPtr ): int // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
44
50
{
45
- $ tokens = $ phpcsFile ->getTokens ();
46
-
47
51
$ filename = $ phpcsFile ->getFilename ();
48
52
49
53
$ hash = md5 ($ filename );
50
54
if (($ this ->checkedFiles [$ hash ] ?? false ) || $ filename === 'STDIN ' || \str_contains ($ filename , '.stub ' )) {
51
55
return 0 ;
52
56
}
53
57
58
+ // Early exit: Skip processing for non-Blade and non-PHP files
59
+ if (!str_ends_with ($ filename , '.php ' )) {
60
+ return 0 ;
61
+ }
62
+
54
63
$ this ->checkedFiles [$ hash ] = true ;
64
+ $ tokens = $ phpcsFile ->getTokens ();
55
65
56
66
foreach ($ tokens as $ position => $ token ) {
57
67
$ tokenContent = $ token ['content ' ];
@@ -81,18 +91,28 @@ public function process(File $phpcsFile, $stackPtr): int // phpcs:ignore Generic
81
91
*/
82
92
private function templateIsMissing (string $ templateName ): bool
83
93
{
94
+ if (isset ($ this ->templateExistsCache [$ templateName ])) {
95
+ return !$ this ->templateExistsCache [$ templateName ];
96
+ }
97
+
84
98
foreach ($ this ->getTemplatePathCandidates ($ templateName ) as $ candidateFilePath ) {
85
99
if (\file_exists ($ candidateFilePath )) {
100
+ $ this ->templateExistsCache [$ templateName ] = true ;
86
101
return false ;
87
102
}
88
103
}
89
104
105
+ $ this ->templateExistsCache [$ templateName ] = false ;
90
106
return true ;
91
107
}
92
108
93
109
private function resolveLaravelBaseDir (): string
94
110
{
95
- return $ this ->baseDir ?? dirname (__DIR__ , 6 ); // assume this file in the classic vendor dir
111
+ if ($ this ->resolvedBaseDir === null ) {
112
+ $ this ->resolvedBaseDir = $ this ->baseDir ?? dirname (__DIR__ , 6 ); // assume this file in the classic vendor dir for phpcs installed as a dependency
113
+ }
114
+
115
+ return $ this ->resolvedBaseDir ;
96
116
}
97
117
98
118
/**
0 commit comments