diff --git a/src/Util/ClassSource/Model/ClassData.php b/src/Util/ClassSource/Model/ClassData.php index 4ce0cd605..2e59c89c3 100644 --- a/src/Util/ClassSource/Model/ClassData.php +++ b/src/Util/ClassSource/Model/ClassData.php @@ -30,13 +30,14 @@ private function __construct( private bool $isFinal = true, private string $rootNamespace = 'App', private ?string $classSuffix = null, + public readonly ?string $implements = null, ) { if (str_starts_with(haystack: $this->namespace, needle: $this->rootNamespace)) { $this->namespace = substr_replace(string: $this->namespace, replace: '', offset: 0, length: \strlen($this->rootNamespace) + 1); } } - public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = []): self + public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = [], ?string $implements = null): self { $className = Str::getShortClassName($class); @@ -50,6 +51,10 @@ public static function create(string $class, ?string $suffix = null, ?string $ex $useStatements->addUseStatement($extendsClass); } + if ($implements) { + $useStatements->addUseStatement($implements); + } + return new self( className: Str::asClassName($className), namespace: Str::getNamespace($class), @@ -57,6 +62,7 @@ className: Str::asClassName($className), isEntity: $isEntity, useStatementGenerator: $useStatements, classSuffix: $suffix, + implements: null === $implements ? null : Str::getShortClassName($implements), ); } @@ -130,10 +136,17 @@ public function getClassDeclaration(): string $extendsDeclaration = \sprintf(' extends %s', $this->extends); } + $implementsDeclaration = ''; + + if (null !== $this->implements) { + $implementsDeclaration = \sprintf(' implements %s', $this->implements); + } + return \sprintf('%sclass %s%s', $this->isFinal ? 'final ' : '', $this->className, $extendsDeclaration, + $implementsDeclaration ); } diff --git a/tests/Util/ClassSource/ClassDataTest.php b/tests/Util/ClassSource/ClassDataTest.php index 27d345028..f39bd6cbf 100644 --- a/tests/Util/ClassSource/ClassDataTest.php +++ b/tests/Util/ClassSource/ClassDataTest.php @@ -12,7 +12,11 @@ namespace Symfony\Bundle\MakerBundle\Tests\Util\ClassSource; use PHPUnit\Framework\TestCase; +use Symfony\Bundle\MakerBundle\InputAwareMakerInterface; +use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; +use Symfony\Bundle\MakerBundle\Maker\MakeWebhook; use Symfony\Bundle\MakerBundle\MakerBundle; +use Symfony\Bundle\MakerBundle\MakerInterface; use Symfony\Bundle\MakerBundle\Test\MakerTestKernel; use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData; @@ -56,6 +60,20 @@ public function testGetClassDeclarationWithExtends(): void self::assertSame('final class MakerBundle extends MakerTestKernel', $meta->getClassDeclaration()); } + public function testGetClassDeclarationWithImplements(): void + { + $meta = ClassData::create(class: AbstractMaker::class, implements: MakerInterface::class); + + self::assertSame('final class AbstractMaker implements MakerInterface', $meta->getClassDeclaration()); + } + + public function testGetClassDeclarationWithExtendsAndImplements(): void + { + $meta = ClassData::create(class: MakeWebhook::class, extendsClass: AbstractMaker::class, implements: InputAwareMakerInterface::class); + + self::assertSame('final class MakeWebhook extends AbstractMaker implements InputAwareMakerInterface', $meta->getClassDeclaration()); + } + /** @dataProvider suffixDataProvider */ public function testSuffix(?string $suffix, string $expectedResult): void {