Skip to content

Commit 320e635

Browse files
committed
minor #2267 [Autocomplete] Remove symfony/string dependency (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Autocomplete] Remove symfony/string dependency * remove Symfony\String dependency (used only to get form short name) * fix Attribute methods missing internal tag Commits ------- b9e3037 [Autocomplete] Remove symfony/string dependency
2 parents 89c71d1 + b9e3037 commit 320e635

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/Autocomplete/composer.json

+1-2
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

+14-4
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Autocomplete\Tests\Unit\Form;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
16+
use Symfony\UX\Autocomplete\Tests\Fixtures\Form\ProductType;
17+
18+
class AsEntityAutocompleteFieldTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider provideClassNames
22+
*/
23+
public function testShortName(string $shortName, string $className): void
24+
{
25+
$this->assertEquals($shortName, AsEntityAutocompleteField::shortName($className));
26+
}
27+
28+
/**
29+
* @return iterable<{string, string}>
30+
*/
31+
public static function provideClassNames(): iterable
32+
{
33+
yield from [
34+
['as_entity_autocomplete_field', AsEntityAutocompleteField::class],
35+
['product_type', ProductType::class],
36+
['bar', 'Bar'],
37+
['foo_bar', 'FooBar'],
38+
['foo_bar', 'Foo\FooBar'],
39+
['foo_bar', 'Foo\Bar\FooBar'],
40+
];
41+
}
42+
}

0 commit comments

Comments
 (0)