Skip to content

Commit 734f814

Browse files
committed
VisibilityAware: added new setter/getters for each visiblity
1 parent 48b8c53 commit 734f814

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ We can add constants and properties:
7878
$class->addConstant('ID', 123);
7979

8080
$class->addProperty('items', [1, 2, 3])
81-
->setVisibility('private')
81+
->setPrivate()
8282
->setStatic()
8383
->addComment('@var int[]');
8484
```
@@ -99,7 +99,7 @@ $method = $class->addMethod('count')
9999
->addComment('Count it.')
100100
->addComment('@return int')
101101
->setFinal()
102-
->setVisibility('protected')
102+
->setProtected()
103103
->setBody('return count($items ?: $this->items);');
104104

105105
$method->addParameter('items', []) // $items = []
@@ -130,7 +130,7 @@ PHP Generator supports all new PHP 7.3 and 7.4 features:
130130
$class = new Nette\PhpGenerator\ClassType('Demo');
131131

132132
$class->addConstant('ID', 123)
133-
->setVisibility('private'); // constant visiblity
133+
->setPrivate(); // constant visiblity
134134

135135
$class->addProperty('items')
136136
->setType('array') // typed properites

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public function addMethod(string $name): Method
421421
if ($this->type === self::TYPE_INTERFACE) {
422422
$method->setBody(null);
423423
} else {
424-
$method->setVisibility(self::VISIBILITY_PUBLIC);
424+
$method->setPublic();
425425
}
426426
return $this->methods[$name] = $method;
427427
}

src/PhpGenerator/Traits/VisibilityAware.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,46 @@ public function getVisibility(): ?string
4040
{
4141
return $this->visibility;
4242
}
43+
44+
45+
/** @return static */
46+
public function setPublic(): self
47+
{
48+
$this->visibility = ClassType::VISIBILITY_PUBLIC;
49+
return $this;
50+
}
51+
52+
53+
public function isPublic(): bool
54+
{
55+
return $this->visibility === ClassType::VISIBILITY_PUBLIC || $this->visibility === null;
56+
}
57+
58+
59+
/** @return static */
60+
public function setProtected(): self
61+
{
62+
$this->visibility = ClassType::VISIBILITY_PROTECTED;
63+
return $this;
64+
}
65+
66+
67+
public function isProtected(): bool
68+
{
69+
return $this->visibility === ClassType::VISIBILITY_PROTECTED;
70+
}
71+
72+
73+
/** @return static */
74+
public function setPrivate(): self
75+
{
76+
$this->visibility = ClassType::VISIBILITY_PRIVATE;
77+
return $this;
78+
}
79+
80+
81+
public function isPrivate(): bool
82+
{
83+
return $this->visibility === ClassType::VISIBILITY_PRIVATE;
84+
}
4385
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY
4747
->addComment('Commented');
4848

4949
$class->addProperty('handle')
50-
->setVisibility('private')
50+
->setPrivate()
5151
->addComment('@var resource orignal file handle');
5252

5353
$class->addProperty('order')
@@ -69,6 +69,9 @@ Assert::true($class->hasProperty('sections'));
6969
Assert::false($class->hasProperty('unknown'));
7070
Assert::true($p->isStatic());
7171
Assert::null($p->getVisibility());
72+
Assert::false($p->isPrivate());
73+
Assert::false($p->isProtected());
74+
Assert::true($p->isPublic());
7275

7376
$m = $class->addMethod('getHandle')
7477
->addComment('Returns file handle.')
@@ -100,6 +103,9 @@ Assert::true($m->getReturnReference());
100103
Assert::false($m->isReturnNullable());
101104
Assert::null($m->getReturnType());
102105
Assert::same('protected', $m->getVisibility());
106+
Assert::false($m->isPrivate());
107+
Assert::true($m->isProtected());
108+
Assert::false($m->isPublic());
103109

104110
$method = $class->addMethod('show')
105111
->setAbstract(true);

tests/PhpGenerator/Printer.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $class = (new ClassType('Example'))
2323
->addComment("Description of class.\nThis is example\n");
2424

2525
$class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY'))
26-
->setVisibility('private')
26+
->setPrivate()
2727
->addComment('Commented');
2828

2929
$class->addConstant('MULTILINE_LONG', ['aaaaaaaa' => 1, 'bbbbbbbb' => 2, 'cccccccc' => 3, 'dddddddd' => 4, 'eeeeeeee' => 5, 'ffffffff' => 6]);

0 commit comments

Comments
 (0)