Skip to content
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

Allow ClassData to define an implemented class #1638

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
feat: allow ClassData to define an implemented class
MrYamous committed Jan 1, 2025
commit 028a8a0d58db65d36da4d45b4f65179101879aca
15 changes: 14 additions & 1 deletion src/Util/ClassSource/Model/ClassData.php
Original file line number Diff line number Diff line change
@@ -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,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),
);
}

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

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

Check failure on line 67 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 67 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 67 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 67 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that two strings are identical.

Check failure on line 67 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that two strings are identical.
}

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());

Check failure on line 74 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 74 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 74 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 74 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that two strings are identical.

Check failure on line 74 in tests/Util/ClassSource/ClassDataTest.php

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that two strings are identical.
}

/** @dataProvider suffixDataProvider */
public function testSuffix(?string $suffix, string $expectedResult): void
{