Skip to content

Commit

Permalink
bug #52867 [Validator] Only trigger deprecation when Validator annota…
Browse files Browse the repository at this point in the history
…tions are used (HypeMC)

This PR was merged into the 6.4 branch.

Discussion
----------

[Validator] Only trigger deprecation when Validator annotations are used

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52828
| License       | MIT

The deprecations are now triggered only if one of the annotations is from the validator component.

Commits
-------

83a8cad [Validator] Only trigger deprecation when Validator annotations are used
  • Loading branch information
derrabus committed Dec 3, 2023
2 parents b6ae3aa + 83a8cad commit 4b16f3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
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;
}

0 comments on commit 4b16f3a

Please sign in to comment.