Skip to content

Commit 597981c

Browse files
Cleanup
1 parent 34649b0 commit 597981c

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.0",
19+
"php": "^8.1",
2020
"nikic/php-parser": "^4.18|^5.0",
2121
"phpdocumentor/type-resolver": "^1.6.2",
2222
"symfony/process": "^5.2|^6.0|^7.0"

src/Transformers/EnumTransformer.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,15 @@ protected function toEnumValue(ReflectionEnumBackedCase $case): string
7373
{
7474
$value = $case->getBackingValue();
7575

76-
$newValue = "";
77-
78-
if (! is_string($value)) {
76+
if (!is_string($value)) {
7977
return "{$value}";
8078
}
8179

82-
for ($i = 0; $i < strlen($value); $i++) {
83-
$char = $value[$i];
84-
85-
$newValue .= match ($char) {
86-
'\\' => '\\\\',
87-
'\'' => '\\\'',
88-
default => $char,
89-
};
90-
}
80+
$escaped = strtr($value, [
81+
'\\' => '\\\\',
82+
'\'' => '\\\'',
83+
]);
9184

92-
return "'" . $newValue . "'";
85+
return "'{$escaped}'";
9386
}
9487
}

tests/FakeClasses/StringBackedEnumWithSingleQuotes.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Transformers/EnumTransformerTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,32 @@
116116
});
117117

118118
it('can transform a backed enum with strings with single-quotes into a enum', function () {
119+
enum TestStringBackedEnumWithSingleQuotes: string
120+
{
121+
case NO_QUOTE = 'no quote';
122+
case HAS_QUOTE = 'has quote \'';
123+
}
124+
119125
$transformer = new EnumTransformer(
120126
TypeScriptTransformerConfig::create()->transformToNativeEnums(true)
121127
);
122128

123129
$type = $transformer->transform(
124-
new ReflectionClass(StringBackedEnumWithSingleQuotes::class),
130+
new ReflectionClass(TestStringBackedEnumWithSingleQuotes::class),
125131
'Enum'
126132
);
127133

128134
assertEquals("'NO_QUOTE' = 'no quote', 'HAS_QUOTE' = 'has quote \''", $type->transformed);
129135
assertTrue($type->missingSymbols->isEmpty());
130136
assertFalse($type->isInline);
131137
assertEquals('enum', $type->keyword);
132-
});
133138

134-
it('can transform a backed enum with strings with single-quotes into a union', function () {
135139
$transformer = new EnumTransformer(
136140
TypeScriptTransformerConfig::create()->transformToNativeEnums(false)
137141
);
138142

139143
$type = $transformer->transform(
140-
new ReflectionClass(StringBackedEnumWithSingleQuotes::class),
144+
new ReflectionClass(TestStringBackedEnumWithSingleQuotes::class),
141145
'Enum'
142146
);
143147

0 commit comments

Comments
 (0)