Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/Util/ClassSource/Model/ClassData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -50,13 +51,18 @@ 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),
extends: null === $extendsClass ? null : Str::getShortClassName($extendsClass),
isEntity: $isEntity,
useStatementGenerator: $useStatements,
classSuffix: $suffix,
implements: null === $implements ? null : Str::getShortClassName($implements),
);
}

Expand Down Expand Up @@ -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
);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Util/ClassSource/ClassDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand Down