Skip to content

Commit f39e0c9

Browse files
committed
Move array_merge calls out of loops to improve performance
1 parent f2e57d0 commit f39e0c9

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

ContainerBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,10 +1280,10 @@ public function findTags()
12801280
{
12811281
$tags = [];
12821282
foreach ($this->getDefinitions() as $id => $definition) {
1283-
$tags = array_merge(array_keys($definition->getTags()), $tags);
1283+
$tags[] = array_keys($definition->getTags());
12841284
}
12851285

1286-
return array_unique($tags);
1286+
return array_unique(array_merge([], ...$tags));
12871287
}
12881288

12891289
/**

Dumper/GraphvizDumper.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,22 @@ private function findEdges(string $id, array $arguments, bool $required, string
130130
$lazyEdge = $lazy || $this->container->getDefinition((string) $argument)->isLazy();
131131
}
132132

133-
$edges[] = ['name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge];
133+
$edges[] = [['name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge]];
134134
} elseif ($argument instanceof ArgumentInterface) {
135-
$edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true));
135+
$edges[] = $this->findEdges($id, $argument->getValues(), $required, $name, true);
136136
} elseif ($argument instanceof Definition) {
137-
$edges = array_merge($edges,
138-
$this->findEdges($id, $argument->getArguments(), $required, ''),
139-
$this->findEdges($id, $argument->getProperties(), false, '')
140-
);
137+
$edges[] = $this->findEdges($id, $argument->getArguments(), $required, '');
138+
$edges[] = $this->findEdges($id, $argument->getProperties(), false, '');
139+
141140
foreach ($argument->getMethodCalls() as $call) {
142-
$edges = array_merge($edges, $this->findEdges($id, $call[1], false, $call[0].'()'));
141+
$edges[] = $this->findEdges($id, $call[1], false, $call[0].'()');
143142
}
144143
} elseif (\is_array($argument)) {
145-
$edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
144+
$edges[] = $this->findEdges($id, $argument, $required, $name, $lazy);
146145
}
147146
}
148147

149-
return $edges;
148+
return array_merge([], ...$edges);
150149
}
151150

152151
private function findNodes(): array

Tests/ContainerBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,18 @@ public function testReferencingDeprecatedPublicService()
17531753

17541754
$this->addToAssertionCount(1);
17551755
}
1756+
1757+
public function testFindTags()
1758+
{
1759+
$container = new ContainerBuilder();
1760+
$container
1761+
->register(A::class)
1762+
->addTag('tag1')
1763+
->addTag('tag2')
1764+
->addTag('tag3');
1765+
1766+
$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
1767+
}
17561768
}
17571769

17581770
class FooClass

0 commit comments

Comments
 (0)