Skip to content

Commit f85a822

Browse files
committed
minor #53659 [DependencyInjection] Fix replacing arguments (HypeMC)
This PR was merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Fix replacing arguments | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Currently, `replaceArgument()` can throw an `OutOfBoundsException` for a valid index. Commits ------- b2653a8797 [DependencyInjection] Fix replacing arguments
2 parents 597a4a1 + cebc95a commit f85a822

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Definition.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ public function replaceArgument(int|string $index, mixed $argument): static
257257
throw new OutOfBoundsException(sprintf('Cannot replace arguments for class "%s" if none have been configured yet.', $this->class));
258258
}
259259

260-
if (\is_int($index) && ($index < 0 || $index > \count($this->arguments) - 1)) {
261-
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d] of the arguments of class "%s".', $index, \count($this->arguments) - 1, $this->class));
262-
}
263-
264260
if (!\array_key_exists($index, $this->arguments)) {
265261
throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist in class "%s".', $index, $this->class));
266262
}

Tests/DefinitionTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public function testReplaceArgumentShouldCheckBounds()
292292
$def->addArgument('foo');
293293

294294
$this->expectException(\OutOfBoundsException::class);
295-
$this->expectExceptionMessage('The index "1" is not in the range [0, 0] of the arguments of class "stdClass".');
295+
$this->expectExceptionMessage('The argument "1" doesn\'t exist in class "stdClass".');
296296

297297
$def->replaceArgument(1, 'bar');
298298
}
@@ -307,6 +307,17 @@ public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
307307
$def->replaceArgument(0, 'bar');
308308
}
309309

310+
public function testReplaceArgumentWithNonConsecutiveIntIndex()
311+
{
312+
$def = new Definition('stdClass');
313+
314+
$def->setArguments([1 => 'foo']);
315+
$this->assertSame([1 => 'foo'], $def->getArguments());
316+
317+
$def->replaceArgument(1, 'bar');
318+
$this->assertSame([1 => 'bar'], $def->getArguments());
319+
}
320+
310321
public function testSetGetProperties()
311322
{
312323
$def = new Definition('stdClass');

0 commit comments

Comments
 (0)