Skip to content

Commit

Permalink
added Modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 27, 2024
1 parent 83968a1 commit c20f4f6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
24 changes: 14 additions & 10 deletions src/PhpGenerator/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ abstract class ClassLike
use Traits\CommentAware;
use Traits\AttributeAware;

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

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

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

/** @deprecated use ClassLike::VisibilityPrivate */
public const VISIBILITY_PRIVATE = self::VisibilityPrivate;
/** @deprecated use Modifier::Public */
public const VISIBILITY_PUBLIC = Modifier::Public;

/** @deprecated use Modifier::Protected */
public const VISIBILITY_PROTECTED = Modifier::Protected;

/** @deprecated use Modifier::Private */
public const VISIBILITY_PRIVATE = Modifier::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) => Modifier::Public,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Modifier::Protected,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Modifier::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);
? Modifier::Private
: ($from->isProtected() ? Modifier::Protected : Modifier::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 === Modifier::Private)) {
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/PhpGenerator/Modifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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;


/**
* Class members modifiers.
*/
final class Modifier
{
public const Public = 'public';
public const Protected = 'protected';
public const Private = 'private';
}
16 changes: 8 additions & 8 deletions src/PhpGenerator/Traits/VisibilityAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Nette\PhpGenerator\Traits;

use Nette;
use Nette\PhpGenerator\ClassLike;
use Nette\PhpGenerator\Modifier;


/**
Expand All @@ -27,7 +27,7 @@ trait VisibilityAware
*/
public function setVisibility(?string $val): static
{
if (!in_array($val, [ClassLike::VisibilityPublic, ClassLike::VisibilityProtected, ClassLike::VisibilityPrivate, null], true)) {
if (!in_array($val, [Modifier::Public, Modifier::Protected, Modifier::Private, null], true)) {
throw new Nette\InvalidArgumentException('Argument must be public|protected|private.');
}

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

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


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


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


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


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


public function isPrivate(): bool
{
return $this->visibility === ClassLike::VisibilityPrivate;
return $this->visibility === Modifier::Private;
}
}

0 comments on commit c20f4f6

Please sign in to comment.