Skip to content

Commit

Permalink
Validate parameters with parametersSchema each time after container c…
Browse files Browse the repository at this point in the history
…reation

This allows for validation of dynamic parameters
  • Loading branch information
ondrejmirtes committed Aug 1, 2023
1 parent eeb2c63 commit 0d1811e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 111 deletions.
125 changes: 114 additions & 11 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace PHPStan\DependencyInjection;

use Nette\DI\Config\Adapters\PhpAdapter;
use Nette\DI\Definitions\Statement;
use Nette\DI\Extensions\ExtensionsExtension;
use Nette\DI\Extensions\PhpExtension;
use Nette\DI\Helpers;
use Nette\Schema\Context as SchemaContext;
use Nette\Schema\Elements\AnyOf;
use Nette\Schema\Elements\Structure;
use Nette\Schema\Elements\Type;
use Nette\Schema\Expect;
use Nette\Schema\Processor;
use Nette\Schema\Schema;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use Phar;
Expand All @@ -21,6 +29,7 @@
use PHPStan\Reflection\PhpVersionStaticAccessor;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -89,8 +98,8 @@ public function create(
?string $cliAutoloadFile = null,
): Container
{
$allConfigFiles = $this->detectDuplicateIncludedFiles(
$additionalConfigFiles,
[$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles(
array_merge([__DIR__ . '/../../conf/parametersSchema.neon'], $additionalConfigFiles),
[
'rootDir' => $this->rootDirectory,
'currentWorkingDirectory' => $this->currentWorkingDirectory,
Expand Down Expand Up @@ -134,6 +143,7 @@ public function create(
$configurator->setAllConfigFiles($allConfigFiles);

$container = $configurator->createContainer()->getByType(Container::class);
$this->validateParameters($container->getParameters(), $projectConfig['parametersSchema']);
self::postInitializeContainer($container);

return $container;
Expand Down Expand Up @@ -232,7 +242,7 @@ public function getConfigDirectory(): string
/**
* @param string[] $configFiles
* @param array<string, string> $loaderParameters
* @return string[]
* @return array{list<string>, array<mixed>}
* @throws DuplicateIncludedFilesException
*/
private function detectDuplicateIncludedFiles(
Expand All @@ -243,19 +253,22 @@ private function detectDuplicateIncludedFiles(
$neonAdapter = new NeonAdapter();

Check failure on line 253 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 253 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
$phpAdapter = new PhpAdapter();

Check failure on line 254 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 254 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
$allConfigFiles = [];
$configArray = [];

Check failure on line 256 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 256 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
foreach ($configFiles as $configFile) {

Check failure on line 257 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 257 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
$allConfigFiles = array_merge($allConfigFiles, self::getConfigFiles($this->fileHelper, $neonAdapter, $phpAdapter, $configFile, $loaderParameters, null));
[$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($this->fileHelper, $neonAdapter, $phpAdapter, $configFile, $loaderParameters, null);
$allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles);
$configArray = \Nette\Schema\Helpers::merge($tmpConfigArray, $configArray);
}

$normalized = array_map(fn (string $file): string => $this->fileHelper->normalizePath($file), $allConfigFiles);

$deduplicated = array_unique($normalized);
if (count($normalized) <= count($deduplicated)) {
return $normalized;
return [$normalized, $configArray];

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 267 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
}

if (!$this->checkDuplicateFiles) {
return $normalized;
return [$normalized, $configArray];

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 271 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
}

Check failure on line 273 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.

Check failure on line 273 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::detectDuplicateIncludedFiles() should return array{list<string>, array} but returns array{array<string>, array|string}.
$duplicateFiles = array_unique(array_diff_key($normalized, $deduplicated));
Expand All @@ -265,7 +278,7 @@ private function detectDuplicateIncludedFiles(

/**
* @param array<string, string> $loaderParameters
* @return string[]
* @return array{string[], array<mixed>}
*/
private static function getConfigFiles(
FileHelper $fileHelper,
Expand All @@ -277,10 +290,10 @@ private static function getConfigFiles(
): array

Check failure on line 290 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 290 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.
{

Check failure on line 291 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 291 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.
if ($generateBaselineFile === $fileHelper->normalizePath($configFile)) {
return [];
return [[], []];
}
if (!is_file($configFile) || !is_readable($configFile)) {
return [];
return [[], []];
}

if (str_ends_with($configFile, '.php')) {
Expand All @@ -294,11 +307,13 @@ private static function getConfigFiles(
$includes = Helpers::expand($data['includes'], $loaderParameters);

Check failure on line 307 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 307 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.
foreach ($includes as $include) {
$include = self::expandIncludedFile($include, $configFile);
$allConfigFiles = array_merge($allConfigFiles, self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile));
[$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile);

Check failure on line 310 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 310 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.
$allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles);
$data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data);
}
}

return $allConfigFiles;
return [$allConfigFiles, $data];

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.

Check failure on line 316 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\DependencyInjection\ContainerFactory::getConfigFiles() should return array{array<string>, array} but returns array{non-empty-array<string>, array|string}.
}

private static function expandIncludedFile(string $includedFile, string $mainFile): string
Expand All @@ -308,4 +323,92 @@ private static function expandIncludedFile(string $includedFile, string $mainFil
: dirname($mainFile) . '/' . $includedFile;
}

/**

Check failure on line 326 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 326 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.
* @param array<mixed> $parameters
* @param array<mixed> $parametersSchema
*/
private function validateParameters(array $parameters, array $parametersSchema): void
{
if (!$parameters['__validate']) {

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.

Check failure on line 332 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Only booleans are allowed in a negated boolean, mixed given.
return;
}

$schema = $this->processArgument(

Check failure on line 336 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 336 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Variable static method call on Nette\Schema\Expect.
new Statement('schema', [

Check failure on line 337 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 337 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Variable static method call on Nette\Schema\Expect.
new Statement('structure', [$parametersSchema]),

Check failure on line 338 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 338 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.
]),

Check failure on line 339 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 339 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.
);
$processor = new Processor();
$processor->onNewContext[] = static function (SchemaContext $context): void {
$context->path = ['parameters'];
};
$processor->process($schema, $parameters);
}

/**
* @param Statement[] $statements
*/
private function processSchema(array $statements, bool $required = true): Schema
{
if (count($statements) === 0) {
throw new ShouldNotHappenException();
}

$parameterSchema = null;
foreach ($statements as $statement) {

Check failure on line 358 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 358 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Variable static method call on Nette\Schema\Expect.
$processedArguments = array_map(fn ($argument) => $this->processArgument($argument), $statement->arguments);
if ($parameterSchema === null) {

Check failure on line 360 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 360 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.
/** @var Type|AnyOf|Structure $parameterSchema */
$parameterSchema = Expect::{$statement->getEntity()}(...$processedArguments);

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Variable static method call on Nette\Schema\Expect.

Check failure on line 362 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Variable static method call on Nette\Schema\Expect.
} else {
$parameterSchema->{$statement->getEntity()}(...$processedArguments);

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.

Check failure on line 364 in src/DependencyInjection/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Variable method call on Nette\Schema\Elements\AnyOf|Nette\Schema\Elements\Structure|Nette\Schema\Elements\Type.
}
}

if ($required) {
$parameterSchema->required();
}

return $parameterSchema;
}

/**
* @param mixed $argument
* @return mixed
*/
private function processArgument($argument, bool $required = true)
{
if ($argument instanceof Statement) {
if ($argument->entity === 'schema') {
$arguments = [];
foreach ($argument->arguments as $schemaArgument) {
if (!$schemaArgument instanceof Statement) {
throw new ShouldNotHappenException('schema() should contain another statement().');
}

$arguments[] = $schemaArgument;
}

if (count($arguments) === 0) {
throw new ShouldNotHappenException('schema() should have at least one argument.');
}

return $this->processSchema($arguments, $required);
}

return $this->processSchema([$argument], $required);
} elseif (is_array($argument)) {
$processedArray = [];
foreach ($argument as $key => $val) {
$required = $key[0] !== '?';
$key = $required ? $key : substr($key, 1);
$processedArray[$key] = $this->processArgument($val, $required);
}

return $processedArray;
}

return $argument;
}

}
100 changes: 0 additions & 100 deletions src/DependencyInjection/ParametersSchemaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@

use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Context as SchemaContext;
use Nette\Schema\DynamicParameter;
use Nette\Schema\Elements\AnyOf;
use Nette\Schema\Elements\Structure;
use Nette\Schema\Elements\Type;
use Nette\Schema\Expect;
use Nette\Schema\Processor;
use Nette\Schema\Schema;
use PHPStan\ShouldNotHappenException;
use function array_map;
use function count;
use function is_array;
use function substr;

class ParametersSchemaExtension extends CompilerExtension
{
Expand All @@ -26,93 +15,4 @@ public function getConfigSchema(): Schema
return Expect::arrayOf(Expect::type(Statement::class))->min(1);
}

public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
if (!$builder->parameters['__validate']) {
return;
}

/** @var mixed[] $config */
$config = $this->config;
$config['analysedPaths'] = new Statement(DynamicParameter::class);
$config['analysedPathsFromConfig'] = new Statement(DynamicParameter::class);
$schema = $this->processArgument(
new Statement('schema', [
new Statement('structure', [$config]),
]),
);
$processor = new Processor();
$processor->onNewContext[] = static function (SchemaContext $context): void {
$context->path = ['parameters'];
};
$processor->process($schema, $builder->parameters);
}

/**
* @param Statement[] $statements
*/
private function processSchema(array $statements, bool $required = true): Schema
{
if (count($statements) === 0) {
throw new ShouldNotHappenException();
}

$parameterSchema = null;
foreach ($statements as $statement) {
$processedArguments = array_map(fn ($argument) => $this->processArgument($argument), $statement->arguments);
if ($parameterSchema === null) {
/** @var Type|AnyOf|Structure $parameterSchema */
$parameterSchema = Expect::{$statement->getEntity()}(...$processedArguments);
} else {
$parameterSchema->{$statement->getEntity()}(...$processedArguments);
}
}

if ($required) {
$parameterSchema->required();
}

return $parameterSchema;
}

/**
* @param mixed $argument
* @return mixed
*/
private function processArgument($argument, bool $required = true)
{
if ($argument instanceof Statement) {
if ($argument->entity === 'schema') {
$arguments = [];
foreach ($argument->arguments as $schemaArgument) {
if (!$schemaArgument instanceof Statement) {
throw new ShouldNotHappenException('schema() should contain another statement().');
}

$arguments[] = $schemaArgument;
}

if (count($arguments) === 0) {
throw new ShouldNotHappenException('schema() should have at least one argument.');
}

return $this->processSchema($arguments, $required);
}

return $this->processSchema([$argument], $required);
} elseif (is_array($argument)) {
$processedArray = [];
foreach ($argument as $key => $val) {
$required = $key[0] !== '?';
$key = $required ? $key : substr($key, 1);
$processedArray[$key] = $this->processArgument($val, $required);
}

return $processedArray;
}

return $argument;
}

}

0 comments on commit 0d1811e

Please sign in to comment.