Skip to content

Commit fdc5e14

Browse files
committed
constants are PascalCase
1 parent 437578a commit fdc5e14

14 files changed

+109
-94
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/ClassType.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ final class ClassType
3131
TYPE_ENUM = 'enum';
3232

3333
public const
34-
VISIBILITY_PUBLIC = 'public',
35-
VISIBILITY_PROTECTED = 'protected',
36-
VISIBILITY_PRIVATE = 'private';
34+
VisibilityPublic = 'public',
35+
VisibilityProtected = 'protected',
36+
VisibilityPrivate = 'private';
37+
38+
public const
39+
VISIBILITY_PUBLIC = self::VisibilityPublic,
40+
VISIBILITY_PROTECTED = self::VisibilityProtected,
41+
VISIBILITY_PRIVATE = self::VisibilityPrivate;
3742

3843
/** @var PhpNamespace|null */
3944
private $namespace;
@@ -155,7 +160,7 @@ public function getNamespace(): ?PhpNamespace
155160
/** @return static */
156161
public function setName(?string $name): self
157162
{
158-
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::KEYWORDS[strtolower($name)]))) {
163+
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::Keywords[strtolower($name)]))) {
159164
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
160165
}
161166

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
/** @var int */
2323
public $maxDepth = 50;
@@ -135,7 +135,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
135135
}
136136

137137
array_pop($parents);
138-
$wrap = strpos($outInline, "\n") !== false || $level * self::INDENT_LENGTH + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
138+
$wrap = strpos($outInline, "\n") !== false || $level * self::IndentLength + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
139139
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
140140
}
141141

src/PhpGenerator/Extractor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private function prepareReplacements(array $statements): array
106106
if ($node instanceof Node\Name\FullyQualified) {
107107
if ($node->getAttribute('originalName') instanceof Node\Name) {
108108
$of = $node->getAttribute('parent') instanceof Node\Expr\ConstFetch
109-
? PhpNamespace::NAME_CONSTANT
110-
: ($node->getAttribute('parent') instanceof Node\Expr\FuncCall ? PhpNamespace::NAME_FUNCTION : PhpNamespace::NAME_NORMAL);
109+
? PhpNamespace::NameConstant
110+
: ($node->getAttribute('parent') instanceof Node\Expr\FuncCall ? PhpNamespace::NameFunction : PhpNamespace::NameNormal);
111111
$replacements[] = [
112112
$node->getStartFilePos() - $start,
113113
$node->getEndFilePos() - $start,
@@ -225,9 +225,9 @@ public function enterNode(Node $node)
225225
private function addUseToNamespace(Node\Stmt\Use_ $node, PhpNamespace $namespace): void
226226
{
227227
$of = [
228-
$node::TYPE_NORMAL => PhpNamespace::NAME_NORMAL,
229-
$node::TYPE_FUNCTION => PhpNamespace::NAME_FUNCTION,
230-
$node::TYPE_CONSTANT => PhpNamespace::NAME_CONSTANT,
228+
$node::TYPE_NORMAL => PhpNamespace::NameNormal,
229+
$node::TYPE_FUNCTION => PhpNamespace::NameFunction,
230+
$node::TYPE_CONSTANT => PhpNamespace::NameConstant,
231231
][$node->type];
232232
foreach ($node->uses as $use) {
233233
$namespace->addUse($use->name->toString(), $use->alias ? $use->alias->toString() : null, $of);

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ private function getAttributes($from): array
348348
private function getVisibility($from): string
349349
{
350350
return $from->isPrivate()
351-
? ClassType::VISIBILITY_PRIVATE
352-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC);
351+
? ClassType::VisibilityPrivate
352+
: ($from->isProtected() ? ClassType::VisibilityProtected : ClassType::VisibilityPublic);
353353
}
354354

355355

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($var): string
@@ -83,9 +88,9 @@ public static function formatDocComment(string $content): string
8388
}
8489

8590

86-
public static function tagName(string $name, string $of = PhpNamespace::NAME_NORMAL): string
91+
public static function tagName(string $name, string $of = PhpNamespace::NameNormal): string
8792
{
88-
return isset(self::KEYWORDS[strtolower($name)])
93+
return isset(self::Keywords[strtolower($name)])
8994
? $name
9095
: "/*($of*/$name";
9196
}
@@ -118,13 +123,13 @@ public static function unindent(string $s, int $level = 1): string
118123

119124
public static function isIdentifier($value): bool
120125
{
121-
return is_string($value) && preg_match('#^' . self::PHP_IDENT . '$#D', $value);
126+
return is_string($value) && preg_match('#^' . self::ReIdentifier . '$#D', $value);
122127
}
123128

124129

125130
public static function isNamespaceIdentifier($value, bool $allowLeadingSlash = false): bool
126131
{
127-
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::PHP_IDENT . '(\\\\' . self::PHP_IDENT . ')*$#D';
132+
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::ReIdentifier . '(\\\\' . self::ReIdentifier . ')*$#D';
128133
return is_string($value) && preg_match($re, $value);
129134
}
130135

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function addPromotedParameter(string $name, $defaultValue = null): Promot
138138
/** @throws Nette\InvalidStateException */
139139
public function validate(): void
140140
{
141-
if ($this->abstract && ($this->final || $this->visibility === ClassType::VISIBILITY_PRIVATE)) {
141+
if ($this->abstract && ($this->final || $this->visibility === ClassType::VisibilityPrivate)) {
142142
throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
143143
}
144144
}

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function getFunctions(): array
137137

138138

139139
/** @return static */
140-
public function addUse(string $name, ?string $alias = null, string $of = PhpNamespace::NAME_NORMAL): self
140+
public function addUse(string $name, ?string $alias = null, string $of = PhpNamespace::NameNormal): self
141141
{
142142
$this->addNamespace('')->addUse($name, $alias, $of);
143143
return $this;

src/PhpGenerator/PhpNamespace.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ 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+
public const
34+
NAME_NORMAL = self::NameNormal,
35+
NAME_FUNCTION = self::NameFunction,
36+
NAME_CONSTANT = self::NameConstant;
3237

3338
/** @var string */
3439
private $name;
@@ -38,9 +43,9 @@ final class PhpNamespace
3843

3944
/** @var string[][] */
4045
private $aliases = [
41-
self::NAME_NORMAL => [],
42-
self::NAME_FUNCTION => [],
43-
self::NAME_CONSTANT => [],
46+
self::NameNormal => [],
47+
self::NameFunction => [],
48+
self::NameConstant => [],
4449
];
4550

4651
/** @var ClassType[] */
@@ -94,21 +99,21 @@ public function getBracketedSyntax(): bool
9499
* @throws InvalidStateException
95100
* @return static
96101
*/
97-
public function addUse(string $name, ?string $alias = null, string $of = self::NAME_NORMAL): self
102+
public function addUse(string $name, ?string $alias = null, string $of = self::NameNormal): self
98103
{
99104
if (
100105
!Helpers::isNamespaceIdentifier($name, true)
101-
|| (Helpers::isIdentifier($name) && isset(Helpers::KEYWORDS[strtolower($name)]))
106+
|| (Helpers::isIdentifier($name) && isset(Helpers::Keywords[strtolower($name)]))
102107
) {
103108
throw new Nette\InvalidArgumentException("Value '$name' is not valid class/function/constant name.");
104109

105-
} elseif ($alias && (!Helpers::isIdentifier($alias) || isset(Helpers::KEYWORDS[strtolower($alias)]))) {
110+
} elseif ($alias && (!Helpers::isIdentifier($alias) || isset(Helpers::Keywords[strtolower($alias)]))) {
106111
throw new Nette\InvalidArgumentException("Value '$alias' is not valid alias.");
107112
}
108113

109114
$name = ltrim($name, '\\');
110115
$aliases = array_change_key_case($this->aliases[$of]);
111-
$used = [self::NAME_NORMAL => $this->classes, self::NAME_FUNCTION => $this->functions, self::NAME_CONSTANT => []][$of];
116+
$used = [self::NameNormal => $this->classes, self::NameFunction => $this->functions, self::NameConstant => []][$of];
112117

113118
if ($alias === null) {
114119
$base = Helpers::extractShortName($name);
@@ -137,19 +142,19 @@ public function addUse(string $name, ?string $alias = null, string $of = self::N
137142
/** @return static */
138143
public function addUseFunction(string $name, ?string $alias = null): self
139144
{
140-
return $this->addUse($name, $alias, self::NAME_FUNCTION);
145+
return $this->addUse($name, $alias, self::NameFunction);
141146
}
142147

143148

144149
/** @return static */
145150
public function addUseConstant(string $name, ?string $alias = null): self
146151
{
147-
return $this->addUse($name, $alias, self::NAME_CONSTANT);
152+
return $this->addUse($name, $alias, self::NameConstant);
148153
}
149154

150155

151156
/** @return string[] */
152-
public function getUses(string $of = self::NAME_NORMAL): array
157+
public function getUses(string $of = self::NameNormal): array
153158
{
154159
asort($this->aliases[$of]);
155160
return array_filter(
@@ -167,16 +172,16 @@ public function unresolveName(string $name): string
167172
}
168173

169174

170-
public function resolveName(string $name, string $of = self::NAME_NORMAL): string
175+
public function resolveName(string $name, string $of = self::NameNormal): string
171176
{
172-
if (isset(Helpers::KEYWORDS[strtolower($name)]) || $name === '') {
177+
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
173178
return $name;
174179
} elseif ($name[0] === '\\') {
175180
return substr($name, 1);
176181
}
177182

178183
$aliases = array_change_key_case($this->aliases[$of]);
179-
if ($of !== self::NAME_NORMAL) {
184+
if ($of !== self::NameNormal) {
180185
return $aliases[strtolower($name)]
181186
?? $this->resolveName(Helpers::extractNamespace($name) . '\\') . Helpers::extractShortName($name);
182187
}
@@ -188,21 +193,21 @@ public function resolveName(string $name, string $of = self::NAME_NORMAL): strin
188193
}
189194

190195

191-
public function simplifyType(string $type, string $of = self::NAME_NORMAL): string
196+
public function simplifyType(string $type, string $of = self::NameNormal): string
192197
{
193198
return preg_replace_callback('~[\w\x7f-\xff\\\\]+~', function ($m) use ($of) { return $this->simplifyName($m[0], $of); }, $type);
194199
}
195200

196201

197-
public function simplifyName(string $name, string $of = self::NAME_NORMAL): string
202+
public function simplifyName(string $name, string $of = self::NameNormal): string
198203
{
199-
if (isset(Helpers::KEYWORDS[strtolower($name)]) || $name === '') {
204+
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
200205
return $name;
201206
}
202207

203208
$name = ltrim($name, '\\');
204209

205-
if ($of !== self::NAME_NORMAL) {
210+
if ($of !== self::NameNormal) {
206211
foreach ($this->aliases[$of] as $alias => $original) {
207212
if (strcasecmp($original, $name) === 0) {
208213
return $alias;
@@ -247,7 +252,7 @@ public function add(ClassType $class): self
247252
}
248253

249254
$lower = strtolower($name);
250-
if ($orig = array_change_key_case($this->aliases[self::NAME_NORMAL])[$lower] ?? null) {
255+
if ($orig = array_change_key_case($this->aliases[self::NameNormal])[$lower] ?? null) {
251256
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
252257
}
253258

@@ -291,7 +296,7 @@ public function removeClass(string $name): self
291296
public function addFunction(string $name): GlobalFunction
292297
{
293298
$lower = strtolower($name);
294-
if ($orig = array_change_key_case($this->aliases[self::NAME_FUNCTION])[$lower] ?? null) {
299+
if ($orig = array_change_key_case($this->aliases[self::NameFunction])[$lower] ?? null) {
295300
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
296301
}
297302

src/PhpGenerator/Printer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ public function printNamespace(PhpNamespace $namespace): string
238238
$this->namespace = $this->resolveTypes ? $namespace : null;
239239
$name = $namespace->getName();
240240
$uses = $this->printUses($namespace)
241-
. $this->printUses($namespace, PhpNamespace::NAME_FUNCTION)
242-
. $this->printUses($namespace, PhpNamespace::NAME_CONSTANT);
241+
. $this->printUses($namespace, PhpNamespace::NameFunction)
242+
. $this->printUses($namespace, PhpNamespace::NameConstant);
243243

244244
$items = [];
245245
foreach ($namespace->getClasses() as $class) {
@@ -282,12 +282,12 @@ public function printFile(PhpFile $file): string
282282
}
283283

284284

285-
protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace::NAME_NORMAL): string
285+
protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace::NameNormal): string
286286
{
287287
$prefix = [
288-
PhpNamespace::NAME_NORMAL => '',
289-
PhpNamespace::NAME_FUNCTION => 'function ',
290-
PhpNamespace::NAME_CONSTANT => 'const ',
288+
PhpNamespace::NameNormal => '',
289+
PhpNamespace::NameFunction => 'function ',
290+
PhpNamespace::NameConstant => 'const ',
291291
][$of];
292292
$name = $namespace->getName();
293293
$uses = [];

0 commit comments

Comments
 (0)