Skip to content

[make:registration-form] use UniqueEntity attribute instead of annotation #1261

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

Merged
merged 1 commit into from
Jan 3, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bundle\MakerBundle\Maker;

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

// see if it makes sense to add the UniqueEntity constraint
$userClassDetails = new ClassDetails($this->userClass);
$addAnnotation = false;
if (!$userClassDetails->doesDocBlockContainAnnotation('@UniqueEntity')) {
$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)));
$this->addUniqueEntityConstraint = false;

if (!$userClassDetails->hasAttribute(UniqueEntity::class)) {
$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)));
}
$this->addUniqueEntityConstraint = $addAnnotation;

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

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

public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(
Annotation::class,
'doctrine/annotations'
);

$dependencies->addClassDependency(
AbstractType::class,
'form'
Expand Down
17 changes: 7 additions & 10 deletions src/Util/ClassDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ public function getPath(): string
return (new \ReflectionClass($this->fullClassName))->getFileName();
}

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

if (false === $docComment) {
return false;
foreach ($reflected->getAttributes($attributeClassName) as $reflectedAttribute) {
if ($reflectedAttribute->getName() === $attributeClassName) {
return true;
}
}

return str_contains($docComment, $annotation);
return false;
}
}
34 changes: 34 additions & 0 deletions tests/Util/ClassDetailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Tests\Util;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bundle\MakerBundle\Util\ClassDetails;

/**
* @author Jesse Rushlow <[email protected]>
*/
final class ClassDetailsTest extends TestCase
{
public function testHasAttribute(): void
{
self::assertTrue((new ClassDetails(FixtureClassDetails::class))->hasAttribute(UniqueEntity::class));

self::assertFalse((new ClassDetails(__CLASS__))->hasAttribute(UniqueEntity::class));
}
}

#[UniqueEntity]
final class FixtureClassDetails
{
}