-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\PhpGenerator; | ||
|
||
use Nette; | ||
|
||
|
||
final class ClassManipulator | ||
{ | ||
public function __construct( | ||
private ClassType $class, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* Inherits property from parent class. | ||
*/ | ||
public function inheritProperty(string $name, bool $returnIfExists = false): Property | ||
{ | ||
$extends = $this->class->getExtends(); | ||
if ($this->class->hasProperty($name)) { | ||
return $returnIfExists | ||
? $this->class->getProperty($name) | ||
: throw new Nette\InvalidStateException("Cannot inherit property '$name', because it already exists."); | ||
|
||
} elseif (!$extends) { | ||
throw new Nette\InvalidStateException("Class '{$this->class->getName()}' has not setExtends() set."); | ||
} | ||
|
||
try { | ||
$rp = new \ReflectionProperty($extends, $name); | ||
} catch (\ReflectionException) { | ||
throw new Nette\InvalidStateException("Property '$name' has not been found in ancestor {$extends}"); | ||
} | ||
|
||
$property = (new Factory)->fromPropertyReflection($rp); | ||
$this->class->addMember($property); | ||
return $property; | ||
} | ||
|
||
|
||
/** | ||
* Inherits method from parent class or interface. | ||
*/ | ||
public function inheritMethod(string $name, bool $returnIfExists = false): Method | ||
{ | ||
$parents = [...(array) $this->class->getExtends(), ...$this->class->getImplements()]; | ||
if ($this->class->hasMethod($name)) { | ||
return $returnIfExists | ||
? $this->class->getMethod($name) | ||
: throw new Nette\InvalidStateException("Cannot inherit method '$name', because it already exists."); | ||
|
||
} elseif (!$parents) { | ||
throw new Nette\InvalidStateException("Class '{$this->class->getName()}' has neither setExtends() nor setImplements() set."); | ||
} | ||
|
||
foreach ($parents as $parent) { | ||
try { | ||
$rm = new \ReflectionMethod($parent, $name); | ||
} catch (\ReflectionException) { | ||
continue; | ||
} | ||
$method = (new Factory)->fromMethodReflection($rm); | ||
$this->class->addMember($method); | ||
return $method; | ||
} | ||
|
||
throw new Nette\InvalidStateException("Method '$name' has not been found in any ancestor: " . implode(', ', $parents)); | ||
} | ||
|
||
|
||
/** | ||
* Automatically implements all methods from the given interface. | ||
*/ | ||
public function implementInterface(string $interfaceName): void | ||
{ | ||
$interface = new \ReflectionClass($interfaceName); | ||
if (!$interface->isInterface()) { | ||
throw new Nette\InvalidArgumentException("Class '$interfaceName' is not an interface."); | ||
} | ||
|
||
$this->class->addImplement($interfaceName); | ||
foreach ($interface->getMethods() as $method) { | ||
$this->inheritMethod($method->getName(), returnIfExists: true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/PhpGenerator/ClassManipulator.implementInterface.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\PhpGenerator\ClassManipulator; | ||
use Nette\PhpGenerator\ClassType; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
interface TestInterface | ||
{ | ||
public function testMethod(); | ||
} | ||
|
||
$class = new ClassType('TestClass'); | ||
$manipulator = new ClassManipulator($class); | ||
|
||
// Test valid interface implementation | ||
$manipulator->implementInterface(TestInterface::class); | ||
Assert::true(in_array(TestInterface::class, $class->getImplements(), true)); | ||
Assert::true($class->hasMethod('testMethod')); | ||
|
||
// Test exception for non-interface | ||
Assert::exception( | ||
fn() => $manipulator->implementInterface(stdClass::class), | ||
InvalidArgumentException::class, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters