Skip to content

Commit c8de3de

Browse files
minor symfony#61616 [DependencyInjection] Minor optims (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [DependencyInjection] Minor optims | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Found while reading the code once again :) Commits ------- 5eeafbb [DependencyInjection] Minor optims
2 parents 690ab9a + 5eeafbb commit c8de3de

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,16 +348,10 @@ public function addObjectResource(object|string $object): static
348348
if (\is_object($object)) {
349349
$object = $object::class;
350350
}
351-
if (!isset($this->classReflectors[$object])) {
352-
$this->classReflectors[$object] = new \ReflectionClass($object);
353-
}
354-
$class = $this->classReflectors[$object];
351+
$class = $this->classReflectors[$object] ??= new \ReflectionClass($object);
355352

356353
foreach ($class->getInterfaceNames() as $name) {
357-
if (null === $interface = &$this->classReflectors[$name]) {
358-
$interface = new \ReflectionClass($name);
359-
}
360-
$file = $interface->getFileName();
354+
$file = ($this->classReflectors[$name] ??= new \ReflectionClass($name))->getFileName();
361355
if (false !== $file && file_exists($file)) {
362356
$this->fileExists($file);
363357
}
@@ -1172,14 +1166,14 @@ private function createService(Definition $definition, array &$inlineServices, b
11721166
$service = $factory(...$arguments);
11731167

11741168
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
1175-
$r = new \ReflectionClass($factory[0]);
1169+
$r = $this->classReflectors[$factory[0]] ??= new \ReflectionClass($factory[0]);
11761170

1177-
if (0 < strpos($r->getDocComment() ?: '', "\n * @deprecated ")) {
1171+
if (str_contains($r->getDocComment() ?: '', "\n * @deprecated ")) {
11781172
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name);
11791173
}
11801174
}
11811175
} else {
1182-
$r = new \ReflectionClass($class);
1176+
$r = $this->classReflectors[$class] ??= new \ReflectionClass($class);
11831177

11841178
if (\is_object($tryProxy)) {
11851179
if ($r->getConstructor()) {
@@ -1191,7 +1185,7 @@ private function createService(Definition $definition, array &$inlineServices, b
11911185
$service = $r->getConstructor() ? $r->newInstanceArgs($arguments) : $r->newInstance();
11921186
}
11931187

1194-
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment() ?: '', "\n * @deprecated ")) {
1188+
if (!$definition->isDeprecated() && str_contains($r->getDocComment() ?: '', "\n * @deprecated ")) {
11951189
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name);
11961190
}
11971191
}

0 commit comments

Comments
 (0)