From cf616e932b1832a0d72c89080e63b727a36031b0 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 13:01:03 -0800 Subject: [PATCH 1/7] Allow glob patterns in includes An entry in `includes` can now be a glob pattern, like `baselines/*.neon`, which includes every matching file in alphabetical order, as if each were listed in its place. This makes a baseline split into one file per error identifier (or per directory) a single line in the config, so that files can be added or deleted as errors are fixed without also editing the list of includes. A pattern that matches nothing includes nothing, so the last baseline file can be deleted once its errors are fixed. A path that names an existing file is never treated as a pattern, so a file whose name contains a glob metacharacter can still be included. Both places that follow includes expand the pattern: `NeonLoader`, which builds the config, and `ContainerFactory::getConfigFiles()`, which collects the config files. The latter feeds the container cache key, so adding or removing a matched file recompiles the container. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- phpstan-baseline.neon | 6 +++ src/DependencyInjection/ContainerFactory.php | 10 ++-- .../IncludedFilePattern.php | 45 ++++++++++++++++ src/DependencyInjection/NeonLoader.php | 18 +++++++ .../DependencyInjection/GlobIncludesTest.php | 51 +++++++++++++++++++ .../IncludedFilePatternTest.php | 33 ++++++++++++ .../globIncludes/baselines/a.neon | 6 +++ .../globIncludes/baselines/b.neon | 6 +++ .../globIncludes/baselines/c.txt | 1 + .../baselines/subdir.neon/.gitkeep | 0 .../globIncludes/empty.neon | 2 + .../globIncludes/literal/[literal].neon | 5 ++ .../globIncludes/main.neon | 6 +++ 13 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/DependencyInjection/IncludedFilePattern.php create mode 100644 tests/PHPStan/DependencyInjection/GlobIncludesTest.php create mode 100644 tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/baselines/a.neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/baselines/b.neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/baselines/c.txt create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/baselines/subdir.neon/.gitkeep create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/empty.neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/literal/[literal].neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/main.neon diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 898b4143837..dab997f496c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -313,6 +313,12 @@ parameters: count: 1 path: src/DependencyInjection/NeonAdapter.php + - + rawMessage: 'Call to static method merge() of internal class Nette\Schema\Helpers from outside its root namespace Nette.' + identifier: staticMethod.internalClass + count: 1 + path: src/DependencyInjection/NeonLoader.php + - rawMessage: 'Call to method getTokenCode() of internal class PhpParser\Internal\TokenStream from outside its root namespace PhpParser.' identifier: method.internalClass diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index d329282476b..a45ac0b62ac 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -320,11 +320,13 @@ private static function getConfigFiles( $includes = Helpers::expand($data['includes'], $loaderParameters); foreach ($includes as $include) { $include = self::expandIncludedFile($include, $configFile); - [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile); - $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles); + foreach (IncludedFilePattern::expand($include) ?? [$include] as $includedFile) { + [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $includedFile, $loaderParameters, $generateBaselineFile); + $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles); - /** @var array $data */ - $data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data); + /** @var array $data */ + $data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data); + } } } diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php new file mode 100644 index 00000000000..3448ec5ccbd --- /dev/null +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -0,0 +1,45 @@ +|null the matched files, or null when $includedFile is not a pattern + */ + public static function expand(string $includedFile): ?array + { + if (is_file($includedFile) || !FileExcluder::isFnmatchPattern($includedFile)) { + return null; + } + + $matches = glob($includedFile); + if ($matches === false) { + return []; + } + + // glob() sorts with the locale's collation, while the order of includes decides how values merge + $files = array_values(array_filter($matches, static fn (string $match): bool => is_file($match))); + sort($files, SORT_STRING); + + return $files; + } + +} diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index 6664157fcfd..19df32f39c8 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -3,8 +3,11 @@ namespace PHPStan\DependencyInjection; use Nette\DI\Config\Loader; +use Nette\Schema\Helpers; use Override; use PHPStan\File\FileHelper; +use PHPStan\ShouldNotHappenException; +use function is_array; final class NeonLoader extends Loader { @@ -22,6 +25,21 @@ public function __construct( #[Override] public function load(string $file, ?bool $merge = true): array { + // Loader::load() passes each entry in includes back here, after expanding it to an absolute path + $matchedFiles = IncludedFilePattern::expand($file); + if ($matchedFiles !== null) { + $res = []; + foreach ($matchedFiles as $matchedFile) { + // same merge as for consecutive entries in includes, so later files override earlier ones + $res = Helpers::merge($this->load($matchedFile, $merge), $res); + if (!is_array($res)) { + throw new ShouldNotHappenException(); + } + } + + return $res; + } + if ($this->generateBaselineFile === null) { return parent::load($file, $merge); } diff --git a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php new file mode 100644 index 00000000000..4d0e73b4e7c --- /dev/null +++ b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php @@ -0,0 +1,51 @@ + $ignoreErrors */ + $ignoreErrors = self::getContainer()->getParameter('ignoreErrors'); + $identifiers = array_column($ignoreErrors, 'identifier'); + $this->assertContains('a.identifier', $identifiers); + $this->assertContains('b.identifier', $identifiers); + $this->assertContains('literal.identifier', $identifiers); + } + + public function testMatchedFilesAreIncludedInAlphabeticalOrder(): void + { + // a later include overrides an earlier one, so b.neon wins over a.neon + $this->assertSame('b', self::getContainer()->getParameter('globIncludesOrder')); + } + + public function testMatchedFilesAreListedAsConfigFiles(): void + { + /** @var list $allConfigFiles */ + $allConfigFiles = self::getContainer()->getParameter('allConfigFiles'); + $allConfigFiles = array_map(static fn (string $file): string|false => realpath($file), $allConfigFiles); + + $this->assertContains(realpath(__DIR__ . '/globIncludes/baselines/a.neon'), $allConfigFiles); + $this->assertContains(realpath(__DIR__ . '/globIncludes/baselines/b.neon'), $allConfigFiles); + $this->assertContains(realpath(__DIR__ . '/globIncludes/literal/[literal].neon'), $allConfigFiles); + } + + /** + * @return string[] + */ + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/globIncludes/main.neon', + __DIR__ . '/globIncludes/empty.neon', + ]; + } + +} diff --git a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php new file mode 100644 index 00000000000..337653d5f1d --- /dev/null +++ b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php @@ -0,0 +1,33 @@ +assertSame([$dir . '/a.neon', $dir . '/b.neon'], IncludedFilePattern::expand($dir . '/*.neon')); + } + + public function testPatternMatchingNothingIsEmpty(): void + { + $this->assertSame([], IncludedFilePattern::expand(__DIR__ . '/globIncludes/nonexistent/*.neon')); + } + + public function testPlainPathIsNotAPattern(): void + { + $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/main.neon')); + $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/missing.neon')); + } + + public function testExistingFileWithMetacharactersIsNotAPattern(): void + { + $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/literal/[literal].neon')); + } + +} diff --git a/tests/PHPStan/DependencyInjection/globIncludes/baselines/a.neon b/tests/PHPStan/DependencyInjection/globIncludes/baselines/a.neon new file mode 100644 index 00000000000..ab22eeb4024 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/baselines/a.neon @@ -0,0 +1,6 @@ +parameters: + globIncludesOrder: a + ignoreErrors: + - + identifier: a.identifier + path: '*/a.php' diff --git a/tests/PHPStan/DependencyInjection/globIncludes/baselines/b.neon b/tests/PHPStan/DependencyInjection/globIncludes/baselines/b.neon new file mode 100644 index 00000000000..dd3d2c0c179 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/baselines/b.neon @@ -0,0 +1,6 @@ +parameters: + globIncludesOrder: b + ignoreErrors: + - + identifier: b.identifier + path: '*/b.php' diff --git a/tests/PHPStan/DependencyInjection/globIncludes/baselines/c.txt b/tests/PHPStan/DependencyInjection/globIncludes/baselines/c.txt new file mode 100644 index 00000000000..4536626c02e --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/baselines/c.txt @@ -0,0 +1 @@ +Not matched by the pattern. diff --git a/tests/PHPStan/DependencyInjection/globIncludes/baselines/subdir.neon/.gitkeep b/tests/PHPStan/DependencyInjection/globIncludes/baselines/subdir.neon/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/PHPStan/DependencyInjection/globIncludes/empty.neon b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon new file mode 100644 index 00000000000..8b5983d9234 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon @@ -0,0 +1,2 @@ +includes: + - nonexistent/*.neon diff --git a/tests/PHPStan/DependencyInjection/globIncludes/literal/[literal].neon b/tests/PHPStan/DependencyInjection/globIncludes/literal/[literal].neon new file mode 100644 index 00000000000..5b1854074ed --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/literal/[literal].neon @@ -0,0 +1,5 @@ +parameters: + ignoreErrors: + - + identifier: literal.identifier + path: '*/literal.php' diff --git a/tests/PHPStan/DependencyInjection/globIncludes/main.neon b/tests/PHPStan/DependencyInjection/globIncludes/main.neon new file mode 100644 index 00000000000..e7b961f3527 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/main.neon @@ -0,0 +1,6 @@ +includes: + - baselines/*.neon + - 'literal/[literal].neon' + +parametersSchema: + globIncludesOrder: string() From f6df7fd637655c9a836fc5567d8cba13c04f3b31 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 15:30:06 -0800 Subject: [PATCH 2/7] Leave the files being loaded out of an include pattern A loader that sits among the files it includes, like `baselines/_loader.neon` with `includes: [*.neon]`, matched itself. `ContainerFactory::getConfigFiles()` has no guard against recursive includes, so it recursed until memory ran out, and Nette's loader would have thrown "Recursive included file" instead. The pattern now leaves out the file with the includes and every file that includes it in turn, so such a loader includes only its siblings. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- src/DependencyInjection/ContainerFactory.php | 6 ++++-- .../IncludedFilePattern.php | 16 +++++++++++++-- src/DependencyInjection/NeonLoader.php | 20 +++++++++++-------- .../DependencyInjection/GlobIncludesTest.php | 1 + .../IncludedFilePatternTest.php | 6 ++++++ .../globIncludes/main.neon | 1 + .../globIncludes/selfIncluding/_loader.neon | 2 ++ .../globIncludes/selfIncluding/sibling.neon | 5 +++++ 8 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/_loader.neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/sibling.neon diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index a45ac0b62ac..c56416ebbde 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -291,6 +291,7 @@ private function detectDuplicateIncludedFiles( /** * @param array $loaderParameters + * @param list $loadingFiles the files that include $configFile, in turn * @return array{list, array} */ private static function getConfigFiles( @@ -300,6 +301,7 @@ private static function getConfigFiles( string $configFile, array $loaderParameters, ?string $generateBaselineFile, + array $loadingFiles = [], ): array { if ($generateBaselineFile === $fileHelper->normalizePath($configFile)) { @@ -320,8 +322,8 @@ private static function getConfigFiles( $includes = Helpers::expand($data['includes'], $loaderParameters); foreach ($includes as $include) { $include = self::expandIncludedFile($include, $configFile); - foreach (IncludedFilePattern::expand($include) ?? [$include] as $includedFile) { - [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $includedFile, $loaderParameters, $generateBaselineFile); + foreach (IncludedFilePattern::expand($include, [...$loadingFiles, $configFile]) ?? [$include] as $includedFile) { + [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $includedFile, $loaderParameters, $generateBaselineFile, [...$loadingFiles, $configFile]); $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles); /** @var array $data */ diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index 3448ec5ccbd..b530a2fcf76 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -4,9 +4,12 @@ use PHPStan\File\FileExcluder; use function array_filter; +use function array_map; use function array_values; use function glob; +use function in_array; use function is_file; +use function realpath; use function sort; use const SORT_STRING; @@ -16,15 +19,20 @@ * * A path that names an existing file is never treated as a pattern, * so a file whose name contains a glob metacharacter can still be included. + * + * The files that are being loaded at the time are left out of the matches, + * so that a loader like `baselines/_loader.neon` can include `*.neon` + * without including itself. */ final class IncludedFilePattern { /** * @param string $includedFile already expanded to an absolute path + * @param list $loadingFiles the file with the includes, and the files that include it in turn * @return list|null the matched files, or null when $includedFile is not a pattern */ - public static function expand(string $includedFile): ?array + public static function expand(string $includedFile, array $loadingFiles = []): ?array { if (is_file($includedFile) || !FileExcluder::isFnmatchPattern($includedFile)) { return null; @@ -36,7 +44,11 @@ public static function expand(string $includedFile): ?array } // glob() sorts with the locale's collation, while the order of includes decides how values merge - $files = array_values(array_filter($matches, static fn (string $match): bool => is_file($match))); + $loadingPaths = array_map(static fn (string $file): string|false => realpath($file), $loadingFiles); + $files = array_values(array_filter( + $matches, + static fn (string $match): bool => is_file($match) && !in_array(realpath($match), $loadingPaths, true), + )); sort($files, SORT_STRING); return $files; diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index 19df32f39c8..55bd0ba9de2 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -7,11 +7,15 @@ use Override; use PHPStan\File\FileHelper; use PHPStan\ShouldNotHappenException; +use function array_pop; use function is_array; final class NeonLoader extends Loader { + /** @var list */ + private array $loadingFiles = []; + public function __construct( private FileHelper $fileHelper, private ?string $generateBaselineFile, @@ -26,7 +30,7 @@ public function __construct( public function load(string $file, ?bool $merge = true): array { // Loader::load() passes each entry in includes back here, after expanding it to an absolute path - $matchedFiles = IncludedFilePattern::expand($file); + $matchedFiles = IncludedFilePattern::expand($file, $this->loadingFiles); if ($matchedFiles !== null) { $res = []; foreach ($matchedFiles as $matchedFile) { @@ -40,16 +44,16 @@ public function load(string $file, ?bool $merge = true): array return $res; } - if ($this->generateBaselineFile === null) { - return parent::load($file, $merge); - } - - $normalizedFile = $this->fileHelper->normalizePath($file); - if ($this->generateBaselineFile === $normalizedFile) { + if ($this->generateBaselineFile !== null && $this->generateBaselineFile === $this->fileHelper->normalizePath($file)) { return []; } - return parent::load($file, $merge); + $this->loadingFiles[] = $file; + try { + return parent::load($file, $merge); + } finally { + array_pop($this->loadingFiles); + } } } diff --git a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php index 4d0e73b4e7c..736053bc892 100644 --- a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php +++ b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php @@ -18,6 +18,7 @@ public function testMatchedFilesAreIncluded(): void $this->assertContains('a.identifier', $identifiers); $this->assertContains('b.identifier', $identifiers); $this->assertContains('literal.identifier', $identifiers); + $this->assertContains('sibling.identifier', $identifiers); } public function testMatchedFilesAreIncludedInAlphabeticalOrder(): void diff --git a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php index 337653d5f1d..6c3f147d2d9 100644 --- a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php +++ b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php @@ -14,6 +14,12 @@ public function testPatternMatchesFilesInAlphabeticalOrder(): void $this->assertSame([$dir . '/a.neon', $dir . '/b.neon'], IncludedFilePattern::expand($dir . '/*.neon')); } + public function testPatternDoesNotMatchFilesBeingLoaded(): void + { + $dir = __DIR__ . '/globIncludes/selfIncluding'; + $this->assertSame([$dir . '/sibling.neon'], IncludedFilePattern::expand($dir . '/*.neon', [__DIR__ . '/globIncludes/main.neon', $dir . '/../selfIncluding/_loader.neon'])); + } + public function testPatternMatchingNothingIsEmpty(): void { $this->assertSame([], IncludedFilePattern::expand(__DIR__ . '/globIncludes/nonexistent/*.neon')); diff --git a/tests/PHPStan/DependencyInjection/globIncludes/main.neon b/tests/PHPStan/DependencyInjection/globIncludes/main.neon index e7b961f3527..665ab7b5fd7 100644 --- a/tests/PHPStan/DependencyInjection/globIncludes/main.neon +++ b/tests/PHPStan/DependencyInjection/globIncludes/main.neon @@ -1,6 +1,7 @@ includes: - baselines/*.neon - 'literal/[literal].neon' + - selfIncluding/_loader.neon parametersSchema: globIncludesOrder: string() diff --git a/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/_loader.neon b/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/_loader.neon new file mode 100644 index 00000000000..85016ce3d6e --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/_loader.neon @@ -0,0 +1,2 @@ +includes: + - *.neon diff --git a/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/sibling.neon b/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/sibling.neon new file mode 100644 index 00000000000..82c107820a9 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/selfIncluding/sibling.neon @@ -0,0 +1,5 @@ +parameters: + ignoreErrors: + - + identifier: sibling.identifier + path: '*/sibling.php' From a642cac13e8bac8664e07b73db6a23dbfcc0d71a Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 16:46:57 -0800 Subject: [PATCH 3/7] Match only the include as written against a pattern Whether an include was a pattern was decided after the directory of the including file had been prepended, so a `[`, `]`, `*` or `?` in that directory counted as a metacharacter: * A pattern under a directory like `Projects [old]/` matched nothing, because `[old]` was read as a character class, and the files it was meant to include were dropped silently. * A missing plain include under such a directory was taken for a pattern that matches nothing, so it was dropped too, where it used to fail as a missing file. Now only the entry as written is checked for metacharacters, and the directory it is resolved against is escaped before it goes to glob(). `NeonLoader` matches the pattern in `expandIncludedFile()`, which Nette's loader calls with the entry as written, and `load()` loads the matched files. As a result, a top-level config file, which never goes through `expandIncludedFile()`, is no longer expanded as a pattern either. `ContainerFactory::getConfigFiles()` never expanded those, so the container cache key could have left out files that were loaded. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- src/DependencyInjection/ContainerFactory.php | 7 +-- .../IncludedFilePattern.php | 43 ++++++++++++--- src/DependencyInjection/NeonLoader.php | 33 ++++++++++-- .../DependencyInjection/GlobIncludesTest.php | 11 ++++ .../IncludedFilePatternTest.php | 52 ++++++++++++++++--- .../bracketed [dir]/baselines/bracketed.neon | 5 ++ .../globIncludes/bracketed [dir]/loader.neon | 2 + .../globIncludes/main.neon | 1 + 8 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/baselines/bracketed.neon create mode 100644 tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/loader.neon diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index c56416ebbde..f61d4b5e2d0 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -320,10 +320,11 @@ private static function getConfigFiles( if (isset($data['includes'])) { Validators::assert($data['includes'], 'list', sprintf("section 'includes' in file '%s'", $configFile)); $includes = Helpers::expand($data['includes'], $loaderParameters); + $loadingFiles = [...$loadingFiles, $configFile]; foreach ($includes as $include) { - $include = self::expandIncludedFile($include, $configFile); - foreach (IncludedFilePattern::expand($include, [...$loadingFiles, $configFile]) ?? [$include] as $includedFile) { - [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $includedFile, $loaderParameters, $generateBaselineFile, [...$loadingFiles, $configFile]); + $includedFiles = IncludedFilePattern::expand($include, $configFile, $loadingFiles) ?? [self::expandIncludedFile($include, $configFile)]; + foreach ($includedFiles as $includedFile) { + [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $includedFile, $loaderParameters, $generateBaselineFile, $loadingFiles); $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles); /** @var array $data */ diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index b530a2fcf76..ae08c407624 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -6,18 +6,25 @@ use function array_filter; use function array_map; use function array_values; +use function dirname; use function glob; use function in_array; use function is_file; +use function preg_match; use function realpath; use function sort; +use function strtr; use const SORT_STRING; /** * Lets an entry in `includes` be a glob pattern, like `baselines/*.neon`, * which includes every matching file in alphabetical order. * - * A path that names an existing file is never treated as a pattern, + * Only the entry as written is a pattern. The directory of the file with the includes, + * which a relative entry is resolved against, is matched literally, + * even if its path contains a glob metacharacter. + * + * An entry that names an existing file is never treated as a pattern, * so a file whose name contains a glob metacharacter can still be included. * * The files that are being loaded at the time are left out of the matches, @@ -28,30 +35,50 @@ final class IncludedFilePattern { /** - * @param string $includedFile already expanded to an absolute path - * @param list $loadingFiles the file with the includes, and the files that include it in turn - * @return list|null the matched files, or null when $includedFile is not a pattern + * @param string $include the entry in includes, with its parameters already expanded + * @param string $mainFile the file with the includes + * @param list $loadingFiles $mainFile, and the files that include it in turn + * @return list|null the matched files, or null when $include is not a pattern */ - public static function expand(string $includedFile, array $loadingFiles = []): ?array + public static function expand(string $include, string $mainFile, array $loadingFiles): ?array { - if (is_file($includedFile) || !FileExcluder::isFnmatchPattern($includedFile)) { + if (!FileExcluder::isFnmatchPattern($include)) { + return null; + } + + // same check as Loader::expandIncludedFile() + $baseDirectory = preg_match('#([a-z]+:)?[/\\\\]#Ai', $include) === 1 ? '' : dirname($mainFile) . '/'; + if (is_file($baseDirectory . $include)) { return null; } - $matches = glob($includedFile); + $matches = glob(self::escape($baseDirectory) . $include); if ($matches === false) { return []; } - // glob() sorts with the locale's collation, while the order of includes decides how values merge $loadingPaths = array_map(static fn (string $file): string|false => realpath($file), $loadingFiles); $files = array_values(array_filter( $matches, static fn (string $match): bool => is_file($match) && !in_array(realpath($match), $loadingPaths, true), )); + + // glob() sorts with the locale's collation, while the order of includes decides how values merge sort($files, SORT_STRING); return $files; } + /** + * Makes glob() match $path literally, by putting each metacharacter in a bracket expression of its own. + */ + private static function escape(string $path): string + { + return strtr($path, [ + '[' => '[[]', + '*' => '[*]', + '?' => '[?]', + ]); + } + } diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index 55bd0ba9de2..b5623b0c3cf 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -16,6 +16,13 @@ final class NeonLoader extends Loader /** @var list */ private array $loadingFiles = []; + /** + * The files matched by each included pattern, by the path that expandIncludedFile() returned for it. + * + * @var array> + */ + private array $patternMatches = []; + public function __construct( private FileHelper $fileHelper, private ?string $generateBaselineFile, @@ -29,9 +36,10 @@ public function __construct( #[Override] public function load(string $file, ?bool $merge = true): array { - // Loader::load() passes each entry in includes back here, after expanding it to an absolute path - $matchedFiles = IncludedFilePattern::expand($file, $this->loadingFiles); - if ($matchedFiles !== null) { + if (isset($this->patternMatches[$file])) { + $matchedFiles = $this->patternMatches[$file]; + unset($this->patternMatches[$file]); + $res = []; foreach ($matchedFiles as $matchedFile) { // same merge as for consecutive entries in includes, so later files override earlier ones @@ -56,4 +64,23 @@ public function load(string $file, ?bool $merge = true): array } } + /** + * Loader::load() calls this for each entry in includes, and passes the result back to load(). + * + * The pattern is matched here rather than in load(), because only here is the entry known as written, + * apart from the directory of $mainFile it is resolved against. + */ + #[Override] + public function expandIncludedFile(string $includedFile, string $mainFile): string + { + $expandedFile = parent::expandIncludedFile($includedFile, $mainFile); + + $matchedFiles = IncludedFilePattern::expand($includedFile, $mainFile, $this->loadingFiles); + if ($matchedFiles !== null) { + $this->patternMatches[$expandedFile] = $matchedFiles; + } + + return $expandedFile; + } + } diff --git a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php index 736053bc892..81f3adefbc7 100644 --- a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php +++ b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php @@ -2,6 +2,8 @@ namespace PHPStan\DependencyInjection; +use Nette\FileNotFoundException; +use PHPStan\File\FileHelper; use PHPStan\Testing\PHPStanTestCase; use function array_column; use function array_map; @@ -19,6 +21,7 @@ public function testMatchedFilesAreIncluded(): void $this->assertContains('b.identifier', $identifiers); $this->assertContains('literal.identifier', $identifiers); $this->assertContains('sibling.identifier', $identifiers); + $this->assertContains('bracketed.identifier', $identifiers); } public function testMatchedFilesAreIncludedInAlphabeticalOrder(): void @@ -38,6 +41,14 @@ public function testMatchedFilesAreListedAsConfigFiles(): void $this->assertContains(realpath(__DIR__ . '/globIncludes/literal/[literal].neon'), $allConfigFiles); } + public function testTopLevelConfigFileIsNotAPattern(): void + { + $loader = new NeonLoader(new FileHelper(__DIR__), null); + + $this->expectException(FileNotFoundException::class); + $loader->load(__DIR__ . '/globIncludes/baselines/*.neon'); + } + /** * @return string[] */ diff --git a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php index 6c3f147d2d9..5429cefe6f5 100644 --- a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php +++ b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php @@ -7,33 +7,69 @@ class IncludedFilePatternTest extends TestCase { + private const DIR = __DIR__ . '/globIncludes'; + public function testPatternMatchesFilesInAlphabeticalOrder(): void { // subdir.neon is a directory and c.txt does not match, so neither is included - $dir = __DIR__ . '/globIncludes/baselines'; - $this->assertSame([$dir . '/a.neon', $dir . '/b.neon'], IncludedFilePattern::expand($dir . '/*.neon')); + $mainFile = self::DIR . '/main.neon'; + $this->assertSame( + [self::DIR . '/baselines/a.neon', self::DIR . '/baselines/b.neon'], + IncludedFilePattern::expand('baselines/*.neon', $mainFile, [$mainFile]), + ); + } + + public function testAbsolutePattern(): void + { + $mainFile = self::DIR . '/main.neon'; + $this->assertSame( + [self::DIR . '/baselines/a.neon', self::DIR . '/baselines/b.neon'], + IncludedFilePattern::expand(self::DIR . '/baselines/*.neon', $mainFile, [$mainFile]), + ); } public function testPatternDoesNotMatchFilesBeingLoaded(): void { - $dir = __DIR__ . '/globIncludes/selfIncluding'; - $this->assertSame([$dir . '/sibling.neon'], IncludedFilePattern::expand($dir . '/*.neon', [__DIR__ . '/globIncludes/main.neon', $dir . '/../selfIncluding/_loader.neon'])); + $mainFile = self::DIR . '/selfIncluding/_loader.neon'; + $this->assertSame( + [self::DIR . '/selfIncluding/sibling.neon'], + IncludedFilePattern::expand('*.neon', $mainFile, [self::DIR . '/main.neon', self::DIR . '/selfIncluding/../selfIncluding/_loader.neon']), + ); } public function testPatternMatchingNothingIsEmpty(): void { - $this->assertSame([], IncludedFilePattern::expand(__DIR__ . '/globIncludes/nonexistent/*.neon')); + $mainFile = self::DIR . '/main.neon'; + $this->assertSame([], IncludedFilePattern::expand('baselines/*.php', $mainFile, [$mainFile])); + } + + public function testPatternIsResolvedLiterallyAgainstDirectoryWithMetacharacters(): void + { + $mainFile = self::DIR . '/bracketed [dir]/loader.neon'; + $this->assertSame( + [self::DIR . '/bracketed [dir]/baselines/bracketed.neon'], + IncludedFilePattern::expand('baselines/*.neon', $mainFile, [$mainFile]), + ); } public function testPlainPathIsNotAPattern(): void { - $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/main.neon')); - $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/missing.neon')); + $mainFile = self::DIR . '/main.neon'; + $this->assertNull(IncludedFilePattern::expand('empty.neon', $mainFile, [$mainFile])); + $this->assertNull(IncludedFilePattern::expand('missing.neon', $mainFile, [$mainFile])); + } + + public function testPlainPathInDirectoryWithMetacharactersIsNotAPattern(): void + { + // so that a missing include is still reported as missing + $mainFile = self::DIR . '/bracketed [dir]/loader.neon'; + $this->assertNull(IncludedFilePattern::expand('missing.neon', $mainFile, [$mainFile])); } public function testExistingFileWithMetacharactersIsNotAPattern(): void { - $this->assertNull(IncludedFilePattern::expand(__DIR__ . '/globIncludes/literal/[literal].neon')); + $mainFile = self::DIR . '/main.neon'; + $this->assertNull(IncludedFilePattern::expand('literal/[literal].neon', $mainFile, [$mainFile])); } } diff --git a/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/baselines/bracketed.neon b/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/baselines/bracketed.neon new file mode 100644 index 00000000000..5374ecc6e90 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/baselines/bracketed.neon @@ -0,0 +1,5 @@ +parameters: + ignoreErrors: + - + identifier: bracketed.identifier + path: '*/bracketed.php' diff --git a/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/loader.neon b/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/loader.neon new file mode 100644 index 00000000000..04570fba66c --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/bracketed [dir]/loader.neon @@ -0,0 +1,2 @@ +includes: + - baselines/*.neon diff --git a/tests/PHPStan/DependencyInjection/globIncludes/main.neon b/tests/PHPStan/DependencyInjection/globIncludes/main.neon index 665ab7b5fd7..32396249bca 100644 --- a/tests/PHPStan/DependencyInjection/globIncludes/main.neon +++ b/tests/PHPStan/DependencyInjection/globIncludes/main.neon @@ -2,6 +2,7 @@ includes: - baselines/*.neon - 'literal/[literal].neon' - selfIncluding/_loader.neon + - 'bracketed [dir]/loader.neon' parametersSchema: globIncludesOrder: string() From 7d027a3925b0a37b2c1aeae33b008d23e8368363 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 16:47:16 -0800 Subject: [PATCH 4/7] Leave unresolvable files out of the files being loaded realpath() returns false for a file it cannot resolve, like PHPStan's own config files inside phpstan.phar, which are among the files being loaded whenever a project config is. With false in the list, a match whose realpath() also failed would have been taken for a file being loaded and left out of the pattern's matches. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- src/DependencyInjection/IncludedFilePattern.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index ae08c407624..ed419efac22 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -57,7 +57,9 @@ public static function expand(string $include, string $mainFile, array $loadingF return []; } - $loadingPaths = array_map(static fn (string $file): string|false => realpath($file), $loadingFiles); + // realpath() fails on files that cannot be resolved, like the ones inside phpstan.phar, + // and a false among these would leave out every match that cannot be resolved either + $loadingPaths = array_filter(array_map(static fn (string $file): string|false => realpath($file), $loadingFiles), static fn (string|false $path): bool => $path !== false); $files = array_values(array_filter( $matches, static fn (string $match): bool => is_file($match) && !in_array(realpath($match), $loadingPaths, true), From b708e4dcad7d33d0c4327bcccb42312ce980fa22 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 16:47:57 -0800 Subject: [PATCH 5/7] Fail when the directory of an included pattern is missing A pattern that matches nothing still includes nothing, so that the last file in a directory can be deleted, such as a baseline fixed down to zero. But a pattern whose directory does not exist, like `baseline/*.neon` for `baselines/`, is more likely a typo, and it used to disable the files it was meant to include without a word. It now fails like a missing plain include does, with Nette's FileNotFoundException naming the directory and the pattern. The directory is the one before the segment with the first metacharacter, so `modules/*/phpstan.neon` needs `modules/` to exist. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- .../IncludedFilePattern.php | 26 +++++++++++++++++++ .../IncludedFilePatternTest.php | 21 +++++++++++++++ .../globIncludes/empty.neon | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index ed419efac22..0b85e1c2da7 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -2,6 +2,7 @@ namespace PHPStan\DependencyInjection; +use Nette\FileNotFoundException; use PHPStan\File\FileExcluder; use function array_filter; use function array_map; @@ -9,10 +10,12 @@ use function dirname; use function glob; use function in_array; +use function is_dir; use function is_file; use function preg_match; use function realpath; use function sort; +use function sprintf; use function strtr; use const SORT_STRING; @@ -27,6 +30,9 @@ * An entry that names an existing file is never treated as a pattern, * so a file whose name contains a glob metacharacter can still be included. * + * A pattern that matches nothing includes nothing, + * but the directory it is matched in has to exist. + * * The files that are being loaded at the time are left out of the matches, * so that a loader like `baselines/_loader.neon` can include `*.neon` * without including itself. @@ -39,6 +45,7 @@ final class IncludedFilePattern * @param string $mainFile the file with the includes * @param list $loadingFiles $mainFile, and the files that include it in turn * @return list|null the matched files, or null when $include is not a pattern + * @throws FileNotFoundException when the directory the pattern is matched in is missing */ public static function expand(string $include, string $mainFile, array $loadingFiles): ?array { @@ -52,6 +59,13 @@ public static function expand(string $include, string $mainFile, array $loadingF return null; } + // a pattern that matches nothing includes nothing, so that the last matching file can be deleted, + // but one whose directory is missing is more likely a mistake, like a missing plain include + $patternDirectory = $baseDirectory . self::getDirectoryBeforePattern($include); + if (!is_dir($patternDirectory)) { + throw new FileNotFoundException(sprintf("Directory '%s' of the included pattern '%s' is missing.", $patternDirectory, $include)); + } + $matches = glob(self::escape($baseDirectory) . $include); if ($matches === false) { return []; @@ -71,6 +85,18 @@ public static function expand(string $include, string $mainFile, array $loadingF return $files; } + /** + * Returns the part of $include up to the directory separator before the segment with its first metacharacter. + */ + private static function getDirectoryBeforePattern(string $include): string + { + if (preg_match('~^((?:[^*?[\]]*[/\\\\])?)[^/\\\\*?[\]]*[*?[\]]~', $include, $matches) !== 1) { + return ''; + } + + return $matches[1]; + } + /** * Makes glob() match $path literally, by putting each metacharacter in a bracket expression of its own. */ diff --git a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php index 5429cefe6f5..83ff3354de1 100644 --- a/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php +++ b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php @@ -2,7 +2,9 @@ namespace PHPStan\DependencyInjection; +use Nette\FileNotFoundException; use PHPUnit\Framework\TestCase; +use function sprintf; class IncludedFilePatternTest extends TestCase { @@ -43,6 +45,25 @@ public function testPatternMatchingNothingIsEmpty(): void $this->assertSame([], IncludedFilePattern::expand('baselines/*.php', $mainFile, [$mainFile])); } + public function testPatternInMissingDirectoryFails(): void + { + $mainFile = self::DIR . '/main.neon'; + + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf("Directory '%s/nonexistent/' of the included pattern 'nonexistent/*.neon' is missing.", self::DIR)); + IncludedFilePattern::expand('nonexistent/*.neon', $mainFile, [$mainFile]); + } + + public function testDirectoryOfPatternIsTheOneBeforeTheFirstMetacharacter(): void + { + // baselines/ exists, so this matches nothing rather than failing on the missing baselines/*/ + $mainFile = self::DIR . '/main.neon'; + $this->assertSame([], IncludedFilePattern::expand('baselines/*/missing/*.neon', $mainFile, [$mainFile])); + + $this->expectException(FileNotFoundException::class); + IncludedFilePattern::expand('nonexistent/sub/*/x.neon', $mainFile, [$mainFile]); + } + public function testPatternIsResolvedLiterallyAgainstDirectoryWithMetacharacters(): void { $mainFile = self::DIR . '/bracketed [dir]/loader.neon'; diff --git a/tests/PHPStan/DependencyInjection/globIncludes/empty.neon b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon index 8b5983d9234..0c614182265 100644 --- a/tests/PHPStan/DependencyInjection/globIncludes/empty.neon +++ b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon @@ -1,2 +1,2 @@ includes: - - nonexistent/*.neon + - baselines/*.php From 6ea7ed5fbca7adce62b8331fbbaed4394e2600af Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 17:17:38 -0800 Subject: [PATCH 6/7] Do less work per include when no pattern is used LoaderFactoryBench::benchLoadConfigAgain measured 4.7 % more instructions than expected, from the work this branch added to every include and every loaded file, whether a pattern is used or not: * IncludedFilePattern::expand() checked each include for metacharacters with the regex in FileExcluder::isFnmatchPattern(). strpbrk() finds the same characters without a regex. * NeonLoader::load() wrapped each file in try/finally to pop the stack of files being loaded. Nette's own Loader::load() unsets its $loadedFiles without finally, since a config that failed to load leaves the loader with nothing to continue with, so the stack is now popped the same way. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- src/DependencyInjection/IncludedFilePattern.php | 6 ++++-- src/DependencyInjection/NeonLoader.php | 11 ++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index 0b85e1c2da7..628e54ce03a 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -3,7 +3,6 @@ namespace PHPStan\DependencyInjection; use Nette\FileNotFoundException; -use PHPStan\File\FileExcluder; use function array_filter; use function array_map; use function array_values; @@ -16,6 +15,7 @@ use function realpath; use function sort; use function sprintf; +use function strpbrk; use function strtr; use const SORT_STRING; @@ -49,7 +49,9 @@ final class IncludedFilePattern */ public static function expand(string $include, string $mainFile, array $loadingFiles): ?array { - if (!FileExcluder::isFnmatchPattern($include)) { + // the same metacharacters as FileExcluder::isFnmatchPattern(), but without a regex, + // because this runs for every include whenever the config is loaded + if (strpbrk($include, '*?[]') === false) { return null; } diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index b5623b0c3cf..5a54542c4fe 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -56,12 +56,13 @@ public function load(string $file, ?bool $merge = true): array return []; } + // like Loader::$loadedFiles, which the parent does not unset either when loading fails, + // as a config that failed to load leaves the loader with nothing to continue with $this->loadingFiles[] = $file; - try { - return parent::load($file, $merge); - } finally { - array_pop($this->loadingFiles); - } + $res = parent::load($file, $merge); + array_pop($this->loadingFiles); + + return $res; } /** From 1773240d8807656ccdd189936689935252b61a95 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 16 Nov 2026 17:53:35 -0800 Subject: [PATCH 7/7] Skip the pattern helper for plain includes in NeonLoader LoaderFactoryBench::benchLoadConfigAgain still measured 3.7 % more instructions than expected. NeonLoader::expandIncludedFile() called IncludedFilePattern::expand() for every include, only for it to find no metacharacters in nearly all of them. It now looks for them itself and calls the helper only when one is found, through a shared IncludedFilePattern::METACHARACTERS constant so the two checks cannot drift apart. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Session: https://claude.ai/code/session_01Tnky3HeNQTYTAish2dfgd9 Claude-Code-Session-Id: 020072d9-40ca-4276-af5c-1783ac89d13b --- src/DependencyInjection/IncludedFilePattern.php | 10 +++++++--- src/DependencyInjection/NeonLoader.php | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/IncludedFilePattern.php b/src/DependencyInjection/IncludedFilePattern.php index 628e54ce03a..612adc0cdaa 100644 --- a/src/DependencyInjection/IncludedFilePattern.php +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -40,6 +40,12 @@ final class IncludedFilePattern { + /** + * The same metacharacters as FileExcluder::isFnmatchPattern() looks for, to find them with strpbrk() + * rather than a regex, because every include is checked whenever the config is loaded. + */ + public const METACHARACTERS = '*?[]'; + /** * @param string $include the entry in includes, with its parameters already expanded * @param string $mainFile the file with the includes @@ -49,9 +55,7 @@ final class IncludedFilePattern */ public static function expand(string $include, string $mainFile, array $loadingFiles): ?array { - // the same metacharacters as FileExcluder::isFnmatchPattern(), but without a regex, - // because this runs for every include whenever the config is loaded - if (strpbrk($include, '*?[]') === false) { + if (strpbrk($include, self::METACHARACTERS) === false) { return null; } diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index 5a54542c4fe..ba311053f68 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -9,6 +9,7 @@ use PHPStan\ShouldNotHappenException; use function array_pop; use function is_array; +use function strpbrk; final class NeonLoader extends Loader { @@ -76,6 +77,11 @@ public function expandIncludedFile(string $includedFile, string $mainFile): stri { $expandedFile = parent::expandIncludedFile($includedFile, $mainFile); + // checked here too, so that the common case of a plain include does not pay for the call + if (strpbrk($includedFile, IncludedFilePattern::METACHARACTERS) === false) { + return $expandedFile; + } + $matchedFiles = IncludedFilePattern::expand($includedFile, $mainFile, $this->loadingFiles); if ($matchedFiles !== null) { $this->patternMatches[$expandedFile] = $matchedFiles;