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..f61d4b5e2d0 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)) { @@ -318,13 +320,16 @@ 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); - [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile); - $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles); + $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 */ - $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..612adc0cdaa --- /dev/null +++ b/src/DependencyInjection/IncludedFilePattern.php @@ -0,0 +1,118 @@ + $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 + { + if (strpbrk($include, self::METACHARACTERS) === false) { + return null; + } + + // same check as Loader::expandIncludedFile() + $baseDirectory = preg_match('#([a-z]+:)?[/\\\\]#Ai', $include) === 1 ? '' : dirname($mainFile) . '/'; + if (is_file($baseDirectory . $include)) { + 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 []; + } + + // 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), + )); + + // glob() sorts with the locale's collation, while the order of includes decides how values merge + sort($files, SORT_STRING); + + 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. + */ + private static function escape(string $path): string + { + return strtr($path, [ + '[' => '[[]', + '*' => '[*]', + '?' => '[?]', + ]); + } + +} diff --git a/src/DependencyInjection/NeonLoader.php b/src/DependencyInjection/NeonLoader.php index 6664157fcfd..ba311053f68 100644 --- a/src/DependencyInjection/NeonLoader.php +++ b/src/DependencyInjection/NeonLoader.php @@ -3,12 +3,27 @@ namespace PHPStan\DependencyInjection; use Nette\DI\Config\Loader; +use Nette\Schema\Helpers; use Override; use PHPStan\File\FileHelper; +use PHPStan\ShouldNotHappenException; +use function array_pop; +use function is_array; +use function strpbrk; 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, @@ -22,16 +37,57 @@ public function __construct( #[Override] public function load(string $file, ?bool $merge = true): array { - if ($this->generateBaselineFile === null) { - return parent::load($file, $merge); + 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 + $res = Helpers::merge($this->load($matchedFile, $merge), $res); + if (!is_array($res)) { + throw new ShouldNotHappenException(); + } + } + + return $res; } - $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); + // 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; + $res = parent::load($file, $merge); + array_pop($this->loadingFiles); + + return $res; + } + + /** + * 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); + + // 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; + } + + return $expandedFile; } } diff --git a/tests/PHPStan/DependencyInjection/GlobIncludesTest.php b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php new file mode 100644 index 00000000000..81f3adefbc7 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/GlobIncludesTest.php @@ -0,0 +1,63 @@ + $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); + $this->assertContains('sibling.identifier', $identifiers); + $this->assertContains('bracketed.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); + } + + public function testTopLevelConfigFileIsNotAPattern(): void + { + $loader = new NeonLoader(new FileHelper(__DIR__), null); + + $this->expectException(FileNotFoundException::class); + $loader->load(__DIR__ . '/globIncludes/baselines/*.neon'); + } + + /** + * @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..83ff3354de1 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/IncludedFilePatternTest.php @@ -0,0 +1,96 @@ +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 + { + $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 + { + $mainFile = self::DIR . '/main.neon'; + $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'; + $this->assertSame( + [self::DIR . '/bracketed [dir]/baselines/bracketed.neon'], + IncludedFilePattern::expand('baselines/*.neon', $mainFile, [$mainFile]), + ); + } + + public function testPlainPathIsNotAPattern(): void + { + $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 + { + $mainFile = self::DIR . '/main.neon'; + $this->assertNull(IncludedFilePattern::expand('literal/[literal].neon', $mainFile, [$mainFile])); + } + +} 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/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/empty.neon b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon new file mode 100644 index 00000000000..0c614182265 --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/empty.neon @@ -0,0 +1,2 @@ +includes: + - baselines/*.php 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..32396249bca --- /dev/null +++ b/tests/PHPStan/DependencyInjection/globIncludes/main.neon @@ -0,0 +1,8 @@ +includes: + - baselines/*.neon + - 'literal/[literal].neon' + - selfIncluding/_loader.neon + - 'bracketed [dir]/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'