From 0ea7fc2bd61688a17f931028f885877d86fae5b4 Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 29 Mar 2023 16:30:24 +0100 Subject: [PATCH] Added ClassConstantNameNormalizer --- src/AbstractNormalizer.php | 2 +- src/ClassConstantNameNormalizer.php | 36 +++++++++++++++++++ test/ClassConstantNameNormalizerTest.php | 45 ++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/ClassConstantNameNormalizer.php create mode 100644 test/ClassConstantNameNormalizerTest.php diff --git a/src/AbstractNormalizer.php b/src/AbstractNormalizer.php index cb3620d..35ab7b6 100644 --- a/src/AbstractNormalizer.php +++ b/src/AbstractNormalizer.php @@ -271,7 +271,7 @@ protected function sanitizeReserved(string $string): string { assert($this->suffix !== null); - if (in_array(strtolower($string), self::RESERVED, true)) { + if (in_array(strtolower($string), static::RESERVED, true)) { return $string . $this->suffix; } return $string; diff --git a/src/ClassConstantNameNormalizer.php b/src/ClassConstantNameNormalizer.php new file mode 100644 index 0000000..5b53278 --- /dev/null +++ b/src/ClassConstantNameNormalizer.php @@ -0,0 +1,36 @@ +toAscii($label); + $spaced = $this->separatorsToSpace($ascii); + $speltOut = $this->spellOutAscii($spaced); + $cased = $this->toCase($speltOut); + + return $this->sanitizeReserved($cased); + } +} diff --git a/test/ClassConstantNameNormalizerTest.php b/test/ClassConstantNameNormalizerTest.php new file mode 100644 index 0000000..c137bac --- /dev/null +++ b/test/ClassConstantNameNormalizerTest.php @@ -0,0 +1,45 @@ +normalize($constantName); + self::assertSame($expected, $actual); + } + + public function constantNameProvider(): array + { + return [ + 'unicode_spellout' => ['€ sign', WordCase::UpperSnake, 'EURO_SIGN'], + 'ascii_spellout' => ['$', WordCase::UpperSnake, 'DOLLAR'], + 'const_reserved' => ['exit', WordCase::UpperSnake, 'EXIT'], + 'class' => ['class', WordCase::UpperSnake, 'CLASS_RESERVED'], + 'lead_digits' => ['12 foo', WordCase::UpperSnake, 'ONE_TWO_FOO'], + 'PascalCase' => ['foo bar', WordCase::Pascal, 'FooBar'], + ]; + } + + public function testNormalizeUsesSeparators(): void + { + $expected = 'FOO_BAR_BAZ'; + $normalizer = new ClassConstantNameNormalizer('Foo', WordCase::UpperSnake, '|/'); + $actual = $normalizer->normalize('Foo|Bar/ Baz'); + self::assertSame($expected, $actual); + } +}