Skip to content

Commit 9771dca

Browse files
committed
Compact namespace at TransformedType->getNamespaceSegments() instead of TransformedType->getTypescriptName()
1 parent ee44437 commit 9771dca

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

src/Compactors/Compactor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
*/
88
interface Compactor
99
{
10-
public function compact(string $typescriptIdentifier): string;
10+
public function removeSuffix(string $typeName): string;
11+
12+
public function removePrefix(string $namespace): string;
1113
}

src/Compactors/ConfigCompactor.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ public function __construct(TypeScriptTransformerConfig $config) {
2323
protected function getPrefixes(): array {
2424
if ($this->prefixes === null) {
2525
$this->prefixes = array_map(
26-
function(string $prefix): string {
27-
$prefix = str_replace("\\", ".", $prefix);
28-
if (!str_ends_with($prefix, ".")) {
29-
$prefix .= ".";
26+
function (string $prefix): string {
27+
if (str_ends_with($prefix, "\\")) {
28+
$prefix = rtrim($prefix, "\\");
3029
}
3130
return $prefix;
3231
},
@@ -46,28 +45,30 @@ protected function getSuffixes(): array {
4645
return $this->suffixes;
4746
}
4847

49-
public function compact(
50-
string $typescriptIdentifier
51-
): string {
52-
$matchingPrefix = '';
48+
public function removeSuffix(string $typeName): string {
5349
$matchingSuffix = '';
54-
foreach ($this->getPrefixes() as $prefix) {
55-
if (str_starts_with($typescriptIdentifier, $prefix)) {
56-
$matchingPrefix = $prefix;
57-
break;
58-
}
59-
}
6050
foreach ($this->getSuffixes() as $suffix) {
61-
if (str_ends_with($typescriptIdentifier, $suffix)) {
51+
if (str_ends_with($typeName, $suffix)) {
6252
$matchingSuffix = $suffix;
6353
break;
6454
}
6555
}
6656
if ($matchingSuffix !== '') {
67-
$typescriptIdentifier = substr($typescriptIdentifier, 0, -strlen($matchingSuffix));
57+
$typeName = substr($typeName, 0, -strlen($matchingSuffix));
58+
}
59+
return $typeName;
60+
}
61+
62+
public function removePrefix(string $namespace): string {
63+
$matchingPrefix = '';
64+
foreach ($this->getPrefixes() as $prefix) {
65+
if (str_starts_with($namespace, $prefix)) {
66+
$matchingPrefix = $prefix;
67+
break;
68+
}
6869
}
69-
$substr = substr($typescriptIdentifier, strlen($matchingPrefix));
70-
return $substr;
70+
$substr = substr($namespace, strlen($matchingPrefix));
71+
return ltrim($substr, '\\');
7172
}
7273

7374
}

src/Compactors/IdentityCompactor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
class IdentityCompactor implements Compactor
66
{
77

8-
public function compact(string $typescriptIdentifier): string {
9-
return $typescriptIdentifier;
8+
public function removeSuffix(string $typeName): string {
9+
return $typeName;
1010
}
1111

12+
public function removePrefix(string $namespace): string {
13+
return $namespace;
14+
}
1215
}

src/Structures/TransformedType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function create(
3333
string $keyword = 'type',
3434
bool $trailingSemicolon = true,
3535
): self {
36-
return new self($class, $compactor->compact($name), $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
36+
return new self($class, $compactor->removeSuffix($name), $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
3737
}
3838

3939
public static function createInline(
@@ -77,6 +77,12 @@ public function getNamespaceSegments(): array
7777
return [];
7878
}
7979

80+
$namespace = $this->compactor->removePrefix($namespace);
81+
82+
if ($namespace === '') {
83+
return [];
84+
}
85+
8086
return explode('\\', $namespace);
8187
}
8288

@@ -91,9 +97,7 @@ public function getTypeScriptName($fullyQualified = true): string
9197
[$this->name]
9298
);
9399

94-
return $this->compactor->compact(
95-
implode('.', $segments)
96-
);
100+
return implode('.', $segments);
97101
}
98102

99103
public function replaceSymbol(string $class, string $replacement): void

src/Writers/TypeDefinitionWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function format(TypesCollection $collection): string
2727

2828
foreach ($namespaces as $namespace => $types) {
2929
asort($types);
30-
$namespace = $this->compactor->compact($namespace);
30+
$namespace = $this->compactor->removePrefix($namespace);
3131

3232
$output .= "declare namespace {$namespace} {".PHP_EOL;
3333

tests/Transformers/DtoTransformerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class DummyOptionalDto
169169
(new DtoTransformer(
170170
TypeScriptTransformerConfig::create()
171171
->compactorPrefixes([
172-
"Spatie.TypeScriptTransformer.Tests.FakeClasses.Integration"
172+
"Spatie\\TypeScriptTransformer\\Tests\\FakeClasses\\Integration"
173173
])
174174
))->transform(
175175
$reflectionClass,
@@ -182,7 +182,7 @@ class DummyOptionalDto
182182
(new DtoTransformer(
183183
TypeScriptTransformerConfig::create()
184184
->compactorPrefixes([
185-
"Spatie.TypeScriptTransformer.Tests.FakeClasses"
185+
"Spatie\\TypeScriptTransformer\\Tests\\FakeClasses"
186186
])
187187
))->transform(
188188
$reflectionClass,
@@ -194,8 +194,8 @@ class DummyOptionalDto
194194
(new DtoTransformer(
195195
TypeScriptTransformerConfig::create()
196196
->compactorPrefixes([
197-
"Spatie.TypeScriptTransformer.Tests.RealClasses",
198-
"Spatie.TypeScriptTransformer.Tests.FakeClasses"
197+
"Spatie\\TypeScriptTransformer\\Tests\\RealClasses",
198+
"Spatie\\TypeScriptTransformer\\Tests\\FakeClasses"
199199
])
200200
))->transform(
201201
$reflectionClass,
@@ -209,7 +209,7 @@ class DummyOptionalDto
209209
'Dto',
210210
(new DtoTransformer(
211211
TypeScriptTransformerConfig::create()
212-
->compactorPrefixes("Spatie.TypeScriptTransformer.Tests.FakeClasses.Integration")
212+
->compactorPrefixes("Spatie\\TypeScriptTransformer\\Tests\\FakeClasses\\Integration")
213213
->compactorSuffixes([
214214
'WithChildren'
215215
])

0 commit comments

Comments
 (0)