@@ -37,8 +37,12 @@ final class PhpNamespace
3737 /** @var bool */
3838 private $ bracketedSyntax = false ;
3939
40- /** @var string[] */
41- private $ aliases = [];
40+ /** @var string[][] */
41+ private $ aliases = [
42+ self ::NAME_NORMAL => [],
43+ self ::NAME_FUNCTION => [],
44+ self ::NAME_CONSTANT => [],
45+ ];
4246
4347 /** @var ClassType[] */
4448 private $ classes = [];
@@ -90,44 +94,62 @@ public function getBracketedSyntax(): bool
9094 * @throws InvalidStateException
9195 * @return static
9296 */
93- public function addUse (string $ name , string $ alias = null ): self
97+ public function addUse (string $ name , string $ alias = null , string $ of = self :: NAME_NORMAL ): self
9498 {
9599 if (
96100 !Helpers::isNamespaceIdentifier ($ name , true )
97101 || (Helpers::isIdentifier ($ name ) && isset (Helpers::KEYWORDS [strtolower ($ name )]))
98102 ) {
99- throw new Nette \InvalidArgumentException ("Value ' $ name' is not valid class name. " );
103+ throw new Nette \InvalidArgumentException ("Value ' $ name' is not valid class/function/constant name. " );
104+
100105 } elseif ($ alias && (!Helpers::isIdentifier ($ alias ) || isset (Helpers::KEYWORDS [strtolower ($ alias )]))) {
101106 throw new Nette \InvalidArgumentException ("Value ' $ alias' is not valid alias. " );
102107 }
103108
104109 $ name = ltrim ($ name , '\\' );
110+ $ aliases = $ this ->aliases [$ of ];
111+ $ used = [self ::NAME_NORMAL => $ this ->classes , self ::NAME_FUNCTION => $ this ->functions , self ::NAME_CONSTANT => []][$ of ];
112+
105113 if ($ alias === null ) {
106114 $ base = Helpers::extractShortName ($ name );
107115 $ counter = null ;
108116 do {
109117 $ alias = $ base . $ counter ;
110118 $ counter ++;
111- } while ((isset ($ this -> aliases [$ alias ]) && $ this -> aliases [$ alias ] !== $ name ) || isset ($ this -> classes [$ alias ]));
119+ } while ((isset ($ aliases [$ alias ]) && $ aliases [$ alias ] !== $ name ) || isset ($ used [$ alias ]));
112120
113- } elseif (isset ($ this -> aliases [$ alias ]) && $ this -> aliases [$ alias ] !== $ name ) {
121+ } elseif (isset ($ aliases [$ alias ]) && $ aliases [$ alias ] !== $ name ) {
114122 throw new InvalidStateException (
115- "Alias ' $ alias' used already for ' {$ this -> aliases [$ alias ]}', cannot use for ' $ name'. "
123+ "Alias ' $ alias' used already for ' {$ aliases [$ alias ]}', cannot use for ' $ name'. "
116124 );
117- } elseif (isset ($ this -> classes [$ alias ])) {
118- throw new Nette \InvalidStateException ("Name ' $ alias' used already for ' $ this ->name \\{$ this -> classes [$ alias ]->getName ()}'. " );
125+ } elseif (isset ($ used [$ alias ])) {
126+ throw new Nette \InvalidStateException ("Name ' $ alias' used already for ' $ this ->name \\{$ used [$ alias ]->getName ()}'. " );
119127 }
120128
121- $ this ->aliases [$ alias ] = $ name ;
129+ $ this ->aliases [$ of ][ $ alias ] = $ name ;
122130 return $ this ;
123131 }
124132
125133
134+ /** @return static */
135+ public function addUseFunction (string $ name , string $ alias = null ): self
136+ {
137+ return $ this ->addUse ($ name , $ alias , self ::NAME_FUNCTION );
138+ }
139+
140+
141+ /** @return static */
142+ public function addUseConstant (string $ name , string $ alias = null ): self
143+ {
144+ return $ this ->addUse ($ name , $ alias , self ::NAME_CONSTANT );
145+ }
146+
147+
126148 /** @return string[] */
127- public function getUses (): array
149+ public function getUses (string $ of = self :: NAME_NORMAL ): array
128150 {
129- asort ($ this ->aliases );
130- return $ this ->aliases ;
151+ asort ($ this ->aliases [ $ of ] );
152+ return $ this ->aliases [ $ of ] ;
131153 }
132154
133155
@@ -138,24 +160,34 @@ public function unresolveName(string $name): string
138160 }
139161
140162
141- public function simplifyType (string $ type ): string
163+ public function simplifyType (string $ type, string $ of = self :: NAME_NORMAL ): string
142164 {
143- return preg_replace_callback ('~[\w\x7f-\xff \\\\]+~ ' , function ($ m ) { return $ this ->simplifyName ($ m [0 ]); }, $ type );
165+ return preg_replace_callback ('~[\w\x7f-\xff \\\\]+~ ' , function ($ m ) use ( $ of ) { return $ this ->simplifyName ($ m [0 ], $ of ); }, $ type );
144166 }
145167
146168
147- public function simplifyName (string $ name ): string
169+ public function simplifyName (string $ name, string $ of = self :: NAME_NORMAL ): string
148170 {
149171 if (isset (Helpers::KEYWORDS [strtolower ($ name )]) || $ name === '' ) {
150172 return $ name ;
151173 }
152174 $ name = ltrim ($ name , '\\' );
175+
176+ if ($ of !== self ::NAME_NORMAL ) {
177+ foreach ($ this ->aliases [$ of ] as $ alias => $ original ) {
178+ if (strcasecmp ($ original , $ name ) === 0 ) {
179+ return $ alias ;
180+ }
181+ }
182+ return $ this ->simplifyName (Helpers::extractNamespace ($ name ) . '\\' ) . Helpers::extractShortName ($ name );
183+ }
184+
153185 $ lower = strtolower ($ name );
154186 $ res = Strings::startsWith ($ lower , strtolower ($ this ->name ) . '\\' )
155187 ? substr ($ name , strlen ($ this ->name ) + 1 )
156188 : null ;
157189
158- foreach ($ this ->aliases as $ alias => $ original ) {
190+ foreach ($ this ->aliases [ $ of ] as $ alias => $ original ) {
159191 if (Strings::startsWith ($ lower . '\\' , strtolower ($ original ) . '\\' )) {
160192 $ short = $ alias . substr ($ name , strlen ($ original ));
161193 if (!isset ($ res ) || strlen ($ res ) > strlen ($ short )) {
@@ -164,7 +196,7 @@ public function simplifyName(string $name): string
164196 }
165197 }
166198
167- return $ res ?: ($ this ->name ? '\\' : '' ) . $ name ;
199+ return $ res ?? ($ this ->name ? '\\' : '' ) . $ name ;
168200 }
169201
170202
@@ -174,7 +206,7 @@ public function add(ClassType $class): self
174206 $ name = $ class ->getName ();
175207 if ($ name === null ) {
176208 throw new Nette \InvalidArgumentException ('Class does not have a name. ' );
177- } elseif ($ orig = $ this ->aliases [$ name ] ?? null ) {
209+ } elseif ($ orig = $ this ->aliases [self :: NAME_NORMAL ][ $ name ] ?? null ) {
178210 throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
179211 }
180212 $ this ->classes [$ name ] = $ class ;
@@ -209,6 +241,9 @@ public function addEnum(string $name): ClassType
209241
210242 public function addFunction (string $ name ): GlobalFunction
211243 {
244+ if ($ orig = $ this ->aliases [self ::NAME_FUNCTION ][$ name ] ?? null ) {
245+ throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
246+ }
212247 return $ this ->functions [$ name ] = new GlobalFunction ($ name );
213248 }
214249
0 commit comments