@@ -24,6 +24,12 @@ final class NonExistingBladeTemplateSniff implements Sniff
2424 /** @var array<string, bool> */
2525 private array $ checkedFiles = [];
2626
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+
2733 private BladeTemplateExtractor $ bladeExtractor ;
2834 private PhpViewExtractor $ phpExtractor ;
2935
@@ -42,16 +48,20 @@ public function register(): array
4248 /** @inheritDoc */
4349 public function process (File $ phpcsFile , $ stackPtr ): int // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
4450 {
45- $ tokens = $ phpcsFile ->getTokens ();
46-
4751 $ filename = $ phpcsFile ->getFilename ();
4852
4953 $ hash = md5 ($ filename );
5054 if (($ this ->checkedFiles [$ hash ] ?? false ) || $ filename === 'STDIN ' || \str_contains ($ filename , '.stub ' )) {
5155 return 0 ;
5256 }
5357
58+ // Early exit: Skip processing for non-Blade and non-PHP files
59+ if (!str_ends_with ($ filename , '.php ' )) {
60+ return 0 ;
61+ }
62+
5463 $ this ->checkedFiles [$ hash ] = true ;
64+ $ tokens = $ phpcsFile ->getTokens ();
5565
5666 foreach ($ tokens as $ position => $ token ) {
5767 $ tokenContent = $ token ['content ' ];
@@ -81,18 +91,28 @@ public function process(File $phpcsFile, $stackPtr): int // phpcs:ignore Generic
8191 */
8292 private function templateIsMissing (string $ templateName ): bool
8393 {
94+ if (isset ($ this ->templateExistsCache [$ templateName ])) {
95+ return !$ this ->templateExistsCache [$ templateName ];
96+ }
97+
8498 foreach ($ this ->getTemplatePathCandidates ($ templateName ) as $ candidateFilePath ) {
8599 if (\file_exists ($ candidateFilePath )) {
100+ $ this ->templateExistsCache [$ templateName ] = true ;
86101 return false ;
87102 }
88103 }
89104
105+ $ this ->templateExistsCache [$ templateName ] = false ;
90106 return true ;
91107 }
92108
93109 private function resolveLaravelBaseDir (): string
94110 {
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 ;
96116 }
97117
98118 /**
0 commit comments