Skip to content

Commit f90de23

Browse files
committed
[Autocomplete] Remove String dependency
* remove Symfony\String dependency (used only to get form short name) * fix Attribute methods missing internal tag
1 parent 707126b commit f90de23

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/Autocomplete/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"symfony/deprecation-contracts": "^2.5|^3",
3030
"symfony/http-foundation": "^6.3|^7.0",
3131
"symfony/http-kernel": "^6.3|^7.0",
32-
"symfony/property-access": "^6.3|^7.0",
33-
"symfony/string": "^6.3|^7.0"
32+
"symfony/property-access": "^6.3|^7.0"
3433
},
3534
"require-dev": {
3635
"doctrine/collections": "^1.6.8|^2.0",

src/Autocomplete/src/Form/AsEntityAutocompleteField.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\UX\Autocomplete\Form;
1313

14-
use Symfony\Component\String\UnicodeString;
15-
1614
/**
1715
* All form types that want to expose autocomplete functionality should have this.
1816
*
@@ -37,13 +35,25 @@ public function getRoute(): string
3735
return $this->route;
3836
}
3937

38+
/**
39+
* @internal
40+
*
41+
* @param class-string $class
42+
*/
4043
public static function shortName(string $class): string
4144
{
42-
$string = new UnicodeString($class);
45+
if ($pos = (int) strrpos($class, '\\')) {
46+
$class = substr($class, $pos + 1);
47+
}
4348

44-
return $string->afterLast('\\')->snake()->toString();
49+
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class));
4550
}
4651

52+
/**
53+
* @internal
54+
*
55+
* @param class-string $class
56+
*/
4757
public static function getInstance(string $class): ?self
4858
{
4959
$reflectionClass = new \ReflectionClass($class);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Symfony\UX\Autocomplete\Tests\Unit\Form;
4+
5+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\UX\Autocomplete\Tests\Fixtures\Form\ProductType;
8+
9+
class AsEntityAutocompleteFieldTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider provideClassNames
13+
*/
14+
public function testShortName(string $shortName, string $className): void
15+
{
16+
$this->assertEquals($shortName, AsEntityAutocompleteField::shortName($className));
17+
}
18+
19+
/**
20+
* @return iterable<{string, string}>
21+
*/
22+
public static function provideClassNames(): iterable
23+
{
24+
yield from [
25+
['as_entity_autocomplete_field', AsEntityAutocompleteField::class],
26+
['product_type', ProductType::class],
27+
['bar', 'Bar'],
28+
['foo_bar', 'FooBar'],
29+
['foo_bar', 'Foo\FooBar'],
30+
['foo_bar', 'Foo\Bar\FooBar'],
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)