-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for asymmetric visibility
- Loading branch information
Showing
5 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\PhpGenerator; | ||
|
||
use Nette; | ||
|
||
|
||
/** | ||
* Property access mode. | ||
*/ | ||
/*enum*/ final class PropertyAccessMode | ||
{ | ||
use Nette\StaticClass; | ||
|
||
public const Set = 'set'; | ||
public const Get = 'get'; | ||
|
||
|
||
/** @internal */ | ||
public static function from(string $value): string | ||
{ | ||
return $value === self::Set || $value === self::Get | ||
? $value | ||
: throw new \ValueError("'$value' is not a valid value of access mode"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* Test: PropertyLike asymmetric visibility | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PropertyAccessMode; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$class = new ClassType('Demo'); | ||
|
||
// Default visibility | ||
$default = $class->addProperty('first') | ||
->setType('string'); | ||
Assert::true($default->isPublic(PropertyAccessMode::Get)); | ||
Assert::true($default->isPublic(PropertyAccessMode::Set)); | ||
Assert::null($default->getVisibility()); | ||
Assert::null($default->getVisibility('set')); | ||
|
||
// Public with private setter | ||
$restricted = $class->addProperty('second') | ||
->setType('string') | ||
->setVisibility(null, 'private'); | ||
Assert::true($restricted->isPublic()); | ||
Assert::false($restricted->isPublic('set')); | ||
Assert::true($restricted->isPrivate('set')); | ||
Assert::null($restricted->getVisibility()); | ||
Assert::same('private', $restricted->getVisibility('set')); | ||
|
||
// Public with protected setter using individual methods | ||
$mixed = $class->addProperty('third') | ||
->setType('string') | ||
->setPublic() | ||
->setProtected('set'); | ||
Assert::true($mixed->isPublic()); | ||
Assert::false($mixed->isPublic('set')); | ||
Assert::true($mixed->isProtected('set')); | ||
Assert::same('public', $mixed->getVisibility()); | ||
Assert::same('protected', $mixed->getVisibility('set')); | ||
|
||
// Protected with private setter | ||
$nested = $class->addProperty('fourth') | ||
->setType('string') | ||
->setProtected() | ||
->setPrivate('set'); | ||
Assert::false($nested->isPublic()); | ||
Assert::true($nested->isProtected()); | ||
Assert::true($nested->isPrivate('set')); | ||
Assert::same('protected', $nested->getVisibility()); | ||
Assert::same('private', $nested->getVisibility('set')); | ||
|
||
// Test invalid getter visibility | ||
Assert::exception( | ||
fn() => $default->setVisibility('invalid', 'public'), | ||
ValueError::class, | ||
); | ||
|
||
// Test invalid setter visibility | ||
Assert::exception( | ||
fn() => $default->setVisibility('public', 'invalid'), | ||
ValueError::class, | ||
); | ||
|
||
|
||
same(<<<'XX' | ||
class Demo | ||
{ | ||
public string $first; | ||
private(set) string $second; | ||
public protected(set) string $third; | ||
protected private(set) string $fourth; | ||
} | ||
|
||
XX, (string) $class); |