8000 more verbose deprecation message, add legacy tests · symfony/symfony@9cb1ec0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cb1ec0

Browse files
more verbose deprecation message, add legacy tests
1 parent 7c0b7ba commit 9cb1ec0

13 files changed

+293
-18
lines changed

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

Lines 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

Lines 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 = null;
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 "%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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,13 @@ public function testOptionalArgsNoRequiredForCoreClasses()
698698
);
699699
}
700700

701+
/**
702+
* @group legacy
703+
*/
701704
public function testSetterInjection()
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);
@@ -774,7 +779,7 @@ public function testWithNonExistingSetterAndAutowiring()
774779
*/
775780
public function testExplicitMethodInjection()
776781
{
777-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
782+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
778783

779784
$container = new ContainerBuilder();
780785
$container->register(Foo::class);
@@ -829,8 +834,13 @@ public function testIgnoreServiceWithClassNotExisting()
829834
$this->assertTrue($container->hasDefinition('bar'));
830835
}
831836

837+
/**
838+
* @group legacy
839+
*/
832840
public function testSetterInjectionCollisionThrowsException()
833841
{
842+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
843+
834844
$container = new ContainerBuilder();
835845

836846
$container->register('c1', CollisionA::class);
@@ -921,9 +931,13 @@ public function testWithFactory()
921931

922932
/**
923933
* @dataProvider provideNotWireableCalls
934+
*
935+
* @group legacy
924936
*/
925937
public function testNotWireableCalls($method, $expectedMsg)
926938
{
939+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
940+
927941
$container = new ContainerBuilder();
928942

929943
$foo = $container->register('foo', NotWireable::class)->setAutowired(true)
@@ -956,8 +970,13 @@ public function provideNotWireableCalls()
956970
];
957971
}
958972

973+
/**
974+
* @group legacy
975+
*/
959976
public function testSuggestRegisteredServicesWithSimilarCase()
960977
{
978+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
979+
961980
$container = new ContainerBuilder();
962981

963982
$container->register(LesTilleuls::class, LesTilleuls::class);
@@ -1137,8 +1156,13 @@ public function testErroredServiceLocator()
11371156
$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());
11381157
}
11391158

1159+
/**
1160+
* @group legacy
1161+
*/
11401162
public function testNamedArgumentAliasResolveCollisions()
11411163
{
1164+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1165+
11421166
$container = new ContainerBuilder();
11431167

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

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

Lines changed: 43 additions & 6 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,10 +25,15 @@ class AutowireRequiredMethodsPassTest extends TestCase
2425
{
2526
use ExpectDeprecationTrait;
2627

28+
/**
29+
* @group legacy
30+
*/
2731
public function testSetterInjection()
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);
@@ -78,10 +84,10 @@ public function testSetterInjectionWithAttribute()
7884
*/
7985
public function testExplicitMethodInjection()
8086
{
81-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
87+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
8288

8389
$container = new ContainerBuilder();
84-
$container->register(Foo::class);
90+
$container->register(FooAnnotation::class);
8591
$container->register(A::class);
8692
$container->register(CollisionA::class);
8793
$container->register(CollisionB::class);
@@ -103,13 +109,18 @@ public function testExplicitMethodInjection()
103109
$this->assertEquals([], $methodCalls[0][1]);
104110
}
105111

112+
/**
113+
* @group legacy
114+
*/
106115
public function testWitherInjection()
107116
{
117+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
118+
108119
$container = new ContainerBuilder();
109-
$container->register(Foo::class);
120+
$container->register(FooAnnotation::class);
110121

111122
$container
112-
->register('wither', Wither::class)
123+
->register('wither', WitherAnnotation::class)
113124
->setAutowired(true);
114125

115126
(new ResolveClassPass())->process($container);
@@ -125,6 +136,32 @@ public function testWitherInjection()
125136
$this->assertSame($expected, $methodCalls);
126137
}
127138

139+
/**
140+
* @group legacy
141+
*/
142+
public function testWitherAnnotationWithStaticReturnTypeInjection()
143+
{
144+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
145+
146+
$container = new ContainerBuilder();
147+
$container->register(FooAnnotation::class);
148+
149+
$container
150+
->register('wither', WitherAnnotationStaticReturnType::class)
151+
->setAutowired(true);
152+
153+
(new ResolveClassPass())->process($container);
154+
(new AutowireRequiredMethodsPass())->process($container);
155+
156+
$methodCalls = $container->getDefinition('wither')->getMethodCalls();
157+
158+
$expected = [
159+
['withFoo', [], true],
160+
['setFoo', []],
161+
];
162+
$this->assertSame($expected, $methodCalls);
163+
}
164+
128165
public function testWitherWithStaticReturnTypeInjection()
129166
{
130167
$container = new ContainerBuilder();
@@ -149,7 +186,7 @@ public function testWitherWithStaticReturnTypeInjection()
149186
public function testWitherInjectionWithAttribute()
150187
{
151188
$container = new ContainerBuilder();
152-
$container->register(Foo::class);
189+
$container->register(FooAnnotation::class);
153190

154191
$container
155192
->register('wither', AutowireWither::class)

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

Lines 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 "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

Lines 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

Lines 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();

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
use Symfony\Component\DependencyInjection\Reference;
4242
use Symfony\Component\DependencyInjection\ServiceLocator;
4343
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
44+
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
4445
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
46+
use Symfony\Component\DependencyInjection\Tests\Compiler\WitherAnnotation;
4547
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
4648
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
4749
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
@@ -1434,7 +1436,35 @@ public function testAliasCanBeFoundInTheDumpedContainerWhenBothTheAliasAndTheSer
14341436
$this->assertContains('bar', $service_ids);
14351437
}
14361438

1437-
public function testWither()
1439+
/**
1440+
* @group legacy
1441+
*/
1442+
public function testWitherAnnotation()
1443+
{
1444+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1445+
1446+
$container = new ContainerBuilder();
1447+
$container->register(FooAnnotation::class)
1448+
->setAutowired(true);
1449+
1450+
$container
1451+
->register('wither', WitherAnnotation::class)
1452+
->setPublic(true)
1453+
->setAutowired(true);
1454+
1455+
$container->compile();
1456+
$dumper = new PhpDumper($container);
1457+
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Annotation']);
1458+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_annotation.php', $dump);
1459+
eval('?>'.$dump);
1460+
1461+
$container = new \Symfony_DI_PhpDumper_Service_Wither_Annotation();
1462+
1463+
$wither = $container->get('wither');
1464+
$this->assertInstanceOf(FooAnnotation::class, $wither->foo);
1465+
}
1466+
1467+
public function testWitherAttribute()
14381468
{
14391469
$container = new ContainerBuilder();
14401470
$container->register(Foo::class)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
6+
7+
class WitherAnnotationStaticReturnType
8+
{
9+
public $foo;
10+
11+
/**
12+
* @required
13+
*/
14+
public function withFoo(FooAnnotation $foo): static
15+
{
16+
$new = clone $this;
17+
$new->foo = $foo;
18+
19+
return $new;
20+
}
21+
22+
/**
23+
* @required
24+
* @return $this
25+
*/
26+
public function setFoo(FooAnnotation $foo): static
27+
{
28+
$this->foo = $foo;
29+
30+
return $this;
31+
}
32+
}

0 commit comments

Comments
 (0)
0