Skip to content

Commit bba27ca

Browse files
committed
ClassType: added addMember() [Closes #35]
1 parent 4193dfc commit bba27ca

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ class Demo
153153
}
154154
```
155155

156+
You can also add existing `Method`, `Property` or `Constant` objects to the class:
157+
158+
```php
159+
$method = new Nette\PhpGenerator\Method('getHandle');
160+
$property = new Nette\PhpGenerator\Property('handle');
161+
$const = new Nette\PhpGenerator\Constant('ROLE');
162+
163+
$class = (new Nette\PhpGenerator\ClassType('Demo'))
164+
->addMember($method)
165+
->addMember($property)
166+
->addMember($const);
167+
```
168+
156169
Tabs versus spaces
157170
------------------
158171

src/PhpGenerator/ClassType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,32 @@ public function addTrait(string $name, array $resolutions = []): self
313313
}
314314

315315

316+
/**
317+
* @param Method|Property|Constant $member
318+
* @return static
319+
*/
320+
public function addMember($member): self
321+
{
322+
if ($member instanceof Method) {
323+
if ($this->type === self::TYPE_INTERFACE) {
324+
$member->setBody(null);
325+
}
326+
$this->methods[$member->getName()] = $member->setNamespace($this->namespace);
327+
328+
} elseif ($member instanceof Property) {
329+
$this->properties[$member->getName()] = $member;
330+
331+
} elseif ($member instanceof Constant) {
332+
$this->consts[$member->getName()] = $member;
333+
334+
} else {
335+
throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant.');
336+
}
337+
338+
return $this;
339+
}
340+
341+
316342
/**
317343
* @param Constant[]|mixed[] $consts
318344
* @return static
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\ClassType;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::exception(function () {
13+
(new ClassType('Example'))
14+
->addMember(new stdClass);
15+
}, Nette\InvalidArgumentException::class, 'Argument must be Method|Property|Constant.');
16+
17+
18+
$class = (new ClassType('Example'))
19+
->addMember($method = new Nette\PhpGenerator\Method('getHandle'))
20+
->addMember($property = new Nette\PhpGenerator\Property('handle'))
21+
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'));
22+
23+
Assert::same(['getHandle' => $method], $class->getMethods());
24+
Assert::same(['handle' => $property], $class->getProperties());
25+
Assert::same(['ROLE' => $const], $class->getConstants());
26+
Assert::same('', $method->getBody());
27+
28+
29+
$class = (new ClassType('Example'))
30+
->setType('interface')
31+
->addMember($method = new Nette\PhpGenerator\Method('getHandle'));
32+
33+
Assert::null($method->getBody());

0 commit comments

Comments
 (0)