Skip to content

Commit 464c3fd

Browse files
committed
PhpFile: added getClasses()
1 parent eb7c840 commit 464c3fd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/PhpGenerator/PhpFile.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ public function getNamespaces(): array
9191
}
9292

9393

94+
/** @return ClassType[] */
95+
public function getClasses(): array
96+
{
97+
$classes = [];
98+
foreach ($this->namespaces as $n => $namespace) {
99+
$n .= $n ? '\\' : '';
100+
foreach ($namespace->getClasses() as $c => $class) {
101+
$classes[$n . $c] = $class;
102+
}
103+
}
104+
return $classes;
105+
}
106+
107+
94108
/** @return static */
95109
public function addUse(string $name, string $alias = null): self
96110
{

tests/PhpGenerator/PhpFile.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,40 @@ $file->addClass('FooBar\I');
7979

8080
sameFile(__DIR__ . '/expected/PhpFile.bracketed.expect', (string) $file);
8181

82+
Assert::same([
83+
'Foo',
84+
'Bar',
85+
'Baz',
86+
'',
87+
'FooBar',
88+
], array_keys($file->getNamespaces()));
89+
90+
Assert::same([
91+
'Foo\A',
92+
'Foo\B',
93+
'Foo\C',
94+
'Bar\B',
95+
'Bar\C',
96+
'Bar\D',
97+
'Bar\EN',
98+
'Baz\E',
99+
'Baz\F',
100+
'Baz\G',
101+
'H',
102+
'FooBar\I',
103+
], array_keys($file->getClasses()));
104+
105+
106+
82107
$file = new PhpFile;
83108
$file->addClass('A');
84109
$file->addUse('A')
85110
->addUse('B', 'C');
86111

87112
sameFile(__DIR__ . '/expected/PhpFile.globalNamespace.expect', (string) $file);
88113

114+
115+
89116
$file = new PhpFile;
90117
$file->addComment('This file is auto-generated. DO NOT EDIT!');
91118
$file->setStrictTypes();

0 commit comments

Comments
 (0)