Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 495e11b

Browse filesBrowse files
more verbose deprecation message, add legacy tests
1 parent 7c0b7ba commit 495e11b
Copy full SHA for 495e11b

13 files changed

+369
-36
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredMethodsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredMethodsPass.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
5757
}
5858
if (false !== $doc = $r->getDocComment()) {
5959
if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
60-
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
60+
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
6161

6262
if ($this->isWither($reflectionMethod, $doc)) {
6363
$withers[] = [$reflectionMethod->name, [], true];

‎src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
4040
if (!($type = $reflectionProperty->getType()) instanceof \ReflectionNamedType) {
4141
continue;
4242
}
43+
$doc = false;
4344
if (!$reflectionProperty->getAttributes(Required::class)
4445
&& ((false === $doc = $reflectionProperty->getDocComment()) || false === stripos($doc, '@required') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc))
4546
) {
4647
continue;
4748
}
48-
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
49+
// Trigger a deprecation only when PHPDoc has been found.
50+
if ($doc) {
51+
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead. It was found on property "%s::$%s".', $value::class, $reflectionProperty->getName());
52+
}
4953
if (\array_key_exists($name = $reflectionProperty->getName(), $properties)) {
5054
continue;
5155
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+28-9Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,13 @@ public function testOptionalArgsNoRequiredForCoreClasses()
698698
);
699699
}
700700

701-
public function testSetterInjection()
701+
/**
702+
* @group legacy
703+
*/
704+
public function testSetterInjectionAnnotation()
702705
{
706+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
707+
703708
$container = new ContainerBuilder();
704709
$container->register(Foo::class);
705710
$container->register(A::class);
@@ -708,7 +713,7 @@ public function testSetterInjection()
708713

709714
// manually configure *one* call, to override autowiring
710715
$container
711-
->register('setter_injection', SetterInjection::class)
716+
->register('setter_injection', SetterInjectionAnnotation::class)
712717
->setAutowired(true)
713718
->addMethodCall('setWithCallsConfigured', ['manual_arg1', 'manual_arg2'])
714719
;
@@ -720,7 +725,7 @@ public function testSetterInjection()
720725
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
721726

722727
$this->assertEquals(
723-
['setWithCallsConfigured', 'setFoo', 'setDependencies', 'setChildMethodWithoutDocBlock'],
728+
['setWithCallsConfigured', 'setFoo', 'setChildMethodWithoutDocBlock', 'setDependencies'],
724729
array_column($methodCalls, 0)
725730
);
726731

@@ -769,13 +774,8 @@ public function testWithNonExistingSetterAndAutowiring()
769774
(new AutowirePass())->process($container);
770775
}
771776

772-
/**
773-
* @group legacy
774-
*/
775-
public function testExplicitMethodInjection()
777+
public function testExplicitMethodInjectionAttribute()
776778
{
777-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
778-
779779
$container = new ContainerBuilder();
780780
$container->register(Foo::class);
781781
$container->register(A::class);
@@ -829,8 +829,13 @@ public function testIgnoreServiceWithClassNotExisting()
829829
$this->assertTrue($container->hasDefinition('bar'));
830830
}
831831

832+
/**
833+
* @group legacy
834+
*/
832835
public function testSetterInjectionCollisionThrowsException()
833836
{
837+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
838+
834839
$container = new ContainerBuilder();
835840

836841
$container->register('c1', CollisionA::class);
@@ -921,9 +926,13 @@ public function testWithFactory()
921926

922927
/**
923928
* @dataProvider provideNotWireableCalls
929+
*
930+
* @group legacy
924931
*/
925932
public function testNotWireableCalls($method, $expectedMsg)
926933
{
934+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
935+
927936
$container = new ContainerBuilder();
928937

929938
$foo = $container->register('foo', NotWireable::class)->setAutowired(true)
@@ -956,8 +965,13 @@ public function provideNotWireableCalls()
956965
];
957966
}
958967

968+
/**
969+
* @group legacy
970+
*/
959971
public function testSuggestRegisteredServicesWithSimilarCase()
960972
{
973+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
974+
961975
$container = new ContainerBuilder();
962976

963977
$container->register(LesTilleuls::class, LesTilleuls::class);
@@ -1137,8 +1151,13 @@ public function testErroredServiceLocator()
11371151
$this->assertSame(['Cannot autowire service "some_locator": it has type "Symfony\Component\DependencyInjection\Tests\Compiler\MissingClass" but this class was not found.'], $container->getDefinition('.errored.some_locator.'.MissingClass::class)->getErrors());
11381152
}
11391153

1154+
/**
1155+
* @group legacy
1156+
*/
11401157
public function testNamedArgumentAliasResolveCollisions()
11411158
{
1159+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1160+
11421161
$container = new ContainerBuilder();
11431162

11441163
$container->register('c1', CollisionA::class);

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowireRequiredMethodsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowireRequiredMethodsPassTest.php
+71-8Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1717
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType;
1920
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
2021

2122
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
@@ -24,17 +25,22 @@ class AutowireRequiredMethodsPassTest extends TestCase
2425
{
2526
use ExpectDeprecationTrait;
2627

27-
public function testSetterInjection()
28+
/**
29+
* @group legacy
30+
*/
31+
public function testSetterInjectionAnnotation()
2832
{
33+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
34+
2935
$container = new ContainerBuilder();
30-
$container->register(Foo::class);
36+
$container->register(FooAnnotation::class);
3137
$container->register(A::class);
3238
$container->register(CollisionA::class);
3339
$container->register(CollisionB::class);
3440

3541
// manually configure *one* call, to override autowiring
3642
$container
37-
->register('setter_injection', SetterInjection::class)
43+
->register('setter_injection', SetterInjectionAnnotation::class)
3844
->setAutowired(true)
3945
->addMethodCall('setWithCallsConfigured', ['manual_arg1', 'manual_arg2']);
4046

@@ -44,7 +50,7 @@ public function testSetterInjection()
4450
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
4551

4652
$this->assertEquals(
47-
['setWithCallsConfigured', 'setFoo', 'setDependencies', 'setChildMethodWithoutDocBlock'],
53+
['setWithCallsConfigured', 'setFoo', 'setChildMethodWithoutDocBlock', 'setDependencies'],
4854
array_column($methodCalls, 0)
4955
);
5056

@@ -76,10 +82,36 @@ public function testSetterInjectionWithAttribute()
7682
/**
7783
* @group legacy
7884
*/
79-
public function testExplicitMethodInjection()
85+
// @deprecated since Symfony 6.3, to be removed in 7.0
86+
public function testExplicitMethodInjectionAnnotation()
8087
{
81-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
88+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
89+
90+
$container = new ContainerBuilder();
91+
$container->register(FooAnnotation::class);
92+
$container->register(A::class);
93+
$container->register(CollisionA::class);
94+
$container->register(CollisionB::class);
8295

96+
$container
97+
->register('setter_injection', SetterInjectionAnnotation::class)
98+
->setAutowired(true)
99+
->addMethodCall('notASetter', []);
100+
101+
(new ResolveClassPass())->process($container);
102+
(new AutowireRequiredMethodsPass())->process($container);
103+
104+
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
105+
106+
$this->assertEquals(
107+
['notASetter', 'setFoo', 'setChildMethodWithoutDocBlock', 'setDependencies', 'setWithCallsConfigured'],
108+
array_column($methodCalls, 0)
109+
);
110+
$this->assertEquals([], $methodCalls[0][1]);
111+
}
112+
113+
public function testExplicitMethodInjectionAttribute()
114+
{
83115
$container = new ContainerBuilder();
84116
$container->register(Foo::class);
85117
$container->register(A::class);
@@ -103,13 +135,18 @@ public function testExplicitMethodInjection()
103135
$this->assertEquals([], $methodCalls[0][1]);
104136
}
105137

138+
/**
139+
* @group legacy
140+
*/
106141
public function testWitherInjection()
107142
{
143+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
144+
108145
$container = new ContainerBuilder();
109-
$container->register(Foo::class);
146+
$container->register(FooAnnotation::class);
110147

111148
$container
112-
->register('wither', Wither::class)
149+
->register('wither', WitherAnnotation::class)
113150
->setAutowired(true);
114151

115152
(new ResolveClassPass())->process($container);
@@ -125,6 +162,32 @@ public function testWitherInjection()
125162
$this->assertSame($expected, $methodCalls);
126163
}
127164

165+
/**
166+
* @group legacy
167+
*/
168+
public function testWitherAnnotationWithStaticReturnTypeInjection()
169+
{
170+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
171+
172+
$container = new ContainerBuilder();
173+
$container->register(FooAnnotation::class);
174+
175+
$container
176+
->register('wither', WitherAnnotationStaticReturnType::class)
177+
->setAutowired(true);
178+
179+
(new ResolveClassPass())->process($container);
180+
(new AutowireRequiredMethodsPass())->process($container);
181+
182+
$methodCalls = $container->getDefinition('wither')->getMethodCalls();
183+
184+
$expected = [
185+
['withFoo', [], true],
186+
['setFoo', []],
187+
];
188+
$this->assertSame($expected, $methodCalls);
189+
}
190+
128191
public function testWitherWithStaticReturnTypeInjection()
129192
{
130193
$container = new ContainerBuilder();

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowireRequiredPropertiesPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowireRequiredPropertiesPassTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredPropertiesPass;
1617
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -21,8 +22,15 @@
2122

2223
class AutowireRequiredPropertiesPassTest extends TestCase
2324
{
25+
use ExpectDeprecationTrait;
26+
27+
/**
28+
* @group legacy
29+
*/
2430
public function testInjection()
2531
{
32+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead. It was found on property "Symfony\Component\DependencyInjection\Definition::$plop".');
33+
2634
$container = new ContainerBuilder();
2735
$container->register(Bar::class);
2836
$container->register(A::class);

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1617
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1718
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -37,6 +38,8 @@
3738

3839
class ResolveBindingsPassTest extends TestCase
3940
{
41+
use ExpectDeprecationTrait;
42+
4043
public function testProcess()
4144
{
4245
$container = new ContainerBuilder();
@@ -143,8 +146,13 @@ public function testTypedReferenceSupport()
143146
$this->assertEquals([new Reference('bar')], $container->getDefinition('def3')->getArguments());
144147
}
145148

149+
/**
150+
* @group legacy
151+
*/
146152
public function testScalarSetter()
147153
{
154+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
155+
148156
$container = new ContainerBuilder();
149157

150158
$definition = $container->autowire('foo', ScalarSetter::class);

‎src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
use Symfony\Component\DependencyInjection\Reference;
4949
use Symfony\Component\DependencyInjection\ServiceLocator;
5050
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
51+
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
5152
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
5253
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
5354
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
5455
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
5556
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
5657
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
5758
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;
59+
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType;
5860
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
5961
use Symfony\Component\DependencyInjection\TypedReference;
6062
use Symfony\Component\ExpressionLanguage\Expression;
@@ -1831,6 +1833,27 @@ public function testLazyWither()
18311833
$this->assertInstanceOf(Wither::class, $wither->withFoo1($wither->foo));
18321834
}
18331835

1836+
/**
1837+
* @group legacy
1838+
*/
1839+
public function testWitherAnnotationWithStaticReturnType()
1840+
{
1841+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1842+
1843+
$container = new ContainerBuilder();
1844+
$container->register(FooAnnotation::class);
1845+
1846+
$container
1847+
->register('wither', WitherAnnotationStaticReturnType::class)
1848+
->setPublic(true)
1849+
->setAutowired(true);
1850+
1851+
$container->compile();
1852+
1853+
$wither = $container->get('wither');
1854+
$this->assertInstanceOf(FooAnnotation::class, $wither->foo);
1855+
}
1856+
18341857
public function testWitherWithStaticReturnType()
18351858
{
18361859
$container = new ContainerBuilder();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.