Skip to content

Commit 78272a3

Browse files
committed
[Validator] Only trigger deprecation when Validator annotations are used
1 parent b6ae3aa commit 78272a3

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ private function getAnnotations(\ReflectionMethod|\ReflectionClass|\ReflectionPr
118118
$annotations = [];
119119

120120
if ($reflection instanceof \ReflectionClass && $annotations = $this->reader->getClassAnnotations($reflection)) {
121-
trigger_deprecation('symfony/validator', '6.4', 'Class "%s" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $reflection->getName());
121+
$this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Class "%s"', $reflection->getName()));
122122
}
123123
if ($reflection instanceof \ReflectionMethod && $annotations = $this->reader->getMethodAnnotations($reflection)) {
124-
trigger_deprecation('symfony/validator', '6.4', 'Method "%s::%s()" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $reflection->getDeclaringClass()->getName(), $reflection->getName());
124+
$this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Method "%s::%s()"', $reflection->getDeclaringClass()->getName(), $reflection->getName()));
125125
}
126126
if ($reflection instanceof \ReflectionProperty && $annotations = $this->reader->getPropertyAnnotations($reflection)) {
127-
trigger_deprecation('symfony/validator', '6.4', 'Property "%s::$%s" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $reflection->getDeclaringClass()->getName(), $reflection->getName());
127+
$this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Property "%s::$%s"', $reflection->getDeclaringClass()->getName(), $reflection->getName()));
128128
}
129129

130130
foreach ($dedup as $annotation) {
@@ -142,4 +142,18 @@ private function getAnnotations(\ReflectionMethod|\ReflectionClass|\ReflectionPr
142142
}
143143
}
144144
}
145+
146+
private function triggerDeprecationIfAnnotationIsUsed(array $annotations, string $messagePrefix): void
147+
{
148+
foreach ($annotations as $annotation) {
149+
if (
150+
$annotation instanceof GroupSequence
151+
|| $annotation instanceof GroupSequenceProvider
152+
|| $annotation instanceof Constraint
153+
) {
154+
trigger_deprecation('symfony/validator', '6.4', sprintf('%s uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $messagePrefix));
155+
break;
156+
}
157+
}
158+
}
145159
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderWithHybridAnnotationsTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
use Symfony\Component\Validator\Constraints\NotBlank;
17+
use Symfony\Component\Validator\Mapping\ClassMetadata;
1618
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
1719

1820
/**
@@ -46,6 +48,14 @@ public function testLoadClassMetadataAndMerge()
4648
parent::testLoadClassMetadataAndMerge();
4749
}
4850

51+
public function testLoadClassMetadataWithOtherAnnotations()
52+
{
53+
$loader = $this->createAnnotationLoader();
54+
$metadata = new ClassMetadata(EntityWithOtherAnnotations::class);
55+
56+
$this->assertTrue($loader->loadClassMetadata($metadata));
57+
}
58+
4959
protected function createAnnotationLoader(): AnnotationLoader
5060
{
5161
return new AnnotationLoader(new AnnotationReader());
@@ -56,3 +66,20 @@ protected function getFixtureNamespace(): string
5666
return 'Symfony\Component\Validator\Tests\Fixtures\Attribute';
5767
}
5868
}
69+
70+
/**
71+
* @Annotation
72+
* @Target({"PROPERTY"})
73+
*/
74+
class SomeAnnotation
75+
{
76+
}
77+
78+
class EntityWithOtherAnnotations
79+
{
80+
/**
81+
* @SomeAnnotation
82+
*/
83+
#[NotBlank]
84+
public ?string $name = null;
85+
}

0 commit comments

Comments
 (0)