Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ private function detectDuplicateIncludedFiles(

/**
* @param array<string, string> $loaderParameters
* @param list<string> $loadingFiles the files that include $configFile, in turn
* @return array{list<string>, array<mixed>}
*/
private static function getConfigFiles(
Expand All @@ -300,6 +301,7 @@ private static function getConfigFiles(
string $configFile,
array $loaderParameters,
?string $generateBaselineFile,
array $loadingFiles = [],
): array
{
if ($generateBaselineFile === $fileHelper->normalizePath($configFile)) {
Expand All @@ -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<mixed> $data */
$data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data);
/** @var array<mixed> $data */
$data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data);
}
}
}

Expand Down
118 changes: 118 additions & 0 deletions src/DependencyInjection/IncludedFilePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use Nette\FileNotFoundException;
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_dir;
use function is_file;
use function preg_match;
use function realpath;
use function sort;
use function sprintf;
use function strpbrk;
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.
*
* 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.
*
* 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.
*/
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
* @param list<string> $loadingFiles $mainFile, and the files that include it in turn
* @return list<string>|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, [
'[' => '[[]',
'*' => '[*]',
'?' => '[?]',
]);
}

}
66 changes: 61 additions & 5 deletions src/DependencyInjection/NeonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
private array $loadingFiles = [];

/**
* The files matched by each included pattern, by the path that expandIncludedFile() returned for it.
*
* @var array<string, list<string>>
*/
private array $patternMatches = [];

public function __construct(
private FileHelper $fileHelper,
private ?string $generateBaselineFile,
Expand All @@ -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;
}

}
63 changes: 63 additions & 0 deletions tests/PHPStan/DependencyInjection/GlobIncludesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use Nette\FileNotFoundException;
use PHPStan\File\FileHelper;
use PHPStan\Testing\PHPStanTestCase;
use function array_column;
use function array_map;
use function realpath;

class GlobIncludesTest extends PHPStanTestCase
{

public function testMatchedFilesAreIncluded(): void
{
/** @var list<array{identifier?: string}> $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<string> $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',
];
}

}
Loading
Loading