Skip to content

Commit 2bb0ef9

Browse files
authored
Merge pull request #6 from NumyAddon/feature/shorter-class-names
2 parents 20d565b + 8827ba4 commit 2bb0ef9

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/Frame.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ public function getRootNode(): self
3636
return $node;
3737
}
3838

39-
public function getName(): string
39+
public function getName(bool $emptyParent = false): string
4040
{
4141
if ($this->parent && str_contains($this->name, '$parent')) {
4242
$parent = $this;
4343
$parentName = '';
44-
while ($parent = $parent->getParent()) {
45-
$parentName = $parent->getName();
46-
if ($parentName) {
47-
break;
44+
if (!$emptyParent) {
45+
while ($parent = $parent->getParent()) {
46+
$parentName = $parent->getName();
47+
if ($parentName) {
48+
break;
49+
}
4850
}
4951
}
5052
return str_replace('$parent', $parentName, $this->name);
@@ -55,17 +57,16 @@ public function getName(): string
5557

5658
public function getClassName(): ?string
5759
{
60+
if ($this->getName() && $this->getRootNode()::class === Frame::class) {
61+
return $this->sanitizeClassName($this->getName());
62+
}
5863
$prefix = $this->parent?->getClassName() ?? '';
59-
$name = $this->getName() ?: $this->getParentKey();
60-
if (!$name) {
64+
$name = $this->getName(true) ?: $this->getParentKey();
65+
if (!$name || ($this->parent && !$prefix)) {
6166
return null;
6267
}
6368

64-
return str_replace(
65-
['$', ' ', '-', '.'],
66-
['', '_', '_', '_'],
67-
$prefix === '' ? $name : $prefix . '_' . $name,
68-
);
69+
return $this->sanitizeClassName($prefix === '' ? $name : ($prefix . '_' . $name));
6970
}
7071

7172
public function getParent(): ?self
@@ -140,7 +141,9 @@ public function getInherits(): array
140141
$inherits = (string) $this->xmlElement->attributes()['inherits'] ?? '';
141142
$inherits = str_replace(' ', '', $inherits);
142143

143-
return $inherits === '' ? [] : explode(',', $inherits);
144+
return $inherits === ''
145+
? []
146+
: array_map($this->sanitizeClassName(...), explode(',', $inherits));
144147
}
145148

146149
/**
@@ -189,4 +192,13 @@ public function getLineNumber(): int
189192

190193
return $node->getLineNo();
191194
}
195+
196+
private function sanitizeClassName(string $name): string
197+
{
198+
return str_replace(
199+
['$', ' ', '-', '.', '!'],
200+
['', '_', '_', '_', '_'],
201+
$name,
202+
);
203+
}
192204
}

src/Registry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
class Registry
1212
{
1313
/**
14-
* @var T[]
14+
* @var array<string, (T&Frame)>
1515
*/
1616
private array $items = [];
1717

1818
/**
19-
* @param T $template
19+
* @param T&Frame $template
2020
*/
2121
public function register(?string $name, Frame $template): void
2222
{
@@ -27,15 +27,15 @@ public function register(?string $name, Frame $template): void
2727
}
2828

2929
/**
30-
* @return T|null
30+
* @return (T&Frame)|null
3131
*/
3232
public function get(string $name): ?Frame
3333
{
3434
return $this->items[$name] ?? null;
3535
}
3636

3737
/**
38-
* @return T[]
38+
* @return array<string, (T&Frame)>
3939
*/
4040
public function all(): array
4141
{

src/XmlFileParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function parseNode(SimpleXMLElement $node, Registry $fileRegistry, ?Fram
103103
$this->frameRegistry->register($frame->getClassName(), $frame);
104104
}
105105
if (!empty($name)) {
106-
$fileRegistry->register($name, $frame);
106+
$fileRegistry->register($frame->getClassName(), $frame);
107107
}
108108
$parent?->addChild($frame);
109109

0 commit comments

Comments
 (0)