Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5711b3

Browse files
committed
added Visibility
1 parent 09d7a2b commit a5711b3

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
@@ -20,19 +20,17 @@ abstract class ClassLike
2020
use Traits\CommentAware;
2121
use Traits\AttributeAware;
2222

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

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

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

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

‎src/PhpGenerator/Extractor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ private function toValue(Node\Expr $node): mixed
483483
private function toVisibility(int $flags): ?string
484484
{
485485
return match (true) {
486-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic,
487-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected,
488-
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate,
486+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public,
487+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected,
488+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private,
489489
default => null,
490490
};
491491
}

‎src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ private function getAttributes($from): array
307307
private function getVisibility($from): string
308308
{
309309
return $from->isPrivate()
310-
? ClassLike::VisibilityPrivate
311-
: ($from->isProtected() ? ClassLike::VisibilityProtected : ClassLike::VisibilityPublic);
310+
? Visibility::Private
311+
: ($from->isProtected() ? Visibility::Protected : Visibility::Public);
312312
}
313313

314314

‎src/PhpGenerator/Method.php

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

‎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
@@ -177,8 +177,7 @@ Assert::same($parameters, $method->getParameters());
177177

178178
Assert::exception(
179179
fn() => (new ClassType)->addMethod('method')->setVisibility('unknown'),
180-
Nette\InvalidArgumentException::class,
181-
'Argument must be public|protected|private.',
180+
ValueError::class,
182181
);
183182

184183

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /