Skip to content

Commit f753510

Browse files
committed
Improve default filters to match more cases - #12
1 parent 49e5f1d commit f753510

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
"@test",
5757
"@analyse"
5858
],
59-
"cs": "php-cs-fixer fix src -v --diff --dry-run",
60-
"cs-fix": "php-cs-fixer fix src -v --diff",
59+
"cs": "php-cs-fixer fix -v --diff --dry-run",
60+
"cs-fix": "php-cs-fixer fix -v --diff",
6161
"test": "vendor/bin/phpunit",
6262
"analyse": "php vendor/bin/phpstan.phar analyse --no-interaction"
6363
},

src/FilterFactory.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ final class FilterFactory
2323
/**
2424
* Returns a normalize filter for labels, names ...
2525
*
26-
* @param callable ...$callables Attached to the end of the filter chain
26+
* @param callable ...$callables Attached to the end of the filter chain
2727
* @return callable
2828
*/
2929
public static function normalizeFilter(callable ...$callables): callable
3030
{
3131
$filter = new Filter\FilterChain();
3232
$filter->attach(new NormalizeLabel());
33-
$filter->attach(new Filter\Word\SeparatorToSeparator(' ', '-'));
34-
$filter->attach(new Filter\Word\UnderscoreToCamelCase());
35-
$filter->attach(new Filter\Word\DashToCamelCase());
3633

3734
foreach ($callables as $callable) {
3835
$filter->attach($callable);
@@ -48,7 +45,15 @@ public static function normalizeFilter(callable ...$callables): callable
4845
*/
4946
public static function classNameFilter(): callable
5047
{
51-
return new UpperCaseFirst(self::normalizeFilter());
48+
return new UpperCaseFirst(
49+
self::normalizeFilter(
50+
new Filter\Word\CamelCaseToUnderscore(),
51+
new Filter\StringToLower(),
52+
new Filter\Word\UnderscoreToCamelCase(),
53+
new Filter\Word\SeparatorToSeparator(' ', '-'),
54+
new Filter\Word\DashToCamelCase()
55+
)
56+
);
5257
}
5358

5459
/**
@@ -59,6 +64,8 @@ public static function classNameFilter(): callable
5964
public static function constantNameFilter(): callable
6065
{
6166
return self::normalizeFilter(
67+
new Filter\Word\SeparatorToSeparator(' ', '-'),
68+
new Filter\Word\DashToCamelCase(),
6269
new Filter\Word\CamelCaseToUnderscore(),
6370
new Filter\StringToUpper()
6471
);
@@ -72,6 +79,8 @@ public static function constantNameFilter(): callable
7279
public static function constantValueFilter(): callable
7380
{
7481
return self::normalizeFilter(
82+
new Filter\Word\SeparatorToSeparator(' ', '-'),
83+
new Filter\Word\DashToCamelCase(),
7584
new Filter\Word\CamelCaseToUnderscore(),
7685
new Filter\StringToLower()
7786
);
@@ -84,7 +93,15 @@ public static function constantValueFilter(): callable
8493
*/
8594
public static function propertyNameFilter(): callable
8695
{
87-
return new LowerCaseFirst(self::normalizeFilter());
96+
return new LowerCaseFirst(
97+
self::normalizeFilter(
98+
new Filter\Word\CamelCaseToUnderscore(),
99+
new Filter\StringToLower(),
100+
new Filter\Word\UnderscoreToCamelCase(),
101+
new Filter\Word\SeparatorToSeparator(' ', '-'),
102+
new Filter\Word\DashToCamelCase()
103+
)
104+
);
88105
}
89106

90107
/**
@@ -94,7 +111,16 @@ public static function propertyNameFilter(): callable
94111
*/
95112
public static function methodNameFilter(): callable
96113
{
97-
return new LowerCaseFirst(self::normalizeFilter(new UpperToLower()));
114+
return new LowerCaseFirst(
115+
self::normalizeFilter(
116+
new Filter\Word\CamelCaseToUnderscore(),
117+
new Filter\StringToLower(),
118+
new Filter\Word\UnderscoreToCamelCase(),
119+
new Filter\Word\SeparatorToSeparator(' ', '-'),
120+
new Filter\Word\DashToCamelCase(),
121+
new UpperToLower()
122+
)
123+
);
98124
}
99125

100126
/**

tests/FilterFactoryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/**
4+
* @see https://github.com/open-code-modeling/php-filter for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-filter/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-filter/blob/master/LICENSE.md MIT License
7+
*/
8+
39
declare(strict_types=1);
410

511
namespace OpenCodeModelingTest\Filter;
@@ -15,6 +21,7 @@ final class FilterFactoryTest extends TestCase
1521
*/
1622
public function providerForLabel(): Generator
1723
{
24+
yield 'ADD_BUILDING' => ['ADD_BUILDING'];
1825
yield 'add building' => ['add building'];
1926
yield 'add_building' => ['add_building'];
2027
yield 'add-building' => ['add-building'];

0 commit comments

Comments
 (0)