Skip to content

Commit 25b78e2

Browse files
committed
[make:registration] use UniqueEntity attribute instead of annotation
1 parent d6eb1d8 commit 25b78e2

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\MakerBundle\Maker;
1313

1414
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
15-
use Doctrine\Common\Annotations\Annotation;
1615
use Doctrine\ORM\EntityManagerInterface;
1716
use Doctrine\ORM\Mapping\Column;
1817
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@@ -143,11 +142,11 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
143142

144143
// see if it makes sense to add the UniqueEntity constraint
145144
$userClassDetails = new ClassDetails($this->userClass);
146-
$addAnnotation = false;
147-
if (!$userClassDetails->doesDocBlockContainAnnotation('@UniqueEntity')) {
148-
$addAnnotation = $io->confirm(sprintf('Do you want to add a <comment>@UniqueEntity</comment> validation annotation on your <comment>%s</comment> class to make sure duplicate accounts aren\'t created?', Str::getShortClassName($this->userClass)));
145+
$this->addUniqueEntityConstraint = false;
146+
147+
if (!$userClassDetails->hasAttribute(UniqueEntity::class)) {
148+
$this->addUniqueEntityConstraint = $io->confirm(sprintf('Do you want to add a <comment>#[UniqueEntity]</comment> validation attribute to your <comment>%s</comment> class to make sure duplicate accounts aren\'t created?', Str::getShortClassName($this->userClass)));
149149
}
150-
$this->addUniqueEntityConstraint = $addAnnotation;
151150

152151
$this->willVerifyEmail = $io->confirm('Do you want to send an email to verify the user\'s email address after registration?', true);
153152

@@ -488,11 +487,6 @@ private function getMissingComponentsComposerMessage(): ?string
488487

489488
public function configureDependencies(DependencyBuilder $dependencies): void
490489
{
491-
$dependencies->addClassDependency(
492-
Annotation::class,
493-
'doctrine/annotations'
494-
);
495-
496490
$dependencies->addClassDependency(
497491
AbstractType::class,
498492
'form'

src/Util/ClassDetails.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,16 @@ public function getPath(): string
5757
return (new \ReflectionClass($this->fullClassName))->getFileName();
5858
}
5959

60-
/**
61-
* An imperfect, but simple way to check for the presence of an annotation.
62-
*
63-
* @param string $annotation The annotation - e.g. @UniqueEntity
64-
*/
65-
public function doesDocBlockContainAnnotation(string $annotation): bool
60+
public function hasAttribute(string $attributeClassName): bool
6661
{
67-
$docComment = (new \ReflectionClass($this->fullClassName))->getDocComment();
62+
$reflected = new \ReflectionClass($this->fullClassName);
6863

69-
if (false === $docComment) {
70-
return false;
64+
foreach ($reflected->getAttributes($attributeClassName) as $reflectedAttribute) {
65+
if ($reflectedAttribute->getName() === $attributeClassName) {
66+
return true;
67+
}
7168
}
7269

73-
return str_contains($docComment, $annotation);
70+
return false;
7471
}
7572
}

tests/Util/ClassDetailsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Tests\Util;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
16+
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
17+
18+
/**
19+
* @author Jesse Rushlow <[email protected]>
20+
*/
21+
final class ClassDetailsTest extends TestCase
22+
{
23+
public function testHasAttribute(): void
24+
{
25+
self::assertTrue((new ClassDetails(FixtureClassDetails::class))->hasAttribute(UniqueEntity::class));
26+
27+
self::assertFalse((new ClassDetails(__CLASS__))->hasAttribute(UniqueEntity::class));
28+
}
29+
}
30+
31+
#[UniqueEntity]
32+
final class FixtureClassDetails
33+
{
34+
}

0 commit comments

Comments
 (0)