Skip to content

Use isset() instead of array_key_exists() in DIC #8907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ public function has($id)
{
$id = strtolower($id);

return array_key_exists($id, $this->services)
|| array_key_exists($id, $this->aliases)
return isset($this->services[$id])
|| array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')
;
}
Expand All @@ -267,16 +268,21 @@ public function has($id)
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
$id = strtolower($id);

// resolve aliases
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}

// re-use shared service instance if it exists
if (array_key_exists($id, $this->services)) {
return $this->services[$id];
// Attempt to retrieve the service by checking first aliases then
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
// calling strotolower() unless necessary.
foreach (array(false, true) as $strtolower) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does flatten code would be more efficient than this loop ?

if ($strtolower) {
$id = strtolower($id);
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
return $this->services[$id];
}
}

if (isset($this->loading[$id])) {
Expand Down Expand Up @@ -339,7 +345,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
*/
public function initialized($id)
{
return array_key_exists(strtolower($id), $this->services);
$id = strtolower($id);
return isset($this->services[$id]) || array_key_exists($id, $this->services);
}

/**
Expand Down