diff --git a/src/Str.php b/src/Str.php index 51a31286b..38f70b096 100644 --- a/src/Str.php +++ b/src/Str.php @@ -117,7 +117,11 @@ public static function asEventMethod(string $eventName): string public static function getShortClassName(string $fullClassName): string { - return substr($fullClassName, strrpos($fullClassName, '\\') + 1); + if (!empty(self::getNamespace($fullClassName))) { + return substr($fullClassName, strrpos($fullClassName, '\\') + 1); + } else { + return $fullClassName; + } } public static function getNamespace(string $fullClassName): string diff --git a/tests/StrTest.php b/tests/StrTest.php index 79f6653e5..0b41e9dc4 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -161,4 +161,18 @@ public function getAsCamelCaseTests() yield ['foo_bar.baz\\pizza', 'FooBarBazPizza']; } + + /** + * @dataProvider getShortClassNameCaseTests + */ + public function testShortClassName(string $original, string $expected) + { + $this->assertSame($expected, Str::getShortClassName($original)); + } + + public function getShortClassNameCaseTests() + { + yield ['App\\Entity\\Foo', 'Foo']; + yield ['Foo', 'Foo']; + } }