Skip to content

Commit 65bf250

Browse files
committed
minor #14470 [DependencyInjection] Removed extra strtolower calls (dosten)
This PR was squashed before being merged into the 2.3 branch (closes #14470). Discussion ---------- [DependencyInjection] Removed extra strtolower calls | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - `Alias` already lowercase the `$id` in the constructor. Using `ContainerBuilder::hasAlias()` and `ContainerBuilder::hasDefinition()` inside the code makes an extra strtolower call. Commits ------- 3bfbf45 [DependencyInjection] Removed extra strtolower calls
2 parents bf3c924 + ae51d22 commit 65bf250

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

ContainerBuilder.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
470470
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id));
471471
}
472472

473-
if (!$this->hasDefinition($id) && isset($this->aliasDefinitions[$id])) {
473+
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
474474
return $this->get($this->aliasDefinitions[$id]);
475475
}
476476

@@ -686,7 +686,7 @@ public function setAlias($alias, $id)
686686
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
687687
}
688688

689-
if ($alias === strtolower($id)) {
689+
if ($alias === (string) $id) {
690690
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
691691
}
692692

@@ -748,7 +748,7 @@ public function getAlias($id)
748748
{
749749
$id = strtolower($id);
750750

751-
if (!$this->hasAlias($id)) {
751+
if (!isset($this->aliasDefinitions[$id])) {
752752
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
753753
}
754754

@@ -866,7 +866,7 @@ public function getDefinition($id)
866866
{
867867
$id = strtolower($id);
868868

869-
if (!$this->hasDefinition($id)) {
869+
if (!array_key_exists($id, $this->definitions)) {
870870
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
871871
}
872872

@@ -888,8 +888,10 @@ public function getDefinition($id)
888888
*/
889889
public function findDefinition($id)
890890
{
891-
while ($this->hasAlias($id)) {
892-
$id = (string) $this->getAlias($id);
891+
$id = strtolower($id);
892+
893+
while (isset($this->aliasDefinitions[$id])) {
894+
$id = (string) $this->aliasDefinitions[$id];
893895
}
894896

895897
return $this->getDefinition($id);

Tests/ContainerBuilderTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ public function testAliases()
189189
$this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
190190
$this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
191191

192+
try {
193+
$builder->setAlias('foobar', 'foobar');
194+
$this->fail('->setAlias() throws an InvalidArgumentException if the alias references itself');
195+
} catch (\InvalidArgumentException $e) {
196+
$this->assertEquals('An alias can not reference itself, got a circular reference on "foobar".', $e->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself');
197+
}
198+
192199
try {
193200
$builder->getAlias('foobar');
194201
$this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');

0 commit comments

Comments
 (0)