From e7fa73a32b6edc6bb50a1eb8d007eb156cb1f4f9 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 29 Feb 2020 18:13:08 +0100 Subject: [PATCH] Fix container lint command when a synthetic service is used in combination with the expression language --- .../Compiler/CheckTypeDeclarationsPass.php | 7 ++++++- .../Compiler/CheckTypeDeclarationsPassTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php index b132174932dc..2d69fe6eb665 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php @@ -191,7 +191,12 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar } elseif ($value instanceof Parameter) { $value = $this->container->getParameter($value); } elseif ($value instanceof Expression) { - $value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]); + try { + $value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]); + } catch (\Exception $e) { + // If a service from the expression cannot be fetched from the container, we skip the validation. + return; + } } elseif (\is_string($value)) { if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) { // Only array parameters are not inlined when dumped. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php index 79cbacecfc5b..081db17468b1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php @@ -739,4 +739,20 @@ public function testProcessSuccessWhenPassingServiceClosureArgumentToClosure() $this->addToAssertionCount(1); } + + public function testExpressionLanguageWithSyntheticService() + { + $container = new ContainerBuilder(); + + $container->register('synthetic') + ->setSynthetic(true); + $container->register('baz', \stdClass::class) + ->addArgument(new Reference('synthetic')); + $container->register('bar', Bar::class) + ->addArgument(new Expression('service("baz").getStdClass()')); + + (new CheckTypeDeclarationsPass())->process($container); + + $this->addToAssertionCount(1); + } }