Skip to content

Commit d827334

Browse files
Guillaume Pédelagrabefabpot
authored andcommitted
[DI] Fix CheckTypeDeclarationPass
1 parent 25e13ee commit d827334

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

Compiler/CheckTypeDeclarationsPass.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1818
use Symfony\Component\DependencyInjection\Container;
1919
use Symfony\Component\DependencyInjection\Definition;
20-
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
2120
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2221
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
2322
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
@@ -207,7 +206,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
207206
if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) {
208207
try {
209208
$value = $this->container->resolveEnvPlaceholders($value, true);
210-
} catch (EnvNotFoundException | RuntimeException $e) {
209+
} catch (\Exception $e) {
211210
// If an env placeholder cannot be resolved, we skip the validation.
212211
return;
213212
}
@@ -250,7 +249,11 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
250249
return;
251250
}
252251

253-
if ('iterable' === $type && (\is_array($value) || is_subclass_of($class, \Traversable::class))) {
252+
if ('iterable' === $type && (\is_array($value) || 'array' === $class || is_subclass_of($class, \Traversable::class))) {
253+
return;
254+
}
255+
256+
if ($type === $class) {
254257
return;
255258
}
256259

Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,42 @@ public function testProcessFactoryDoesNotLoadCodeByDefault()
536536
$this->addToAssertionCount(1);
537537
}
538538

539+
public function testProcessFactoryForTypeSameAsClass()
540+
{
541+
$container = new ContainerBuilder();
542+
543+
$container->register('foo', Foo::class);
544+
$container->register('bar', 'callable')
545+
->setFactory([
546+
new Reference('foo'),
547+
'createCallable',
548+
]);
549+
$container->register('bar_call', BarMethodCall::class)
550+
->addMethodCall('setCallable', [new Reference('bar')]);
551+
552+
(new CheckTypeDeclarationsPass(true))->process($container);
553+
554+
$this->addToAssertionCount(1);
555+
}
556+
557+
public function testProcessFactoryForIterableTypeAndArrayClass()
558+
{
559+
$container = new ContainerBuilder();
560+
561+
$container->register('foo', Foo::class);
562+
$container->register('bar', 'array')
563+
->setFactory([
564+
new Reference('foo'),
565+
'createArray',
566+
]);
567+
$container->register('bar_call', BarMethodCall::class)
568+
->addMethodCall('setIterable', [new Reference('bar')]);
569+
570+
(new CheckTypeDeclarationsPass(true))->process($container);
571+
572+
$this->addToAssertionCount(1);
573+
}
574+
539575
public function testProcessPassingBuiltinTypeDoesNotLoadCodeByDefault()
540576
{
541577
$container = new ContainerBuilder();

Tests/Fixtures/CheckTypeDeclarationsPass/Foo.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ public static function createBarArguments(\stdClass $stdClass, \stdClass $stdCla
1313
{
1414
return new Bar($stdClass);
1515
}
16+
17+
public static function createCallable(): callable
18+
{
19+
return function() {};
20+
}
21+
22+
public static function createArray(): array
23+
{
24+
return [];
25+
}
1626
}

0 commit comments

Comments
 (0)