Skip to content

Commit

Permalink
added Visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 29, 2024
1 parent d0c7e3e commit 132099e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 36 deletions.
20 changes: 9 additions & 11 deletions src/PhpGenerator/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ abstract class ClassLike
use Traits\CommentAware;
use Traits\AttributeAware;

public const
VisibilityPublic = 'public',
VisibilityProtected = 'protected',
VisibilityPrivate = 'private';
/** @deprecated use Visibility::Public */
public const VisibilityPublic = Visibility::Public,
VISIBILITY_PUBLIC = Visibility::Public;

/** @deprecated use ClassLike::VisibilityPublic */
public const VISIBILITY_PUBLIC = self::VisibilityPublic;
/** @deprecated use Visibility::Protected */
public const VisibilityProtected = Visibility::Protected,
VISIBILITY_PROTECTED = Visibility::Protected;

/** @deprecated use ClassLike::VisibilityProtected */
public const VISIBILITY_PROTECTED = self::VisibilityProtected;

/** @deprecated use ClassLike::VisibilityPrivate */
public const VISIBILITY_PRIVATE = self::VisibilityPrivate;
/** @deprecated use Visibility::Private */
public const VisibilityPrivate = Visibility::Private,
VISIBILITY_PRIVATE = Visibility::Private;

private ?PhpNamespace $namespace;
private ?string $name;
Expand Down
6 changes: 3 additions & 3 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ private function formatValue(Node\Expr $value, int $level): Literal
private function toVisibility(int $flags): ?string
{
return match (true) {
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private,
default => null,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ private function getAttributes($from): array
private function getVisibility($from): string
{
return $from->isPrivate()
? ClassLike::VisibilityPrivate
: ($from->isProtected() ? ClassLike::VisibilityProtected : ClassLike::VisibilityPublic);
? Visibility::Private
: ($from->isProtected() ? Visibility::Protected : Visibility::Public);
}


Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function addPromotedParameter(string $name, mixed $defaultValue = null):
/** @throws Nette\InvalidStateException */
public function validate(): void
{
if ($this->abstract && ($this->final || $this->visibility === ClassLike::VisibilityPrivate)) {
if ($this->abstract && ($this->final || $this->visibility === Visibility::Private)) {
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
}
}
Expand Down
27 changes: 10 additions & 17 deletions src/PhpGenerator/Traits/VisibilityAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

namespace Nette\PhpGenerator\Traits;

use Nette;
use Nette\PhpGenerator\ClassLike;
use Nette\PhpGenerator\Visibility;


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


/**
* @param string|null $val public|protected|private
*/
public function setVisibility(?string $val): static
/** @param 'public'|'protected'|'private'|null $value */
public function setVisibility(?string $value): static
{
if (!in_array($val, [ClassLike::VisibilityPublic, ClassLike::VisibilityProtected, ClassLike::VisibilityPrivate, null], true)) {
throw new Nette\InvalidArgumentException('Argument must be public|protected|private.');
}

$this->visibility = $val;
$this->visibility = $value === null ? $value : Visibility::from($value);
return $this;
}

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

public function setPublic(): static
{
$this->visibility = ClassLike::VisibilityPublic;
$this->visibility = Visibility::Public;
return $this;
}


public function isPublic(): bool
{
return $this->visibility === ClassLike::VisibilityPublic || $this->visibility === null;
return $this->visibility === Visibility::Public || $this->visibility === null;
}


public function setProtected(): static
{
$this->visibility = ClassLike::VisibilityProtected;
$this->visibility = Visibility::Protected;
return $this;
}


public function isProtected(): bool
{
return $this->visibility === ClassLike::VisibilityProtected;
return $this->visibility === Visibility::Protected;
}


public function setPrivate(): static
{
$this->visibility = ClassLike::VisibilityPrivate;
$this->visibility = Visibility::Private;
return $this;
}


public function isPrivate(): bool
{
return $this->visibility === ClassLike::VisibilityPrivate;
return $this->visibility === Visibility::Private;
}
}
34 changes: 34 additions & 0 deletions src/PhpGenerator/Visibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;


/**
* Member visibility.
*/
/*enum*/ final class Visibility
{
use Nette\StaticClass;

public const Public = 'public';
public const Protected = 'protected';
public const Private = 'private';


/** @internal */
public static function from(string $value): string
{
return $value === self::Public || $value === self::Protected || $value === self::Private
? $value
: throw new \ValueError("'$value' is not a valid value of visibility");
}
}
3 changes: 1 addition & 2 deletions tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ Assert::same($parameters, $method->getParameters());

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


Expand Down

0 comments on commit 132099e

Please sign in to comment.