Skip to content

Commit 32d06df

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: Fix merge. Fix CS [Messenger] Test generated SQL [Config] YamlReferenceDumper: No default value required for VariableNode with array example Remove PHPUnit configuration files from components without tests. [DependencyInjection] Fix container linter for union types.
2 parents 5f2738e + a8d87fe commit 32d06df

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

Compiler/CheckTypeDeclarationsPass.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,27 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
153153
/**
154154
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
155155
*/
156-
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix, string $type = null): void
156+
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix, \ReflectionType $reflectionType = null): void
157157
{
158-
if (null === $type) {
159-
$type = $parameter->getType();
158+
$reflectionType = $reflectionType ?? $parameter->getType();
160159

161-
if ($type instanceof \ReflectionUnionType) {
162-
foreach ($type->getTypes() as $type) {
163-
try {
164-
$this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $type);
160+
if ($reflectionType instanceof \ReflectionUnionType) {
161+
foreach ($reflectionType->getTypes() as $t) {
162+
try {
163+
$this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $t);
165164

166-
return;
167-
} catch (InvalidParameterTypeException $e) {
168-
}
165+
return;
166+
} catch (InvalidParameterTypeException $e) {
169167
}
170-
171-
throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter);
172168
}
173169

174-
$type = $type->getName();
170+
throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter);
175171
}
172+
if (!$reflectionType instanceof \ReflectionNamedType) {
173+
return;
174+
}
175+
176+
$type = $reflectionType->getName();
176177

177178
if ($value instanceof Reference) {
178179
if (!$this->container->has($value = (string) $value)) {
@@ -285,7 +286,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
285286

286287
$checkFunction = sprintf('is_%s', $type);
287288

288-
if (!$parameter->getType()->isBuiltin() || !$checkFunction($value)) {
289+
if (!$reflectionType->isBuiltin() || !$checkFunction($value)) {
289290
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : get_debug_type($value), $parameter);
290291
}
291292
}

Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarOptionalArgumentNotNull;
2727
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo;
2828
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\FooObject;
29+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructor;
2930
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo;
3031
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Wobble;
3132
use Symfony\Component\ExpressionLanguage\Expression;
@@ -803,4 +804,72 @@ public function testProcessResolveParameters()
803804

804805
putenv('ARRAY=');
805806
}
807+
808+
/**
809+
* @requires PHP 8
810+
*/
811+
public function testUnionTypePassesWithReference()
812+
{
813+
$container = new ContainerBuilder();
814+
815+
$container->register('foo', Foo::class);
816+
$container->register('union', UnionConstructor::class)
817+
->setArguments([new Reference('foo')]);
818+
819+
(new CheckTypeDeclarationsPass(true))->process($container);
820+
821+
$this->addToAssertionCount(1);
822+
}
823+
824+
/**
825+
* @requires PHP 8
826+
*/
827+
public function testUnionTypePassesWithBuiltin()
828+
{
829+
$container = new ContainerBuilder();
830+
831+
$container->register('union', UnionConstructor::class)
832+
->setArguments([42]);
833+
834+
(new CheckTypeDeclarationsPass(true))->process($container);
835+
836+
$this->addToAssertionCount(1);
837+
}
838+
839+
/**
840+
* @requires PHP 8
841+
*/
842+
public function testUnionTypeFailsWithReference()
843+
{
844+
$container = new ContainerBuilder();
845+
846+
$container->register('waldo', Waldo::class);
847+
$container->register('union', UnionConstructor::class)
848+
->setArguments([new Reference('waldo')]);
849+
850+
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
851+
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo" passed.');
852+
853+
(new CheckTypeDeclarationsPass(true))->process($container);
854+
855+
$this->addToAssertionCount(1);
856+
}
857+
858+
/**
859+
* @requires PHP 8
860+
*/
861+
public function testUnionTypeFailsWithBuiltin()
862+
{
863+
$container = new ContainerBuilder();
864+
865+
$container->register('union', UnionConstructor::class)
866+
->setArguments([[1, 2, 3]]);
867+
868+
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
869+
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "array" passed.');
870+
871+
(new CheckTypeDeclarationsPass(true))->process($container);
872+
873+
$this->addToAssertionCount(1);
874+
}
806875
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass;
4+
5+
class UnionConstructor
6+
{
7+
public function __construct(Foo|int $arg)
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)