Skip to content

Commit

Permalink
allow creating class data objects with absolute namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Sep 25, 2024
1 parent d2b0f35 commit 8c8566e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/Util/ClassSource/Model/ClassData.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function getNamespace(): string
return $this->rootNamespace;
}

// Namespace is already absolute, don't add the rootNamespace.
if (str_starts_with($this->namespace, '\\')) {
return substr_replace($this->namespace, '', 0, 1);
}

return \sprintf('%s\%s', $this->rootNamespace, $this->namespace);
}

Expand Down
20 changes: 1 addition & 19 deletions tests/Util/ClassSource/ClassDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Bundle\MakerBundle\Test\MakerTestKernel;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;

class ClassDataTest extends TestCase
Expand Down Expand Up @@ -115,12 +114,9 @@ public function testGetClassNameRelativeNamespace(): void

public function testGetClassNameWithAbsoluteNamespace(): void
{
$this->markTestSkipped();
$class = ClassData::create(class: '\\Foo\\Bar\\Admin\\Baz', suffix: 'Controller');
self::assertSame('BazController', $class->getClassName());
self::assertSame('Baz', $class->getClassName(relative: false, withoutSuffix: true));
// self::assertSame('Admin\FooController', $class->getClassName(relative: true, withoutSuffix: false));
// self::assertSame('Admin\Baz', $class->getClassName(relative: true, withoutSuffix: true));
self::assertSame('Foo\Bar\Admin', $class->getNamespace());
self::assertSame('Foo\Bar\Admin\BazController', $class->getFullClassName());
}

Expand All @@ -147,18 +143,4 @@ public function fullClassNameProvider(): \Generator
yield ['Controller\MyController', 'Custom', false, true, 'Custom\Controller\My'];
yield ['Controller\MyController', 'Custom', true, true, 'Controller\My'];
}

// public function testClassNameDetails(): void
// {
// $class = new ClassNameDetails(
// fullClassName: 'Foo',
// namespacePrefix: 'Controller\\',
// suffix: 'Controller',
// );
//
// self::assertSame('FooController', $class->getFullName());
// self::assertSame('MyController', $class->getShortName());
// self::assertSame('My', $class->getRelativeNameWithoutSuffix());
// self::assertSame('MyController', $class->getRelativeName());
// }
}

0 comments on commit 8c8566e

Please sign in to comment.