@@ -26,9 +26,14 @@ 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
+ public const
34
+ NAME_NORMAL = self ::NameNormal,
35
+ NAME_FUNCTION = self ::NameFunction,
36
+ NAME_CONSTANT = self ::NameConstant;
32
37
33
38
/** @var string */
34
39
private $ name ;
@@ -38,9 +43,9 @@ final class PhpNamespace
38
43
39
44
/** @var string[][] */
40
45
private $ aliases = [
41
- self ::NAME_NORMAL => [],
42
- self ::NAME_FUNCTION => [],
43
- self ::NAME_CONSTANT => [],
46
+ self ::NameNormal => [],
47
+ self ::NameFunction => [],
48
+ self ::NameConstant => [],
44
49
];
45
50
46
51
/** @var ClassType[] */
@@ -94,21 +99,21 @@ public function getBracketedSyntax(): bool
94
99
* @throws InvalidStateException
95
100
* @return static
96
101
*/
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
98
103
{
99
104
if (
100
105
!Helpers::isNamespaceIdentifier ($ name , true )
101
- || (Helpers::isIdentifier ($ name ) && isset (Helpers::KEYWORDS [strtolower ($ name )]))
106
+ || (Helpers::isIdentifier ($ name ) && isset (Helpers::Keywords [strtolower ($ name )]))
102
107
) {
103
108
throw new Nette \InvalidArgumentException ("Value ' $ name' is not valid class/function/constant name. " );
104
109
105
- } elseif ($ alias && (!Helpers::isIdentifier ($ alias ) || isset (Helpers::KEYWORDS [strtolower ($ alias )]))) {
110
+ } elseif ($ alias && (!Helpers::isIdentifier ($ alias ) || isset (Helpers::Keywords [strtolower ($ alias )]))) {
106
111
throw new Nette \InvalidArgumentException ("Value ' $ alias' is not valid alias. " );
107
112
}
108
113
109
114
$ name = ltrim ($ name , '\\' );
110
115
$ 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 ];
112
117
113
118
if ($ alias === null ) {
114
119
$ base = Helpers::extractShortName ($ name );
@@ -137,19 +142,19 @@ public function addUse(string $name, ?string $alias = null, string $of = self::N
137
142
/** @return static */
138
143
public function addUseFunction (string $ name , ?string $ alias = null ): self
139
144
{
140
- return $ this ->addUse ($ name , $ alias , self ::NAME_FUNCTION );
145
+ return $ this ->addUse ($ name , $ alias , self ::NameFunction );
141
146
}
142
147
143
148
144
149
/** @return static */
145
150
public function addUseConstant (string $ name , ?string $ alias = null ): self
146
151
{
147
- return $ this ->addUse ($ name , $ alias , self ::NAME_CONSTANT );
152
+ return $ this ->addUse ($ name , $ alias , self ::NameConstant );
148
153
}
149
154
150
155
151
156
/** @return string[] */
152
- public function getUses (string $ of = self ::NAME_NORMAL ): array
157
+ public function getUses (string $ of = self ::NameNormal ): array
153
158
{
154
159
asort ($ this ->aliases [$ of ]);
155
160
return array_filter (
@@ -167,16 +172,16 @@ public function unresolveName(string $name): string
167
172
}
168
173
169
174
170
- public function resolveName (string $ name , string $ of = self ::NAME_NORMAL ): string
175
+ public function resolveName (string $ name , string $ of = self ::NameNormal ): string
171
176
{
172
- if (isset (Helpers::KEYWORDS [strtolower ($ name )]) || $ name === '' ) {
177
+ if (isset (Helpers::Keywords [strtolower ($ name )]) || $ name === '' ) {
173
178
return $ name ;
174
179
} elseif ($ name [0 ] === '\\' ) {
175
180
return substr ($ name , 1 );
176
181
}
177
182
178
183
$ aliases = array_change_key_case ($ this ->aliases [$ of ]);
179
- if ($ of !== self ::NAME_NORMAL ) {
184
+ if ($ of !== self ::NameNormal ) {
180
185
return $ aliases [strtolower ($ name )]
181
186
?? $ this ->resolveName (Helpers::extractNamespace ($ name ) . '\\' ) . Helpers::extractShortName ($ name );
182
187
}
@@ -188,21 +193,21 @@ public function resolveName(string $name, string $of = self::NAME_NORMAL): strin
188
193
}
189
194
190
195
191
- public function simplifyType (string $ type , string $ of = self ::NAME_NORMAL ): string
196
+ public function simplifyType (string $ type , string $ of = self ::NameNormal ): string
192
197
{
193
198
return preg_replace_callback ('~[\w\x7f-\xff \\\\]+~ ' , function ($ m ) use ($ of ) { return $ this ->simplifyName ($ m [0 ], $ of ); }, $ type );
194
199
}
195
200
196
201
197
- public function simplifyName (string $ name , string $ of = self ::NAME_NORMAL ): string
202
+ public function simplifyName (string $ name , string $ of = self ::NameNormal ): string
198
203
{
199
- if (isset (Helpers::KEYWORDS [strtolower ($ name )]) || $ name === '' ) {
204
+ if (isset (Helpers::Keywords [strtolower ($ name )]) || $ name === '' ) {
200
205
return $ name ;
201
206
}
202
207
203
208
$ name = ltrim ($ name , '\\' );
204
209
205
- if ($ of !== self ::NAME_NORMAL ) {
210
+ if ($ of !== self ::NameNormal ) {
206
211
foreach ($ this ->aliases [$ of ] as $ alias => $ original ) {
207
212
if (strcasecmp ($ original , $ name ) === 0 ) {
208
213
return $ alias ;
@@ -247,7 +252,7 @@ public function add(ClassType $class): self
247
252
}
248
253
249
254
$ 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 ) {
251
256
throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
252
257
}
253
258
@@ -291,7 +296,7 @@ public function removeClass(string $name): self
291
296
public function addFunction (string $ name ): GlobalFunction
292
297
{
293
298
$ 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 ) {
295
300
throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
296
301
}
297
302
0 commit comments