Skip to content

Commit bad08ce

Browse files
committed
added PropertyLike
1 parent a5711b3 commit bad08ce

File tree

4 files changed

+115
-32
lines changed

4 files changed

+115
-32
lines changed

src/PhpGenerator/PromotedParameter.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,7 @@
1717
*/
1818
final class PromotedParameter extends Parameter
1919
{
20-
use Traits\VisibilityAware;
21-
22-
private bool $readOnly = false;
23-
24-
25-
public function setReadOnly(bool $state = true): static
26-
{
27-
$this->readOnly = $state;
28-
return $this;
29-
}
30-
31-
32-
public function isReadOnly(): bool
33-
{
34-
return $this->readOnly;
35-
}
36-
20+
use Traits\PropertyLike;
3721

3822
/** @throws Nette\InvalidStateException */
3923
public function validate(): void

src/PhpGenerator/Property.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
final class Property
2020
{
2121
use Traits\NameAware;
22-
use Traits\VisibilityAware;
22+
use Traits\PropertyLike;
2323
use Traits\CommentAware;
2424
use Traits\AttributeAware;
2525

@@ -28,7 +28,6 @@ final class Property
2828
private ?string $type = null;
2929
private bool $nullable = false;
3030
private bool $initialized = false;
31-
private bool $readOnly = false;
3231

3332

3433
public function setValue(mixed $val): static
@@ -100,19 +99,6 @@ public function isInitialized(): bool
10099
}
101100

102101

103-
public function setReadOnly(bool $state = true): static
104-
{
105-
$this->readOnly = $state;
106-
return $this;
107-
}
108-
109-
110-
public function isReadOnly(): bool
111-
{
112-
return $this->readOnly;
113-
}
114-
115-
116102
/** @throws Nette\InvalidStateException */
117103
public function validate(): void
118104
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\Traits;
11+
12+
13+
14+
/**
15+
* @internal
16+
*/
17+
trait PropertyLike
18+
{
19+
use VisibilityAware;
20+
21+
private bool $readOnly = false;
22+
23+
24+
public function setReadOnly(bool $state = true): static
25+
{
26+
$this->readOnly = $state;
27+
return $this;
28+
}
29+
30+
31+
public function isReadOnly(): bool
32+
{
33+
return $this->readOnly;
34+
}
35+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Test: PropertyLike visibility
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\ClassType;
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$class = new ClassType('Demo');
16+
17+
// Default visibility (public)
18+
$default = $class->addProperty('first')
19+
->setType('string');
20+
Assert::true($default->isPublic());
21+
Assert::false($default->isProtected());
22+
Assert::false($default->isPrivate());
23+
Assert::null($default->getVisibility());
24+
25+
// Explicit public
26+
$public = $class->addProperty('second')
27+
->setType('string')
28+
->setPublic();
29+
Assert::true($public->isPublic());
30+
Assert::false($public->isProtected());
31+
Assert::false($public->isPrivate());
32+
Assert::same('public', $public->getVisibility());
33+
34+
// Protected
35+
$protected = $class->addProperty('third')
36+
->setType('string')
37+
->setProtected();
38+
Assert::false($protected->isPublic());
39+
Assert::true($protected->isProtected());
40+
Assert::false($protected->isPrivate());
41+
Assert::same('protected', $protected->getVisibility());
42+
43+
// Private
44+
$private = $class->addProperty('fourth')
45+
->setType('string')
46+
->setPrivate();
47+
Assert::false($private->isPublic());
48+
Assert::false($private->isProtected());
49+
Assert::true($private->isPrivate());
50+
Assert::same('private', $private->getVisibility());
51+
52+
// Change visibility
53+
$changing = $class->addProperty('fifth')
54+
->setType('string')
55+
->setPublic();
56+
$changing->setVisibility('protected');
57+
Assert::false($changing->isPublic());
58+
Assert::true($changing->isProtected());
59+
Assert::false($changing->isPrivate());
60+
61+
// Test invalid visibility
62+
Assert::exception(
63+
fn() => $changing->setVisibility('invalid'),
64+
Nette\InvalidArgumentException::class,
65+
'Argument must be public|protected|private.',
66+
);
67+
68+
same(<<<'XX'
69+
class Demo
70+
{
71+
public string $first;
72+
public string $second;
73+
protected string $third;
74+
private string $fourth;
75+
protected string $fifth;
76+
}
77+
78+
XX, (string) $class);

0 commit comments

Comments
 (0)