Skip to content

Commit 028a8a0

Browse files
committed
feat: allow ClassData to define an implemented class
1 parent 0624f13 commit 028a8a0

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Util/ClassSource/Model/ClassData.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ private function __construct(
3030
private bool $isFinal = true,
3131
private string $rootNamespace = 'App',
3232
private ?string $classSuffix = null,
33+
public readonly ?string $implements = null,
3334
) {
3435
if (str_starts_with(haystack: $this->namespace, needle: $this->rootNamespace)) {
3536
$this->namespace = substr_replace(string: $this->namespace, replace: '', offset: 0, length: \strlen($this->rootNamespace) + 1);
3637
}
3738
}
3839

39-
public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = []): self
40+
public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = [], ?string $implements = null): self
4041
{
4142
$className = Str::getShortClassName($class);
4243

@@ -50,13 +51,18 @@ public static function create(string $class, ?string $suffix = null, ?string $ex
5051
$useStatements->addUseStatement($extendsClass);
5152
}
5253

54+
if ($implements) {
55+
$useStatements->addUseStatement($implements);
56+
}
57+
5358
return new self(
5459
className: Str::asClassName($className),
5560
namespace: Str::getNamespace($class),
5661
extends: null === $extendsClass ? null : Str::getShortClassName($extendsClass),
5762
isEntity: $isEntity,
5863
useStatementGenerator: $useStatements,
5964
classSuffix: $suffix,
65+
implements: null === $implements ? null : Str::getShortClassName($implements),
6066
);
6167
}
6268

@@ -130,10 +136,17 @@ public function getClassDeclaration(): string
130136
$extendsDeclaration = \sprintf(' extends %s', $this->extends);
131137
}
132138

139+
$implementsDeclaration = '';
140+
141+
if (null !== $this->implements) {
142+
$implementsDeclaration = \sprintf(' implements %s', $this->implements);
143+
}
144+
133145
return \sprintf('%sclass %s%s',
134146
$this->isFinal ? 'final ' : '',
135147
$this->className,
136148
$extendsDeclaration,
149+
$implementsDeclaration
137150
);
138151
}
139152

tests/Util/ClassSource/ClassDataTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
namespace Symfony\Bundle\MakerBundle\Tests\Util\ClassSource;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
16+
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
17+
use Symfony\Bundle\MakerBundle\Maker\MakeWebhook;
1518
use Symfony\Bundle\MakerBundle\MakerBundle;
19+
use Symfony\Bundle\MakerBundle\MakerInterface;
1620
use Symfony\Bundle\MakerBundle\Test\MakerTestKernel;
1721
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
1822

@@ -56,6 +60,20 @@ public function testGetClassDeclarationWithExtends(): void
5660
self::assertSame('final class MakerBundle extends MakerTestKernel', $meta->getClassDeclaration());
5761
}
5862

63+
public function testGetClassDeclarationWithImplements(): void
64+
{
65+
$meta = ClassData::create(class: AbstractMaker::class, implements: MakerInterface::class);
66+
67+
self::assertSame('final class AbstractMaker implements MakerInterface', $meta->getClassDeclaration());
68+
}
69+
70+
public function testGetClassDeclarationWithExtendsAndImplements(): void
71+
{
72+
$meta = ClassData::create(class: MakeWebhook::class, extendsClass: AbstractMaker::class, implements: InputAwareMakerInterface::class);
73+
74+
self::assertSame('final class MakeWebhook extends AbstractMaker implements InputAwareMakerInterface', $meta->getClassDeclaration());
75+
}
76+
5977
/** @dataProvider suffixDataProvider */
6078
public function testSuffix(?string $suffix, string $expectedResult): void
6179
{

0 commit comments

Comments
 (0)