From 83a8cadf5ce55dcf3572e25b258d948aca8091a4 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 2 Dec 2023 16:20:17 +0100 Subject: [PATCH] [Validator] Only trigger deprecation when Validator annotations are used --- .../Mapping/Loader/AnnotationLoader.php | 20 +++++++++++--- ...otationLoaderWithHybridAnnotationsTest.php | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php index ebc1795f2cb58..5bb60b7b59315 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php @@ -118,13 +118,13 @@ private function getAnnotations(\ReflectionMethod|\ReflectionClass|\ReflectionPr $annotations = []; if ($reflection instanceof \ReflectionClass && $annotations = $this->reader->getClassAnnotations($reflection)) { - trigger_deprecation('symfony/validator', '6.4', 'Class "%s" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $reflection->getName()); + $this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Class "%s"', $reflection->getName())); } if ($reflection instanceof \ReflectionMethod && $annotations = $this->reader->getMethodAnnotations($reflection)) { - 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()); + $this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Method "%s::%s()"', $reflection->getDeclaringClass()->getName(), $reflection->getName())); } if ($reflection instanceof \ReflectionProperty && $annotations = $this->reader->getPropertyAnnotations($reflection)) { - 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()); + $this->triggerDeprecationIfAnnotationIsUsed($annotations, sprintf('Property "%s::$%s"', $reflection->getDeclaringClass()->getName(), $reflection->getName())); } foreach ($dedup as $annotation) { @@ -142,4 +142,18 @@ private function getAnnotations(\ReflectionMethod|\ReflectionClass|\ReflectionPr } } } + + private function triggerDeprecationIfAnnotationIsUsed(array $annotations, string $messagePrefix): void + { + foreach ($annotations as $annotation) { + if ( + $annotation instanceof Constraint + || $annotation instanceof GroupSequence + || $annotation instanceof GroupSequenceProvider + ) { + trigger_deprecation('symfony/validator', '6.4', sprintf('%s uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.', $messagePrefix)); + break; + } + } + } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderWithHybridAnnotationsTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderWithHybridAnnotationsTest.php index 7010adeecb44f..453e7b6dfa7af 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderWithHybridAnnotationsTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderWithHybridAnnotationsTest.php @@ -13,6 +13,8 @@ use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; +use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; /** @@ -46,6 +48,14 @@ public function testLoadClassMetadataAndMerge() parent::testLoadClassMetadataAndMerge(); } + public function testLoadClassMetadataWithOtherAnnotations() + { + $loader = $this->createAnnotationLoader(); + $metadata = new ClassMetadata(EntityWithOtherAnnotations::class); + + $this->assertTrue($loader->loadClassMetadata($metadata)); + } + protected function createAnnotationLoader(): AnnotationLoader { return new AnnotationLoader(new AnnotationReader()); @@ -56,3 +66,20 @@ protected function getFixtureNamespace(): string return 'Symfony\Component\Validator\Tests\Fixtures\Attribute'; } } + +/** + * @Annotation + * @Target({"PROPERTY"}) + */ +class SomeAnnotation +{ +} + +class EntityWithOtherAnnotations +{ + /** + * @SomeAnnotation + */ + #[NotBlank] + public ?string $name = null; +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy