Skip to content

Commit c30e587

Browse files
committed
ClassType: added new setter/getters for each type
1 parent 734f814 commit c30e587

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ Interface or Trait
238238

239239
```php
240240
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
241-
$class->setType('interface');
242-
// or $class->setType('trait');
241+
$class->setInterface();
242+
// or $class->setTrait();
243243
```
244244

245245
Trait Resolutions and Visibility

src/PhpGenerator/ClassType.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,48 @@ public function getName(): ?string
121121
}
122122

123123

124+
/** @return static */
125+
public function setClass(): self
126+
{
127+
$this->type = self::TYPE_CLASS;
128+
return $this;
129+
}
130+
131+
132+
public function isClass(): bool
133+
{
134+
return $this->type === self::TYPE_CLASS;
135+
}
136+
137+
138+
/** @return static */
139+
public function setInterface(): self
140+
{
141+
$this->type = self::TYPE_INTERFACE;
142+
return $this;
143+
}
144+
145+
146+
public function isInterface(): bool
147+
{
148+
return $this->type === self::TYPE_INTERFACE;
149+
}
150+
151+
152+
/** @return static */
153+
public function setTrait(): self
154+
{
155+
$this->type = self::TYPE_TRAIT;
156+
return $this;
157+
}
158+
159+
160+
public function isTrait(): bool
161+
{
162+
return $this->type === self::TYPE_TRAIT;
163+
}
164+
165+
124166
/** @return static */
125167
public function setType(string $type): self
126168
{
@@ -268,7 +310,7 @@ public function addTrait(string $name, array $resolutions = []): self
268310
public function addMember($member): self
269311
{
270312
if ($member instanceof Method) {
271-
if ($this->type === self::TYPE_INTERFACE) {
313+
if ($this->isInterface()) {
272314
$member->setBody(null);
273315
}
274316
$this->methods[$member->getName()] = $member;
@@ -418,7 +460,7 @@ public function getMethod(string $name): Method
418460
public function addMethod(string $name): Method
419461
{
420462
$method = new Method($name);
421-
if ($this->type === self::TYPE_INTERFACE) {
463+
if ($this->isInterface()) {
422464
$method->setBody(null);
423465
} else {
424466
$method->setPublic();

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function fromClassReflection(\ReflectionClass $from): ClassType
2525
? new ClassType
2626
: new ClassType($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
2727
$class->setType($from->isInterface() ? $class::TYPE_INTERFACE : ($from->isTrait() ? $class::TYPE_TRAIT : $class::TYPE_CLASS));
28-
$class->setFinal($from->isFinal() && $class->getType() === $class::TYPE_CLASS);
29-
$class->setAbstract($from->isAbstract() && $class->getType() === $class::TYPE_CLASS);
28+
$class->setFinal($from->isFinal() && $class->isClass());
29+
$class->setAbstract($from->isAbstract() && $class->isClass());
3030

3131
$ifaces = $from->getInterfaceNames();
3232
foreach ($ifaces as $iface) {

src/PhpGenerator/PhpNamespace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ public function addClass(string $name): ClassType
172172

173173
public function addInterface(string $name): ClassType
174174
{
175-
return $this->addClass($name)->setType(ClassType::TYPE_INTERFACE);
175+
return $this->addClass($name)->setInterface();
176176
}
177177

178178

179179
public function addTrait(string $name): ClassType
180180
{
181-
return $this->addClass($name)->setType(ClassType::TYPE_TRAIT);
181+
return $this->addClass($name)->setTrait();
182182
}
183183

184184

tests/PhpGenerator/ClassType.interface.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';
1515

1616
$interface = new ClassType('IExample');
1717
$interface
18-
->setType('interface')
18+
->setInterface()
1919
->addExtend('IOne')
2020
->addExtend('ITwo')
2121
->addComment('Description of interface');

tests/PhpGenerator/ClassType.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ $class = new ClassType('Example');
1818

1919
Assert::false($class->isFinal());
2020
Assert::false($class->isAbstract());
21+
Assert::true($class->isClass());
22+
Assert::false($class->isInterface());
23+
Assert::false($class->isTrait());
2124
Assert::same([], $class->getExtends());
2225
Assert::same([], $class->getTraits());
2326
Assert::same([], $class->getTraitResolutions());

0 commit comments

Comments
 (0)