Skip to content

Commit

Permalink
Validate parametersSchema only during DIC compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 3, 2022
1 parent 692158c commit 50f25e9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
24 changes: 6 additions & 18 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Nette\DI\ServiceCreationException;
use Nette\FileNotFoundException;
use Nette\InvalidStateException;
use Nette\Schema\Context as SchemaContext;
use Nette\Schema\Processor;
use Nette\Schema\ValidationException;
use Nette\Utils\AssertionException;
use Nette\Utils\Strings;
Expand Down Expand Up @@ -308,6 +306,12 @@ public static function begin(
$errorOutput->writeLineFormatted('');
}
throw new InceptionNotSuccessfulException();
} catch (ValidationException $e) {
foreach ($e->getMessages() as $message) {
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($message);
}
throw new InceptionNotSuccessfulException();
} catch (ServiceCreationException $e) {
$matches = Strings::match($e->getMessage(), '#Service of type (?<serviceType>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]): Service of type (?<parserServiceType>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]) needed by \$(?<parameterName>[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*) in (?<methodName>[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*)\(\)#');
if ($matches === null) {
Expand Down Expand Up @@ -367,22 +371,6 @@ public static function begin(
$defaultLevelUsed = false;
}

$schema = $container->getParameter('__parametersSchema');
$processor = new Processor();
$processor->onNewContext[] = static function (SchemaContext $context): void {
$context->path = ['parameters'];
};

try {
$processor->process($schema, $container->getParameters());
} catch (ValidationException $e) {
foreach ($e->getMessages() as $message) {
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($message);
}
throw new InceptionNotSuccessfulException();
}

foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) {
self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/NeonAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class NeonAdapter implements Adapter
{

public const CACHE_KEY = 'v16-ignored-errors-validate';
public const CACHE_KEY = 'v17-validate-schema';

private const PREVENT_MERGING_SUFFIX = '!';

Expand Down
17 changes: 14 additions & 3 deletions src/DependencyInjection/ParametersSchemaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

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;
Expand All @@ -26,13 +29,21 @@ public function loadConfiguration(): void
{
/** @var mixed[] $config */
$config = $this->config;
$config['__parametersSchema'] = new Statement(Schema::class);
$builder = $this->getContainerBuilder();
$builder->parameters['__parametersSchema'] = $this->processArgument(
$config['analysedPaths'] = new Statement(DynamicParameter::class);
$config['analysedPathsFromConfig'] = new Statement(DynamicParameter::class);
$config['singleReflectionFile'] = new Statement(DynamicParameter::class);
$config['singleReflectionInsteadOfFile'] = 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'];
};
$builder = $this->getContainerBuilder();
$processor->process($schema, $builder->parameters);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Exceptions/bug-5364.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
implicitThrows: false
exceptions:
implicitThrows: false
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true

0 comments on commit 50f25e9

Please sign in to comment.