Skip to content

Commit 4628dec

Browse files
committed
ClassManipulator::implementInterface() renamed to implement()
1 parent 5dab02e commit 4628dec

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,10 @@ $property = $manipulator->inheritProperty('foo');
902902
$property->setValue('new value');
903903
```
904904
905-
The `implementInterface()` method automatically implements all methods from the given interface in your class:
905+
The `implement()` method automatically implements all methods from the given interface in your class:
906906
907907
```php
908-
$manipulator->implementInterface(SomeInterface::class);
908+
$manipulator->implement(SomeInterface::class);
909909
// Now your class implements SomeInterface and includes all its methods
910910
```
911911

src/PhpGenerator/ClassManipulator.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,24 @@ public function inheritMethod(string $name, bool $returnIfExists = false): Metho
8080
/**
8181
* Implements all methods from the given interface.
8282
*/
83-
public function implementInterface(string $interfaceName): void
83+
public function implement(string $name): void
8484
{
85-
$interface = new \ReflectionClass($interfaceName);
86-
if (!$interface->isInterface()) {
87-
throw new Nette\InvalidArgumentException("Class '$interfaceName' is not an interface.");
85+
$definition = new \ReflectionClass($name);
86+
if (!$definition->isInterface()) {
87+
throw new Nette\InvalidArgumentException("Class '$name' is not an interface.");
8888
}
8989

90-
$this->class->addImplement($interfaceName);
91-
foreach ($interface->getMethods() as $method) {
90+
$this->class->addImplement($name);
91+
foreach ($definition->getMethods() as $method) {
9292
$this->inheritMethod($method->getName(), returnIfExists: true);
9393
}
9494
}
95+
96+
97+
/** @deprecated use implement() */
98+
public function implementInterface(string $interfaceName): void
99+
{
100+
trigger_error(__METHOD__ . '() is deprecated, use implement()', E_USER_DEPRECATED);
101+
$this->implement($interfaceName);
102+
}
95103
}

tests/PhpGenerator/ClassManipulator.implementInterface.phpt renamed to tests/PhpGenerator/ClassManipulator.implement.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ $class = new ClassType('TestClass');
1818
$manipulator = new ClassManipulator($class);
1919

2020
// Test valid interface implementation
21-
$manipulator->implementInterface(TestInterface::class);
21+
$manipulator->implement(TestInterface::class);
2222
Assert::true(in_array(TestInterface::class, $class->getImplements(), true));
2323
Assert::true($class->hasMethod('testMethod'));
2424

2525
// Test exception for non-interface
2626
Assert::exception(
27-
fn() => $manipulator->implementInterface(stdClass::class),
27+
fn() => $manipulator->implement(stdClass::class),
2828
InvalidArgumentException::class,
2929
);

0 commit comments

Comments
 (0)