Skip to content

Commit 0c4df0d

Browse files
committed
added PhpNamespace::getClass() & getFunction()
1 parent b5a3f3b commit 0c4df0d

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

src/PhpGenerator/PhpNamespace.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,30 @@ public function addEnum(string $name): EnumType
313313
}
314314

315315

316+
/**
317+
* Returns a class-like type from the namespace.
318+
*/
319+
public function getClass(string $name): ClassType|InterfaceType|TraitType|EnumType
320+
{
321+
return $this->classes[strtolower($name)] ?? throw new Nette\InvalidArgumentException("Class '$name' not found.");
322+
}
323+
324+
325+
/**
326+
* Returns all class-like types in the namespace.
327+
* @return (ClassType|InterfaceType|TraitType|EnumType)[]
328+
*/
329+
public function getClasses(): array
330+
{
331+
$res = [];
332+
foreach ($this->classes as $class) {
333+
$res[$class->getName()] = $class;
334+
}
335+
336+
return $res;
337+
}
338+
339+
316340
/**
317341
* Removes a class-like type from namespace.
318342
*/
@@ -340,42 +364,36 @@ public function addFunction(string $name): GlobalFunction
340364

341365

342366
/**
343-
* Removes a function type from namespace.
367+
* Returns a function from the namespace.
344368
*/
345-
public function removeFunction(string $name): static
369+
public function getFunction(string $name): GlobalFunction
346370
{
347-
unset($this->functions[strtolower($name)]);
348-
return $this;
371+
return $this->functions[strtolower($name)] ?? throw new Nette\InvalidArgumentException("Function '$name' not found.");
349372
}
350373

351374

352375
/**
353-
* Returns all class-like types in the namespace.
354-
* @return (ClassType|InterfaceType|TraitType|EnumType)[]
376+
* Returns all functions in the namespace.
377+
* @return GlobalFunction[]
355378
*/
356-
public function getClasses(): array
379+
public function getFunctions(): array
357380
{
358381
$res = [];
359-
foreach ($this->classes as $class) {
360-
$res[$class->getName()] = $class;
382+
foreach ($this->functions as $fn) {
383+
$res[$fn->getName()] = $fn;
361384
}
362385

363386
return $res;
364387
}
365388

366389

367390
/**
368-
* Returns all functions in the namespace.
369-
* @return GlobalFunction[]
391+
* Removes a function type from namespace.
370392
*/
371-
public function getFunctions(): array
393+
public function removeFunction(string $name): static
372394
{
373-
$res = [];
374-
foreach ($this->functions as $fn) {
375-
$res[$fn->getName()] = $fn;
376-
}
377-
378-
return $res;
395+
unset($this->functions[strtolower($name)]);
396+
return $this;
379397
}
380398

381399

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ Assert::exception(
2525
$interfaceB = $namespace->addInterface('B');
2626
Assert::same($namespace, $interfaceB->getNamespace());
2727

28+
Assert::same($classA, $namespace->getClass('a'));
29+
2830
Assert::count(2, $namespace->getClasses());
29-
Assert::type(Nette\PhpGenerator\ClassType::class, $namespace->getClasses()['A']);
31+
Assert::same($classA, $namespace->getClasses()['A']);
3032
$namespace->removeClass('a');
3133
Assert::count(1, $namespace->getClasses());
3234

@@ -39,6 +41,8 @@ Assert::exception(
3941
"Cannot add 'Foo', because it already exists.",
4042
);
4143

44+
Assert::same($function, $namespace->getFunction('foo'));
45+
4246
Assert::count(1, $namespace->getFunctions());
4347
Assert::same($function, $namespace->getFunctions()['foo']);
4448
$namespace->removeFunction('FOO');

0 commit comments

Comments
 (0)