Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Only trigger deprecation when Validator annotations are used #52867

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}