Skip to content

Commit 4707546

Browse files
committed
Visibility, PropertyHookType & PropertyAccessMode are enums (BC break)
1 parent 2fc2add commit 4707546

File tree

7 files changed

+57
-100
lines changed

7 files changed

+57
-100
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ private function toValue(Node\Expr $node): mixed
548548
}
549549

550550

551-
private function toVisibility(int $flags): ?string
551+
private function toVisibility(int $flags): ?Visibility
552552
{
553553
return match (true) {
554554
(bool) ($flags & Modifiers::PUBLIC) => Visibility::Public,
@@ -559,7 +559,7 @@ private function toVisibility(int $flags): ?string
559559
}
560560

561561

562-
private function toSetterVisibility(int $flags): ?string
562+
private function toSetterVisibility(int $flags): ?Visibility
563563
{
564564
return match (true) {
565565
!class_exists(Node\PropertyHook::class) => null,

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function fromClassReflection(
117117
}
118118

119119
$modifier = $declaringMethod->getModifiers() !== $method->getModifiers()
120-
? ' ' . $this->getVisibility($method)
120+
? ' ' . $this->getVisibility($method)->value
121121
: null;
122122
$alias = $declaringMethod->name !== $method->name ? ' ' . $method->name : '';
123123
if ($modifier || $alias) {
@@ -361,7 +361,7 @@ private function getAttributes($from): array
361361
}
362362

363363

364-
private function getVisibility(\ReflectionProperty|\ReflectionMethod|\ReflectionClassConstant $from): string
364+
private function getVisibility(\ReflectionProperty|\ReflectionMethod|\ReflectionClassConstant $from): Visibility
365365
{
366366
return $from->isPrivate()
367367
? Visibility::Private

src/PhpGenerator/PropertyAccessMode.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,12 @@
99

1010
namespace Nette\PhpGenerator;
1111

12-
use Nette;
13-
1412

1513
/**
1614
* Property access mode.
1715
*/
18-
/*enum*/ final class PropertyAccessMode
16+
enum PropertyAccessMode: string
1917
{
20-
use Nette\StaticClass;
21-
22-
public const Set = 'set';
23-
public const Get = 'get';
24-
25-
26-
/** @internal */
27-
public static function from(string $value): string
28-
{
29-
return $value === self::Set || $value === self::Get
30-
? $value
31-
: throw new \ValueError("'$value' is not a valid value of access mode");
32-
}
18+
case Set = 'set';
19+
case Get = 'get';
3320
}

src/PhpGenerator/PropertyHookType.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,12 @@
99

1010
namespace Nette\PhpGenerator;
1111

12-
use Nette;
13-
1412

1513
/**
1614
* Property hook type.
1715
*/
18-
/*enum*/ final class PropertyHookType
16+
enum PropertyHookType: string
1917
{
20-
use Nette\StaticClass;
21-
22-
public const Set = 'set';
23-
public const Get = 'get';
24-
25-
26-
/** @internal */
27-
public static function from(string $value): string
28-
{
29-
return $value === self::Set || $value === self::Get
30-
? $value
31-
: throw new \ValueError("'$value' is not a valid value of hook type");
32-
}
18+
case Set = 'set';
19+
case Get = 'get';
3320
}

src/PhpGenerator/Traits/PropertyLike.php

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,82 @@
1313
use Nette\PhpGenerator\PropertyHook;
1414
use Nette\PhpGenerator\PropertyHookType;
1515
use Nette\PhpGenerator\Visibility;
16-
use function array_filter, in_array;
16+
use function array_filter, in_array, is_string;
1717

1818

1919
/**
2020
* @internal
2121
*/
2222
trait PropertyLike
2323
{
24-
/** @var array{set: ?string, get: ?string} */
25-
private array $visibility = [PropertyAccessMode::Set => null, PropertyAccessMode::Get => null];
24+
/** @var array{set: ?Visibility, get: ?Visibility} */
25+
private array $visibility = ['set' => null, 'get' => null];
2626
private bool $final = false;
2727
private bool $readOnly = false;
2828

2929
/** @var array<string, ?PropertyHook> */
30-
private array $hooks = [PropertyHookType::Set => null, PropertyHookType::Get => null];
30+
private array $hooks = ['set' => null, 'get' => null];
3131

3232

33-
/**
34-
* @param 'public'|'protected'|'private'|null $get
35-
* @param 'public'|'protected'|'private'|null $set
36-
*/
37-
public function setVisibility(?string $get, ?string $set = null): static
33+
public function setVisibility(Visibility|string|null $get, Visibility|string|null $set = null): static
3834
{
3935
$this->visibility = [
40-
PropertyAccessMode::Set => $set === null ? $set : Visibility::from($set),
41-
PropertyAccessMode::Get => $get === null ? $get : Visibility::from($get),
36+
'set' => $set instanceof Visibility || $set === null ? $set : Visibility::from($set),
37+
'get' => $get instanceof Visibility || $get === null ? $get : Visibility::from($get),
4238
];
4339
return $this;
4440
}
4541

4642

47-
/** @param 'set'|'get' $mode */
48-
public function getVisibility(string $mode = PropertyAccessMode::Get): ?string
43+
public function getVisibility(PropertyAccessMode|string $mode = PropertyAccessMode::Get): ?string
4944
{
50-
return $this->visibility[PropertyAccessMode::from($mode)];
45+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
46+
return $this->visibility[$mode->value]?->value;
5147
}
5248

5349

54-
/** @param 'set'|'get' $mode */
55-
public function setPublic(string $mode = PropertyAccessMode::Get): static
50+
public function setPublic(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static
5651
{
57-
$this->visibility[PropertyAccessMode::from($mode)] = Visibility::Public;
52+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
53+
$this->visibility[$mode->value] = Visibility::Public;
5854
return $this;
5955
}
6056

6157

62-
/** @param 'set'|'get' $mode */
63-
public function isPublic(string $mode = PropertyAccessMode::Get): bool
58+
public function isPublic(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool
6459
{
65-
return in_array($this->visibility[PropertyAccessMode::from($mode)], [Visibility::Public, null], true);
60+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
61+
return in_array($this->visibility[$mode->value], [Visibility::Public, null], true);
6662
}
6763

6864

69-
/** @param 'set'|'get' $mode */
70-
public function setProtected(string $mode = PropertyAccessMode::Get): static
65+
public function setProtected(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static
7166
{
72-
$this->visibility[PropertyAccessMode::from($mode)] = Visibility::Protected;
67+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
68+
$this->visibility[$mode->value] = Visibility::Protected;
7369
return $this;
7470
}
7571

7672

77-
/** @param 'set'|'get' $mode */
78-
public function isProtected(string $mode = PropertyAccessMode::Get): bool
73+
public function isProtected(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool
7974
{
80-
return $this->visibility[PropertyAccessMode::from($mode)] === Visibility::Protected;
75+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
76+
return $this->visibility[$mode->value] === Visibility::Protected;
8177
}
8278

8379

84-
/** @param 'set'|'get' $mode */
85-
public function setPrivate(string $mode = PropertyAccessMode::Get): static
80+
public function setPrivate(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static
8681
{
87-
$this->visibility[PropertyAccessMode::from($mode)] = Visibility::Private;
82+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
83+
$this->visibility[$mode->value] = Visibility::Private;
8884
return $this;
8985
}
9086

9187

92-
/** @param 'set'|'get' $mode */
93-
public function isPrivate(string $mode = PropertyAccessMode::Get): bool
88+
public function isPrivate(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool
9489
{
95-
return $this->visibility[PropertyAccessMode::from($mode)] === Visibility::Private;
90+
$mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode;
91+
return $this->visibility[$mode->value] === Visibility::Private;
9692
}
9793

9894

@@ -141,24 +137,24 @@ public function getHooks(): array
141137
}
142138

143139

144-
/** @param 'set'|'get' $type */
145-
public function addHook(string $type, string $shortBody = ''): PropertyHook
140+
public function addHook(PropertyHookType|string $type, string $shortBody = ''): PropertyHook
146141
{
147-
return $this->hooks[PropertyHookType::from($type)] = (new PropertyHook)
142+
$type = is_string($type) ? PropertyHookType::from($type) : $type;
143+
return $this->hooks[$type->value] = (new PropertyHook)
148144
->setBody($shortBody, short: true);
149145
}
150146

151147

152-
/** @param 'set'|'get' $type */
153-
public function getHook(string $type): ?PropertyHook
148+
public function getHook(PropertyHookType|string $type): ?PropertyHook
154149
{
155-
return $this->hooks[PropertyHookType::from($type)] ?? null;
150+
$type = is_string($type) ? PropertyHookType::from($type) : $type;
151+
return $this->hooks[$type->value] ?? null;
156152
}
157153

158154

159-
/** @param 'set'|'get' $type */
160-
public function hasHook(string $type): bool
155+
public function hasHook(PropertyHookType|string $type): bool
161156
{
162-
return isset($this->hooks[PropertyHookType::from($type)]);
157+
$type = is_string($type) ? PropertyHookType::from($type) : $type;
158+
return isset($this->hooks[$type->value]);
163159
}
164160
}

src/PhpGenerator/Traits/VisibilityAware.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
*/
1818
trait VisibilityAware
1919
{
20-
/** public|protected|private */
21-
private ?string $visibility = null;
20+
private ?Visibility $visibility = null;
2221

2322

24-
/** @param 'public'|'protected'|'private'|null $value */
25-
public function setVisibility(?string $value): static
23+
public function setVisibility(Visibility|string|null $value): static
2624
{
27-
$this->visibility = $value === null ? $value : Visibility::from($value);
25+
$this->visibility = $value instanceof Visibility || $value === null
26+
? $value
27+
: Visibility::from($value);
2828
return $this;
2929
}
3030

3131

3232
public function getVisibility(): ?string
3333
{
34-
return $this->visibility;
34+
return $this->visibility?->value;
3535
}
3636

3737

src/PhpGenerator/Visibility.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,13 @@
99

1010
namespace Nette\PhpGenerator;
1111

12-
use Nette;
13-
1412

1513
/**
1614
* Member visibility.
1715
*/
18-
/*enum*/ final class Visibility
16+
enum Visibility: string
1917
{
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-
}
18+
case Public = 'public';
19+
case Protected = 'protected';
20+
case Private = 'private';
3421
}

0 commit comments

Comments
 (0)