Skip to content

Commit ab55fb8

Browse files
committed
constants are PascalCase
1 parent a4eb800 commit ab55fb8

15 files changed

+113
-97
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,14 +590,14 @@ To simplify a fully qualified class, function or constant name according to the
590590

591591
```php
592592
echo $namespace->simplifyName('Foo\Bar'); // 'Bar', because 'Foo' is current namespace
593-
echo $namespace->simplifyName('iter\range', $namespace::NAME_FUNCTION); // 'range', because of the defined use-statement
593+
echo $namespace->simplifyName('iter\range', $namespace::NameFunction); // 'range', because of the defined use-statement
594594
```
595595

596596
Conversely, you can convert a simplified class, function or constant name to a fully qualified one using the `resolveName` method:
597597

598598
```php
599599
echo $namespace->resolveName('Bar'); // 'Foo\Bar'
600-
echo $namespace->resolveName('range', $namespace::NAME_FUNCTION); // 'iter\range'
600+
echo $namespace->resolveName('range', $namespace::NameFunction); // 'iter\range'
601601
```
602602

603603
Class Names Resolving

src/PhpGenerator/ClassLike.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ abstract class ClassLike
2222
use Traits\AttributeAware;
2323

2424
public const
25-
VISIBILITY_PUBLIC = 'public',
26-
VISIBILITY_PROTECTED = 'protected',
27-
VISIBILITY_PRIVATE = 'private';
25+
VisibilityPublic = 'public',
26+
VisibilityProtected = 'protected',
27+
VisibilityPrivate = 'private';
28+
29+
/** @deprecated */
30+
public const
31+
VISIBILITY_PUBLIC = self::VisibilityPublic,
32+
VISIBILITY_PROTECTED = self::VisibilityProtected,
33+
VISIBILITY_PRIVATE = self::VisibilityPrivate;
2834

2935
private ?PhpNamespace $namespace;
3036
private ?string $name;
@@ -78,7 +84,7 @@ public function getNamespace(): ?PhpNamespace
7884

7985
public function setName(?string $name): static
8086
{
81-
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::KEYWORDS[strtolower($name)]))) {
87+
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::Keywords[strtolower($name)]))) {
8288
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
8389
}
8490

src/PhpGenerator/ClassType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ final class ClassType extends ClassLike
3333
TYPE_ENUM = 'enum';
3434

3535
private string $type = self::TYPE_CLASS;
36-
3736
private bool $final = false;
3837
private bool $abstract = false;
3938
private ?string $extends = null;

src/PhpGenerator/Dumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
final class Dumper
1919
{
20-
private const INDENT_LENGTH = 4;
20+
private const IndentLength = 4;
2121

2222
public int $maxDepth = 50;
2323
public int $wrapLength = 120;
@@ -130,7 +130,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
130130
}
131131

132132
array_pop($parents);
133-
$wrap = str_contains($outInline, "\n") || $level * self::INDENT_LENGTH + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
133+
$wrap = str_contains($outInline, "\n") || $level * self::IndentLength + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
134134
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
135135
}
136136

src/PhpGenerator/Extractor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ private function prepareReplacements(array $nodes): array
115115
if ($node instanceof Node\Name\FullyQualified) {
116116
if ($node->getAttribute('originalName') instanceof Node\Name) {
117117
$of = match (true) {
118-
$node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NAME_CONSTANT,
119-
$node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NAME_FUNCTION,
120-
default => PhpNamespace::NAME_NORMAL,
118+
$node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NameConstant,
119+
$node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NameFunction,
120+
default => PhpNamespace::NameNormal,
121121
};
122122
$replacements[] = [
123123
$node->getStartFilePos() - $start,
@@ -224,9 +224,9 @@ public function enterNode(Node $node)
224224
private function addUseToNamespace(Node\Stmt\Use_ $node, PhpNamespace $namespace): void
225225
{
226226
$of = [
227-
$node::TYPE_NORMAL => PhpNamespace::NAME_NORMAL,
228-
$node::TYPE_FUNCTION => PhpNamespace::NAME_FUNCTION,
229-
$node::TYPE_CONSTANT => PhpNamespace::NAME_CONSTANT,
227+
$node::TYPE_NORMAL => PhpNamespace::NameNormal,
228+
$node::TYPE_FUNCTION => PhpNamespace::NameFunction,
229+
$node::TYPE_CONSTANT => PhpNamespace::NameConstant,
230230
][$node->type];
231231
foreach ($node->uses as $use) {
232232
$namespace->addUse($use->name->toString(), $use->alias?->toString(), $of);

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ private function getAttributes($from): array
306306
private function getVisibility($from): string
307307
{
308308
return $from->isPrivate()
309-
? ClassType::VISIBILITY_PRIVATE
310-
: ($from->isProtected() ? ClassLike::VISIBILITY_PROTECTED : ClassLike::VISIBILITY_PUBLIC);
309+
? ClassLike::VisibilityPrivate
310+
: ($from->isProtected() ? ClassLike::VisibilityProtected : ClassLike::VisibilityPublic);
311311
}
312312

313313

src/PhpGenerator/Helpers.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ final class Helpers
1919
{
2020
use Nette\StaticClass;
2121

22-
public const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
22+
public const ReIdentifier = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
2323

24-
public const KEYWORDS = [
24+
public const Keywords = [
2525
// built-in types
2626
'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
2727
'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1, 'mixed' => 1, 'false' => 1,
@@ -47,6 +47,11 @@ final class Helpers
4747
'true' => 1,
4848
];
4949

50+
/** @deprecated */
51+
public const
52+
PHP_IDENT = self::ReIdentifier,
53+
KEYWORDS = self::Keywords;
54+
5055

5156
/** @deprecated use (new Nette\PhpGenerator\Dumper)->dump() */
5257
public static function dump(mixed $var): string
@@ -86,9 +91,9 @@ public static function formatDocComment(string $content): string
8691
}
8792

8893

89-
public static function tagName(string $name, string $of = PhpNamespace::NAME_NORMAL): string
94+
public static function tagName(string $name, string $of = PhpNamespace::NameNormal): string
9095
{
91-
return isset(self::KEYWORDS[strtolower($name)])
96+
return isset(self::Keywords[strtolower($name)])
9297
? $name
9398
: "/*($of*/$name";
9499
}
@@ -121,13 +126,13 @@ public static function unindent(string $s, int $level = 1): string
121126

122127
public static function isIdentifier(mixed $value): bool
123128
{
124-
return is_string($value) && preg_match('#^' . self::PHP_IDENT . '$#D', $value);
129+
return is_string($value) && preg_match('#^' . self::ReIdentifier . '$#D', $value);
125130
}
126131

127132

128133
public static function isNamespaceIdentifier(mixed $value, bool $allowLeadingSlash = false): bool
129134
{
130-
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::PHP_IDENT . '(\\\\' . self::PHP_IDENT . ')*$#D';
135+
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::ReIdentifier . '(\\\\' . self::ReIdentifier . ')*$#D';
131136
return is_string($value) && preg_match($re, $value);
132137
}
133138

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function addPromotedParameter(string $name, mixed $defaultValue = null):
9999
/** @throws Nette\InvalidStateException */
100100
public function validate(): void
101101
{
102-
if ($this->abstract && ($this->final || $this->visibility === ClassType::VISIBILITY_PRIVATE)) {
102+
if ($this->abstract && ($this->final || $this->visibility === ClassLike::VisibilityPrivate)) {
103103
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
104104
}
105105
}

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function getFunctions(): array
127127
}
128128

129129

130-
public function addUse(string $name, ?string $alias = null, string $of = PhpNamespace::NAME_NORMAL): static
130+
public function addUse(string $name, ?string $alias = null, string $of = PhpNamespace::NameNormal): static
131131
{
132132
$this->addNamespace('')->addUse($name, $alias, $of);
133133
return $this;

src/PhpGenerator/PhpNamespace.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,25 @@ final class PhpNamespace
2626
use Nette\SmartObject;
2727

2828
public const
29-
NAME_NORMAL = 'n',
30-
NAME_FUNCTION = 'f',
31-
NAME_CONSTANT = 'c';
29+
NameNormal = 'n',
30+
NameFunction = 'f',
31+
NameConstant = 'c';
32+
33+
/** @deprecated */
34+
public const
35+
NAME_NORMAL = self::NameNormal,
36+
NAME_FUNCTION = self::NameFunction,
37+
NAME_CONSTANT = self::NameConstant;
3238

3339
private string $name;
3440

3541
private bool $bracketedSyntax = false;
3642

3743
/** @var string[][] */
3844
private array $aliases = [
39-
self::NAME_NORMAL => [],
40-
self::NAME_FUNCTION => [],
41-
self::NAME_CONSTANT => [],
45+
self::NameNormal => [],
46+
self::NameFunction => [],
47+
self::NameConstant => [],
4248
];
4349

4450
/** @var ClassLike[] */
@@ -91,21 +97,21 @@ public function getBracketedSyntax(): bool
9197
/**
9298
* @throws InvalidStateException
9399
*/
94-
public function addUse(string $name, ?string $alias = null, string $of = self::NAME_NORMAL): static
100+
public function addUse(string $name, ?string $alias = null, string $of = self::NameNormal): static
95101
{
96102
if (
97103
!Helpers::isNamespaceIdentifier($name, true)
98-
|| (Helpers::isIdentifier($name) && isset(Helpers::KEYWORDS[strtolower($name)]))
104+
|| (Helpers::isIdentifier($name) && isset(Helpers::Keywords[strtolower($name)]))
99105
) {
100106
throw new Nette\InvalidArgumentException("Value '$name' is not valid class/function/constant name.");
101107

102-
} elseif ($alias && (!Helpers::isIdentifier($alias) || isset(Helpers::KEYWORDS[strtolower($alias)]))) {
108+
} elseif ($alias && (!Helpers::isIdentifier($alias) || isset(Helpers::Keywords[strtolower($alias)]))) {
103109
throw new Nette\InvalidArgumentException("Value '$alias' is not valid alias.");
104110
}
105111

106112
$name = ltrim($name, '\\');
107113
$aliases = array_change_key_case($this->aliases[$of]);
108-
$used = [self::NAME_NORMAL => $this->classes, self::NAME_FUNCTION => $this->functions, self::NAME_CONSTANT => []][$of];
114+
$used = [self::NameNormal => $this->classes, self::NameFunction => $this->functions, self::NameConstant => []][$of];
109115

110116
if ($alias === null) {
111117
$base = Helpers::extractShortName($name);
@@ -133,18 +139,18 @@ public function addUse(string $name, ?string $alias = null, string $of = self::N
133139

134140
public function addUseFunction(string $name, ?string $alias = null): static
135141
{
136-
return $this->addUse($name, $alias, self::NAME_FUNCTION);
142+
return $this->addUse($name, $alias, self::NameFunction);
137143
}
138144

139145

140146
public function addUseConstant(string $name, ?string $alias = null): static
141147
{
142-
return $this->addUse($name, $alias, self::NAME_CONSTANT);
148+
return $this->addUse($name, $alias, self::NameConstant);
143149
}
144150

145151

146152
/** @return string[] */
147-
public function getUses(string $of = self::NAME_NORMAL): array
153+
public function getUses(string $of = self::NameNormal): array
148154
{
149155
asort($this->aliases[$of]);
150156
return array_filter(
@@ -163,16 +169,16 @@ public function unresolveName(string $name): string
163169
}
164170

165171

166-
public function resolveName(string $name, string $of = self::NAME_NORMAL): string
172+
public function resolveName(string $name, string $of = self::NameNormal): string
167173
{
168-
if (isset(Helpers::KEYWORDS[strtolower($name)]) || $name === '') {
174+
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
169175
return $name;
170176
} elseif ($name[0] === '\\') {
171177
return substr($name, 1);
172178
}
173179

174180
$aliases = array_change_key_case($this->aliases[$of]);
175-
if ($of !== self::NAME_NORMAL) {
181+
if ($of !== self::NameNormal) {
176182
return $aliases[strtolower($name)]
177183
?? $this->resolveName(Helpers::extractNamespace($name) . '\\') . Helpers::extractShortName($name);
178184
}
@@ -184,21 +190,21 @@ public function resolveName(string $name, string $of = self::NAME_NORMAL): strin
184190
}
185191

186192

187-
public function simplifyType(string $type, string $of = self::NAME_NORMAL): string
193+
public function simplifyType(string $type, string $of = self::NameNormal): string
188194
{
189195
return preg_replace_callback('~[\w\x7f-\xff\\\\]+~', fn($m) => $this->simplifyName($m[0], $of), $type);
190196
}
191197

192198

193-
public function simplifyName(string $name, string $of = self::NAME_NORMAL): string
199+
public function simplifyName(string $name, string $of = self::NameNormal): string
194200
{
195-
if (isset(Helpers::KEYWORDS[strtolower($name)]) || $name === '') {
201+
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
196202
return $name;
197203
}
198204

199205
$name = ltrim($name, '\\');
200206

201-
if ($of !== self::NAME_NORMAL) {
207+
if ($of !== self::NameNormal) {
202208
foreach ($this->aliases[$of] as $alias => $original) {
203209
if (strcasecmp($original, $name) === 0) {
204210
return $alias;
@@ -244,7 +250,7 @@ public function add(ClassLike $class): static
244250
$lower = strtolower($name);
245251
if (isset($this->classes[$lower]) && $this->classes[$lower] !== $class) {
246252
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
247-
} elseif ($orig = array_change_key_case($this->aliases[self::NAME_NORMAL])[$lower] ?? null) {
253+
} elseif ($orig = array_change_key_case($this->aliases[self::NameNormal])[$lower] ?? null) {
248254
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
249255
}
250256

@@ -293,7 +299,7 @@ public function addFunction(string $name): GlobalFunction
293299
$lower = strtolower($name);
294300
if (isset($this->functions[$lower])) {
295301
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
296-
} elseif ($orig = array_change_key_case($this->aliases[self::NAME_FUNCTION])[$lower] ?? null) {
302+
} elseif ($orig = array_change_key_case($this->aliases[self::NameFunction])[$lower] ?? null) {
297303
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
298304
}
299305

0 commit comments

Comments
 (0)