Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 461b475

Browse files
committed
Replace plus operator with array_merge
1 parent b52c0e5 commit 461b475

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
300300
};
301301

302302
if ($checkAttributes) {
303-
$autowire = $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF);
304-
$lazy = $parameter->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF);
303+
$attributes = \array_merge($parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF), $parameter->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF));
305304

306-
if (\count($autowire) > 0 && \count($lazy) > 0) {
307-
throw new AutowiringFailedException($this->currentId, "'Lazy' and 'Autowire' attributes cannot be used on the same argument.");
305+
if (1 < \count($attributes)) {
306+
throw new AutowiringFailedException($this->currentId, 'Using both attributes #[Lazy] and #[Autowire] on an argument is not allowed; use the "lazy" parameter of #[Autowire] instead.');
308307
}
309308

310-
$attributes = $autowire + $lazy;
311309
foreach ($attributes as $attribute) {
312310
$attribute = $attribute->newInstance();
313311
$invalidBehavior = $parameter->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE;

src/Symfony/Component/DependencyInjection/Compiler/RegisterAutoconfigureAttributesPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function accept(Definition $definition): bool
4343

4444
public function processClass(ContainerBuilder $container, \ReflectionClass $class): void
4545
{
46-
$attributes = $class->getAttributes(Autoconfigure::class, \ReflectionAttribute::IS_INSTANCEOF) + $class->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF);
46+
$attributes = array_merge($class->getAttributes(Autoconfigure::class, \ReflectionAttribute::IS_INSTANCEOF), $class->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF));
4747
foreach ($attributes as $attribute) {
4848
self::registerForAutoconfiguration($container, $class, $attribute);
4949
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ public function testLazyNotCompatibleWithAutowire()
13901390
try {
13911391
(new AutowirePass())->process($container);
13921392
} catch (AutowiringFailedException $e) {
1393-
$this->assertSame("'Lazy' and 'Autowire' attributes cannot be used on the same argument.", $e->getMessage());
1393+
$this->assertSame('Using both attributes #[Lazy] and #[Autowire] on an argument is not allowed; use the "lazy" parameter of #[Autowire] instead.', $e->getMessage());
13941394
}
13951395
}
13961396
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterAutoconfigureAttributesPassTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureAttributed;
2121
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredInterface;
22+
use Symfony\Component\DependencyInjection\Tests\Fixtures\LazyAutoconfigured;
2223
use Symfony\Component\DependencyInjection\Tests\Fixtures\LazyLoaded;
2324
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
2425
use Symfony\Component\DependencyInjection\Tests\Fixtures\StaticConstructorAutoconfigure;
@@ -119,4 +120,18 @@ public function testLazyServiceAttribute()
119120
;
120121
$this->assertEquals([LazyLoaded::class => $expected], $container->getAutoconfiguredInstanceof());
121122
}
123+
124+
public function testLazyAutoconfigureServiceAttribute()
125+
{
126+
$container = new ContainerBuilder();
127+
$container->register('foo', LazyAutoconfigured::class)
128+
->setAutoconfigured(true);
129+
130+
(new RegisterAutoconfigureAttributesPass())->process($container);
131+
132+
$expected = (new ChildDefinition(''))
133+
->setLazy(true)
134+
;
135+
$this->assertEquals([LazyAutoconfigured::class => $expected], $container->getAutoconfiguredInstanceof());
136+
}
122137
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
use Symfony\Component\DependencyInjection\Attribute\Lazy;
7+
8+
#[Lazy, Autoconfigure(lazy: true)]
9+
class LazyAutoconfigured
10+
{
11+
12+
}

0 commit comments

Comments
 (0)