Skip to content

Commit 16a6b2b

Browse files
committed
used constants
1 parent 3fa9984 commit 16a6b2b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class ClassType
4141
private $name;
4242

4343
/** @var string class|interface|trait */
44-
private $type = 'class';
44+
private $type = self::TYPE_CLASS;
4545

4646
/** @var bool */
4747
private $final = false;
@@ -158,7 +158,7 @@ public function getName(): ?string
158158
*/
159159
public function setType(string $type): self
160160
{
161-
if (!in_array($type, ['class', 'interface', 'trait'], true)) {
161+
if (!in_array($type, [self::TYPE_CLASS, self::TYPE_INTERFACE, self::TYPE_TRAIT], true)) {
162162
throw new Nette\InvalidArgumentException('Argument must be class|interface|trait.');
163163
}
164164
$this->type = $type;
@@ -425,10 +425,10 @@ public function getMethod(string $name): Method
425425
public function addMethod(string $name): Method
426426
{
427427
$method = (new Method($name))->setNamespace($this->namespace);
428-
if ($this->type === 'interface') {
428+
if ($this->type === self::TYPE_INTERFACE) {
429429
$method->setBody(null);
430430
} else {
431-
$method->setVisibility('public');
431+
$method->setVisibility(self::VISIBILITY_PUBLIC);
432432
}
433433
return $this->methods[$name] = $method;
434434
}

src/PhpGenerator/Factory.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function fromClassReflection(\ReflectionClass $from): ClassType
2424
$class = $from->isAnonymous()
2525
? new ClassType
2626
: new ClassType($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
27-
$class->setType($from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class'));
28-
$class->setFinal($from->isFinal() && $class->getType() === 'class');
29-
$class->setAbstract($from->isAbstract() && $class->getType() === 'class');
27+
$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);
3030

3131
$ifaces = $from->getInterfaceNames();
3232
foreach ($ifaces as $iface) {
@@ -65,7 +65,10 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
6565
$method->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
6666
$method->setStatic($from->isStatic());
6767
$isInterface = $from->getDeclaringClass()->isInterface();
68-
$method->setVisibility($from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ($isInterface ? null : 'public')));
68+
$method->setVisibility($from->isPrivate()
69+
? ClassType::VISIBILITY_PRIVATE
70+
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ($isInterface ? null : ClassType::VISIBILITY_PUBLIC))
71+
);
6972
$method->setFinal($from->isFinal());
7073
$method->setAbstract($from->isAbstract() && !$isInterface);
7174
$method->setBody($from->isAbstract() ? null : '');
@@ -121,7 +124,10 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
121124
$prop = new Property($from->getName());
122125
$prop->setValue($from->getDeclaringClass()->getDefaultProperties()[$prop->getName()] ?? null);
123126
$prop->setStatic($from->isStatic());
124-
$prop->setVisibility($from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public'));
127+
$prop->setVisibility($from->isPrivate()
128+
? ClassType::VISIBILITY_PRIVATE
129+
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
130+
);
125131
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
126132
return $prop;
127133
}

src/PhpGenerator/Traits/VisibilityAware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\PhpGenerator\Traits;
1111

1212
use Nette;
13+
use Nette\PhpGenerator\ClassType;
1314

1415

1516
/**
@@ -27,7 +28,7 @@ trait VisibilityAware
2728
*/
2829
public function setVisibility(?string $val): self
2930
{
30-
if (!in_array($val, ['public', 'protected', 'private', null], true)) {
31+
if (!in_array($val, [ClassType::VISIBILITY_PUBLIC, ClassType::VISIBILITY_PROTECTED, ClassType::VISIBILITY_PRIVATE, null], true)) {
3132
throw new Nette\InvalidArgumentException('Argument must be public|protected|private.');
3233
}
3334
$this->visibility = $val;

0 commit comments

Comments
 (0)