@@ -26,19 +26,25 @@ final class PhpNamespace
26
26
use Nette \SmartObject;
27
27
28
28
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;
32
38
33
39
private string $ name ;
34
40
35
41
private bool $ bracketedSyntax = false ;
36
42
37
43
/** @var string[][] */
38
44
private array $ aliases = [
39
- self ::NAME_NORMAL => [],
40
- self ::NAME_FUNCTION => [],
41
- self ::NAME_CONSTANT => [],
45
+ self ::NameNormal => [],
46
+ self ::NameFunction => [],
47
+ self ::NameConstant => [],
42
48
];
43
49
44
50
/** @var ClassLike[] */
@@ -91,21 +97,21 @@ public function getBracketedSyntax(): bool
91
97
/**
92
98
* @throws InvalidStateException
93
99
*/
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
95
101
{
96
102
if (
97
103
!Helpers::isNamespaceIdentifier ($ name , true )
98
- || (Helpers::isIdentifier ($ name ) && isset (Helpers::KEYWORDS [strtolower ($ name )]))
104
+ || (Helpers::isIdentifier ($ name ) && isset (Helpers::Keywords [strtolower ($ name )]))
99
105
) {
100
106
throw new Nette \InvalidArgumentException ("Value ' $ name' is not valid class/function/constant name. " );
101
107
102
- } elseif ($ alias && (!Helpers::isIdentifier ($ alias ) || isset (Helpers::KEYWORDS [strtolower ($ alias )]))) {
108
+ } elseif ($ alias && (!Helpers::isIdentifier ($ alias ) || isset (Helpers::Keywords [strtolower ($ alias )]))) {
103
109
throw new Nette \InvalidArgumentException ("Value ' $ alias' is not valid alias. " );
104
110
}
105
111
106
112
$ name = ltrim ($ name , '\\' );
107
113
$ 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 ];
109
115
110
116
if ($ alias === null ) {
111
117
$ base = Helpers::extractShortName ($ name );
@@ -133,18 +139,18 @@ public function addUse(string $name, ?string $alias = null, string $of = self::N
133
139
134
140
public function addUseFunction (string $ name , ?string $ alias = null ): static
135
141
{
136
- return $ this ->addUse ($ name , $ alias , self ::NAME_FUNCTION );
142
+ return $ this ->addUse ($ name , $ alias , self ::NameFunction );
137
143
}
138
144
139
145
140
146
public function addUseConstant (string $ name , ?string $ alias = null ): static
141
147
{
142
- return $ this ->addUse ($ name , $ alias , self ::NAME_CONSTANT );
148
+ return $ this ->addUse ($ name , $ alias , self ::NameConstant );
143
149
}
144
150
145
151
146
152
/** @return string[] */
147
- public function getUses (string $ of = self ::NAME_NORMAL ): array
153
+ public function getUses (string $ of = self ::NameNormal ): array
148
154
{
149
155
asort ($ this ->aliases [$ of ]);
150
156
return array_filter (
@@ -163,16 +169,16 @@ public function unresolveName(string $name): string
163
169
}
164
170
165
171
166
- public function resolveName (string $ name , string $ of = self ::NAME_NORMAL ): string
172
+ public function resolveName (string $ name , string $ of = self ::NameNormal ): string
167
173
{
168
- if (isset (Helpers::KEYWORDS [strtolower ($ name )]) || $ name === '' ) {
174
+ if (isset (Helpers::Keywords [strtolower ($ name )]) || $ name === '' ) {
169
175
return $ name ;
170
176
} elseif ($ name [0 ] === '\\' ) {
171
177
return substr ($ name , 1 );
172
178
}
173
179
174
180
$ aliases = array_change_key_case ($ this ->aliases [$ of ]);
175
- if ($ of !== self ::NAME_NORMAL ) {
181
+ if ($ of !== self ::NameNormal ) {
176
182
return $ aliases [strtolower ($ name )]
177
183
?? $ this ->resolveName (Helpers::extractNamespace ($ name ) . '\\' ) . Helpers::extractShortName ($ name );
178
184
}
@@ -184,21 +190,21 @@ public function resolveName(string $name, string $of = self::NAME_NORMAL): strin
184
190
}
185
191
186
192
187
- public function simplifyType (string $ type , string $ of = self ::NAME_NORMAL ): string
193
+ public function simplifyType (string $ type , string $ of = self ::NameNormal ): string
188
194
{
189
195
return preg_replace_callback ('~[\w\x7f-\xff \\\\]+~ ' , fn ($ m ) => $ this ->simplifyName ($ m [0 ], $ of ), $ type );
190
196
}
191
197
192
198
193
- public function simplifyName (string $ name , string $ of = self ::NAME_NORMAL ): string
199
+ public function simplifyName (string $ name , string $ of = self ::NameNormal ): string
194
200
{
195
- if (isset (Helpers::KEYWORDS [strtolower ($ name )]) || $ name === '' ) {
201
+ if (isset (Helpers::Keywords [strtolower ($ name )]) || $ name === '' ) {
196
202
return $ name ;
197
203
}
198
204
199
205
$ name = ltrim ($ name , '\\' );
200
206
201
- if ($ of !== self ::NAME_NORMAL ) {
207
+ if ($ of !== self ::NameNormal ) {
202
208
foreach ($ this ->aliases [$ of ] as $ alias => $ original ) {
203
209
if (strcasecmp ($ original , $ name ) === 0 ) {
204
210
return $ alias ;
@@ -244,7 +250,7 @@ public function add(ClassLike $class): static
244
250
$ lower = strtolower ($ name );
245
251
if (isset ($ this ->classes [$ lower ]) && $ this ->classes [$ lower ] !== $ class ) {
246
252
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 ) {
248
254
throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
249
255
}
250
256
@@ -293,7 +299,7 @@ public function addFunction(string $name): GlobalFunction
293
299
$ lower = strtolower ($ name );
294
300
if (isset ($ this ->functions [$ lower ])) {
295
301
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 ) {
297
303
throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
298
304
}
299
305
0 commit comments