10000 bug #35225 [DependencyInjection] Handle ServiceClosureArgument for ca… · symfony/symfony@ffd5d12 · GitHub
[go: up one dir, main page]

Skip to content

Commit ffd5d12

Browse files
bug #35225 [DependencyInjection] Handle ServiceClosureArgument for callable in container linting (shieldo)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection] Handle ServiceClosureArgument for callable in container linting | Q | A | ------------- | --- | Branch? | 4.4 (+) | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | (none) | License | MIT Making use of `ServiceClosureArgument` instances in service definitions was not accounted for in container linting when a service type-hints for `callable` in an argument - adding this check ensures that `ServiceClosureArgument` instances are recognised correctly as callables (once they are resolved). Commits ------- e48829e [DependencyInjection] Handle ServiceClosureArgument for callable in container linting
2 parents b9a0b33 + e48829e commit ffd5d12

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
15+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1516
use Symfony\Component\DependencyInjection\Container;
1617
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
@@ -219,6 +220,10 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
219220
return;
220221
}
221222

223+
if (\in_array($type, ['callable', 'Closure'], true) && $value instanceof ServiceClosureArgument) {
224+
return;
225+
}
226+
222227
if ('iterable' === $type && (\is_array($value) || $value instanceof \Traversable || $value instanceof IteratorArgument)) {
223228
return;
224229
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
16+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1617
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -697,4 +698,28 @@ public function testProcessHandleClosureForCallable()
697698

698699
$this->addToAssertionCount(1);
699700
}
701+
702+
public function testProcessSuccessWhenPassingServiceClosureArgumentToCallable()
703+
{
704+
$container = new ContainerBuilder();
705+
706+
$container->register('bar', BarMethodCall::class)
707+
->addMethodCall('setCallable', [new ServiceClosureArgument(new Reference('foo'))]);
708+
709+
(new CheckTypeDeclarationsPass(true))->process($container);
710+
711+
$this->addToAssertionCount(1);
712+
}
713+
714+
public function testProcessSuccessWhenPassingServiceClosureArgumentToClosure()
715+
{
716+
$container = new ContainerBuilder();
717+
718+
$container->register('bar', BarMethodCall::class)
719+
->addMethodCall('setClosure', [new ServiceClosureArgument(new Reference('foo'))]);
720+
721+
(new CheckTypeDeclarationsPass(true))->process($container);
722+
723+
$this->addToAssertionCount(1);
724+
}
700725
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ public function setIterable(iterable $iterable)
4040
public function setCallable(callable $callable): void
4141
{
4242
}
43+
44+
public function setClosure(\Closure $closure): void
45+
{
46+
}
4347
}

0 commit comments

Comments
 (0)
0