|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\PhpGenerator; |
| 11 | + |
| 12 | +use Nette; |
| 13 | + |
| 14 | + |
| 15 | +final class ClassManipulator |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private ClassType $class, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Inherits property from parent class. |
| 25 | + */ |
| 26 | + public function inheritProperty(string $name, bool $returnIfExists = false): Property |
| 27 | + { |
| 28 | + $extends = $this->class->getExtends(); |
| 29 | + if ($this->class->hasProperty($name)) { |
| 30 | + return $returnIfExists |
| 31 | + ? $this->class->getProperty($name) |
| 32 | + : throw new Nette\InvalidStateException("Cannot inherit property '$name', because it already exists."); |
| 33 | + |
| 34 | + } elseif (!$extends) { |
| 35 | + throw new Nette\InvalidStateException("Class '{$this->class->getName()}' has not setExtends() set."); |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + $rp = new \ReflectionProperty($extends, $name); |
| 40 | + } catch (\ReflectionException) { |
| 41 | + throw new Nette\InvalidStateException("Property '$name' has not been found in ancestor {$extends}"); |
| 42 | + } |
| 43 | + |
| 44 | + $property = (new Factory)->fromPropertyReflection($rp); |
| 45 | + $this->class->addMember($property); |
| 46 | + return $property; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Inherits method from parent class or interface. |
| 52 | + */ |
| 53 | + public function inheritMethod(string $name, bool $returnIfExists = false): Method |
| 54 | + { |
| 55 | + $parents = [...(array) $this->class->getExtends(), ...$this->class->getImplements()]; |
| 56 | + if ($this->class->hasMethod($name)) { |
| 57 | + return $returnIfExists |
| 58 | + ? $this->class->getMethod($name) |
| 59 | + : throw new Nette\InvalidStateException("Cannot inherit method '$name', because it already exists."); |
| 60 | + |
| 61 | + } elseif (!$parents) { |
| 62 | + throw new Nette\InvalidStateException("Class '{$this->class->getName()}' has neither setExtends() nor setImplements() set."); |
| 63 | + } |
| 64 | + |
| 65 | + foreach ($parents as $parent) { |
| 66 | + try { |
| 67 | + $rm = new \ReflectionMethod($parent, $name); |
| 68 | + } catch (\ReflectionException) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + $method = (new Factory)->fromMethodReflection($rm); |
| 72 | + $this->class->addMember($method); |
| 73 | + return $method; |
| 74 | + } |
| 75 | + |
| 76 | + throw new Nette\InvalidStateException("Method '$name' has not been found in any ancestor: " . implode(', ', $parents)); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Implements all methods from the given interface. |
| 82 | + */ |
| 83 | + public function implementInterface(string $interfaceName): void |
| 84 | + { |
| 85 | + $interface = new \ReflectionClass($interfaceName); |
| 86 | + if (!$interface->isInterface()) { |
| 87 | + throw new Nette\InvalidArgumentException("Class '$interfaceName' is not an interface."); |
| 88 | + } |
| 89 | + |
| 90 | + $this->class->addImplement($interfaceName); |
| 91 | + foreach ($interface->getMethods() as $method) { |
| 92 | + $this->inheritMethod($method->getName(), returnIfExists: true); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments