Skip to content

Commit 3122e00

Browse files
committed
ClassType: setInterface() & setTrait() replaced with interface() & trait() factories
1 parent eda0cec commit 3122e00

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ echo $printer->printClass($class); // 4 spaces indentation
219219
Interface or Trait
220220
------------------
221221

222-
You can create interfaces and traits in a similar way, just change the type:
222+
You can create interfaces and traits:
223223

224224
```php
225-
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
226-
$class->setInterface();
227-
// or $class->setTrait();
225+
$interface = Nette\PhpGenerator\ClassType::interface('MyInterface');
226+
$trait = Nette\PhpGenerator\ClassType::trait('MyTrait');
227+
// in a similar way $class = Nette\PhpGenerator\ClassType::class('MyClass');
228228
```
229229

230230
Literals

src/PhpGenerator/ClassType.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ final class ClassType
6868
private $methods = [];
6969

7070

71+
public static function class(string $name = null, PhpNamespace $namespace = null): self
72+
{
73+
return new self($name, $namespace);
74+
}
75+
76+
77+
public static function interface(string $name = null, PhpNamespace $namespace = null): self
78+
{
79+
return (new self($name, $namespace))->setType(self::TYPE_INTERFACE);
80+
}
81+
82+
83+
public static function trait(string $name = null, PhpNamespace $namespace = null): self
84+
{
85+
return (new self($name, $namespace))->setType(self::TYPE_TRAIT);
86+
}
87+
88+
7189
/**
7290
* @param string|object $class
7391
*/
@@ -131,7 +149,7 @@ public function getName(): ?string
131149
}
132150

133151

134-
/** @return static */
152+
/** @deprecated */
135153
public function setClass(): self
136154
{
137155
$this->type = self::TYPE_CLASS;

src/PhpGenerator/PhpNamespace.php

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

180180
public function addInterface(string $name): ClassType
181181
{
182-
return $this->addClass($name)->setInterface();
182+
return $this->addClass($name)->setType(ClassType::TYPE_INTERFACE);
183183
}
184184

185185

186186
public function addTrait(string $name): ClassType
187187
{
188-
return $this->addClass($name)->setTrait();
188+
return $this->addClass($name)->setType(ClassType::TYPE_TRAIT);
189189
}
190190

191191

tests/PhpGenerator/ClassType.interface.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use Tester\Assert;
1313
require __DIR__ . '/../bootstrap.php';
1414

1515

16-
$interface = new ClassType('IExample');
16+
$interface = ClassType::interface('IExample');
1717
$interface
18-
->setInterface()
1918
->addExtend('IOne')
2019
->addExtend('ITwo')
2120
->addComment('Description of interface');

0 commit comments

Comments
 (0)