Skip to content

Commit 20103a8

Browse files
bug #62485 [DependencyInjection] Fix loop corruption in CheckTypeDeclarationsPass (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Fix loop corruption in `CheckTypeDeclarationsPass` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT In `CheckTypeDeclarationsPass`, the loop counter `$i` was incorrectly overwritten with the parameter name when processing **named arguments**. Comparing a string to an integer [here](https://github.com/yoeunes/symfony/blob/3866ff11d86331d1acf49043f5c86b2a054db60f/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php#L140) (e.g. `'b' < 2`) evaluates to `false`. This caused the loop to terminate early, skipping type validation for all subsequent arguments. Commits ------- 3866ff1 [DependencyInjection] Fix loop corruption in CheckTypeDeclarationsPass
2 parents 8af1cb8 + 3866ff1 commit 20103a8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
142142
if (!$p->hasType() || $p->isVariadic()) {
143143
continue;
144144
}
145+
$key = $i;
145146
if (\array_key_exists($p->name, $values)) {
146-
$i = $p->name;
147+
$key = $p->name;
147148
} elseif (!\array_key_exists($i, $values)) {
148149
continue;
149150
}
150151

151-
$this->checkType($checkedDefinition, $values[$i], $p, $envPlaceholderUniquePrefix);
152+
$this->checkType($checkedDefinition, $values[$key], $p, $envPlaceholderUniquePrefix);
152153
}
153154

154155
if ($reflectionFunction->isVariadic() && ($lastParameter = end($reflectionParameters))->hasType()) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,18 @@ public function testErroredDefinitionsAreNotChecked()
10231023

10241024
$this->addToAssertionCount(1);
10251025
}
1026+
1027+
public function testCheckTypeDeclarationsSkipsSubsequentNamedArguments()
1028+
{
1029+
$container = new ContainerBuilder();
1030+
$container->register('service', ServiceWithTwoInts::class)
1031+
->setArguments(['a' => 1, 'b' => []]);
1032+
1033+
$this->expectException(InvalidParameterTypeException::class);
1034+
$this->expectExceptionMessage('argument 2 of "Symfony\Component\DependencyInjection\Tests\Compiler\ServiceWithTwoInts::__construct()" accepts "int", "array" passed');
1035+
1036+
(new CheckTypeDeclarationsPass(true))->process($container);
1037+
}
10261038
}
10271039

10281040
class CallableClass
@@ -1038,3 +1050,10 @@ public static function __callStatic($name, $arguments)
10381050
{
10391051
}
10401052
}
1053+
1054+
class ServiceWithTwoInts
1055+
{
1056+
public function __construct(int $a, int $b)
1057+
{
1058+
}
1059+
}

0 commit comments

Comments
 (0)