Skip to content

Commit 132099e

Browse files
committed
added Visibility
1 parent d0c7e3e commit 132099e

File tree

7 files changed

+60
-36
lines changed

7 files changed

+60
-36
lines changed

src/PhpGenerator/ClassLike.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ abstract class ClassLike
2121
use Traits\CommentAware;
2222
use Traits\AttributeAware;
2323

24-
public const
25-
VisibilityPublic = 'public',
26-
VisibilityProtected = 'protected',
27-
VisibilityPrivate = 'private';
24+
/** @deprecated use Visibility::Public */
25+
public const VisibilityPublic = Visibility::Public,
26+
VISIBILITY_PUBLIC = Visibility::Public;
2827

29-
/** @deprecated use ClassLike::VisibilityPublic */
30-
public const VISIBILITY_PUBLIC = self::VisibilityPublic;
28+
/** @deprecated use Visibility::Protected */
29+
public const VisibilityProtected = Visibility::Protected,
30+
VISIBILITY_PROTECTED = Visibility::Protected;
3131

32-
/** @deprecated use ClassLike::VisibilityProtected */
33-
public const VISIBILITY_PROTECTED = self::VisibilityProtected;
34-
35-
/** @deprecated use ClassLike::VisibilityPrivate */
36-
public const VISIBILITY_PRIVATE = self::VisibilityPrivate;
32+
/** @deprecated use Visibility::Private */
33+
public const VisibilityPrivate = Visibility::Private,
34+
VISIBILITY_PRIVATE = Visibility::Private;
3735

3836
private ?PhpNamespace $namespace;
3937
private ?string $name;

src/PhpGenerator/Extractor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,9 @@ private function formatValue(Node\Expr $value, int $level): Literal
428428
private function toVisibility(int $flags): ?string
429429
{
430430
return match (true) {
431-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic,
432-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected,
433-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate,
431+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public,
432+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected,
433+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private,
434434
default => null,
435435
};
436436
}

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ private function getAttributes($from): array
310310
private function getVisibility($from): string
311311
{
312312
return $from->isPrivate()
313-
? ClassLike::VisibilityPrivate
314-
: ($from->isProtected() ? ClassLike::VisibilityProtected : ClassLike::VisibilityPublic);
313+
? Visibility::Private
314+
: ($from->isProtected() ? Visibility::Protected : Visibility::Public);
315315
}
316316

317317

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function addPromotedParameter(string $name, mixed $defaultValue = null):
9999
/** @throws Nette\InvalidStateException */
100100
public function validate(): void
101101
{
102-
if ($this->abstract && ($this->final || $this->visibility === ClassLike::VisibilityPrivate)) {
102+
if ($this->abstract && ($this->final || $this->visibility === Visibility::Private)) {
103103
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
104104
}
105105
}

src/PhpGenerator/Traits/VisibilityAware.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
namespace Nette\PhpGenerator\Traits;
1111

12-
use Nette;
13-
use Nette\PhpGenerator\ClassLike;
12+
use Nette\PhpGenerator\Visibility;
1413

1514

1615
/**
@@ -22,16 +21,10 @@ trait VisibilityAware
2221
private ?string $visibility = null;
2322

2423

25-
/**
26-
* @param string|null $val public|protected|private
27-
*/
28-
public function setVisibility(?string $val): static
24+
/** @param 'public'|'protected'|'private'|null $value */
25+
public function setVisibility(?string $value): static
2926
{
30-
if (!in_array($val, [ClassLike::VisibilityPublic, ClassLike::VisibilityProtected, ClassLike::VisibilityPrivate, null], true)) {
31-
throw new Nette\InvalidArgumentException('Argument must be public|protected|private.');
32-
}
33-
34-
$this->visibility = $val;
27+
$this->visibility = $value === null ? $value : Visibility::from($value);
3528
return $this;
3629
}
3730

@@ -44,39 +37,39 @@ public function getVisibility(): ?string
4437

4538
public function setPublic(): static
4639
{
47-
$this->visibility = ClassLike::VisibilityPublic;
40+
$this->visibility = Visibility::Public;
4841
return $this;
4942
}
5043

5144

5245
public function isPublic(): bool
5346
{
54-
return $this->visibility === ClassLike::VisibilityPublic || $this->visibility === null;
47+
return $this->visibility === Visibility::Public || $this->visibility === null;
5548
}
5649

5750

5851
public function setProtected(): static
5952
{
60-
$this->visibility = ClassLike::VisibilityProtected;
53+
$this->visibility = Visibility::Protected;
6154
return $this;
6255
}
6356

6457

6558
public function isProtected(): bool
6659
{
67-
return $this->visibility === ClassLike::VisibilityProtected;
60+
return $this->visibility === Visibility::Protected;
6861
}
6962

7063

7164
public function setPrivate(): static
7265
{
73-
$this->visibility = ClassLike::VisibilityPrivate;
66+
$this->visibility = Visibility::Private;
7467
return $this;
7568
}
7669

7770

7871
public function isPrivate(): bool
7972
{
80-
return $this->visibility === ClassLike::VisibilityPrivate;
73+
return $this->visibility === Visibility::Private;
8174
}
8275
}

src/PhpGenerator/Visibility.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
use Nette;
13+
14+
15+
/**
16+
* Member visibility.
17+
*/
18+
/*enum*/ final class Visibility
19+
{
20+
use Nette\StaticClass;
21+
22+
public const Public = 'public';
23+
public const Protected = 'protected';
24+
public const Private = 'private';
25+
26+
27+
/** @internal */
28+
public static function from(string $value): string
29+
{
30+
return $value === self::Public || $value === self::Protected || $value === self::Private
31+
? $value
32+
: throw new \ValueError("'$value' is not a valid value of visibility");
33+
}
34+
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ Assert::same($parameters, $method->getParameters());
175175

176176
Assert::exception(
177177
fn() => (new ClassType)->addMethod('method')->setVisibility('unknown'),
178-
Nette\InvalidArgumentException::class,
179-
'Argument must be public|protected|private.',
178+
ValueError::class,
180179
);
181180

182181

0 commit comments

Comments
 (0)