Skip to content

Commit

Permalink
added inheritMethod()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 29, 2023
1 parent 04456d8 commit 8dd209b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/PhpGenerator/Traits/MethodsAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,46 @@ public function hasMethod(string $name): bool
{
return isset($this->methods[strtolower($name)]);
}


/**
* Inherits method from parent class or interface.
*/
public function inheritMethod(string $name, bool $callParent = false, bool $returnIfExists = false): Method
{
$lower = strtolower($name);
if (isset($this->methods[$lower])) {
return $returnIfExists
? $this->methods[$lower]
: throw new Nette\InvalidStateException("Cannot inherit method '$name', because it already exists.");
}

$parents = match (true) {
$this instanceof Nette\PhpGenerator\ClassType => [...(array) $this->getExtends(), ...$this->getImplements()],
$this instanceof Nette\PhpGenerator\InterfaceType => $this->getExtends(),
$this instanceof Nette\PhpGenerator\EnumType => $this->getImplements(),
};
if (!$parents) {
throw new Nette\InvalidStateException("Class '{$this->getName()}' has neither setExtends() nor setImplements() set.");
}

foreach ($parents as $parent) {
try {
$rm = new \ReflectionMethod($parent, $name);
} catch (\ReflectionException) {
continue;
}
$method = Method::from([$parent, $name]);
if ($callParent) {
$params = array_map(fn(\ReflectionParameter $param) => '$' . $param->getName(), $rm->getParameters());
if ($rm->isVariadic()) {
$params[] = '...' . array_pop($params);
}
$method->setBody('parent::' . $rm->getName() . '(' . implode(', ', $params) . ');');
}
return $this->methods[$lower] = $method;
}

throw new Nette\InvalidStateException("Method '$name' has not been found in any ancestor: " . implode(', ', $parents));
}
}
66 changes: 66 additions & 0 deletions tests/PhpGenerator/Method.inherit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class Foo
{
public function bar(int $a, ...$b): void
{
}
}


// missing parent
$class = new Nette\PhpGenerator\ClassType('Test');
Assert::exception(
fn() => $class->inheritMethod('bar'),
Nette\InvalidStateException::class,
"Class 'Test' has neither setExtends() nor setImplements() set.",
);

$class->setExtends('Unknown1');
$class->addImplement('Unknown2');
Assert::exception(
fn() => $class->inheritMethod('bar'),
Nette\InvalidStateException::class,
"Method 'bar' has not been found in any ancestor: Unknown1, Unknown2",
);


// implemented method
$class = new Nette\PhpGenerator\ClassType('Test');
$class->setExtends(Foo::class);
$method = $class->inheritMethod('bar');
Assert::match(<<<'XX'
public function bar(int $a, ...$b): void
{
}

XX, (string) $method);

$class = new Nette\PhpGenerator\ClassType('Test');
$class->setExtends(Foo::class);
$method = $class->inheritMethod('Bar', callParent: true); // intentionally case insensitive
Assert::match(<<<'XX'
public function bar(int $a, ...$b): void
{
parent::bar($a, ...$b);
}

XX, (string) $method);


// exists
$class = new Nette\PhpGenerator\ClassType('Test');
$method = $class->addMethod('bar');
Assert::same($method, $class->inheritMethod('bar', returnIfExists: true));
Assert::exception(
fn() => $class->inheritMethod('bar', returnIfExists: false),
Nette\InvalidStateException::class,
"Cannot inherit method 'bar', because it already exists.",
);

0 comments on commit 8dd209b

Please sign in to comment.