1717use Countable ;
1818use InvalidArgumentException ;
1919use PhpParser \Node \Name \FullyQualified ;
20- use const SORT_REGULAR ;
2120use function array_flip ;
2221use function array_key_exists ;
2322use function array_map ;
@@ -153,6 +152,19 @@ private function __construct(
153152 }
154153
155154 public function belongsToWhitelistedNamespace (string $ name ): bool
155+ {
156+ $ nameNamespace = $ this ->retrieveNameNamespace ($ name );
157+
158+ foreach ($ this ->namespaces as $ namespace ) {
159+ if ('' === $ namespace || 0 === strpos ($ nameNamespace , $ namespace )) {
160+ return true ;
161+ }
162+ }
163+
164+ return false ;
165+ }
166+
167+ public function isWhitelistedNamespace (string $ name ): bool
156168 {
157169 $ name = strtolower ($ name );
158170
@@ -161,9 +173,24 @@ public function belongsToWhitelistedNamespace(string $name): bool
161173 }
162174
163175 foreach ($ this ->namespaces as $ namespace ) {
164- if ('' === $ namespace || 0 === strpos ( $ name , $ namespace ) ) {
176+ if ('' === $ namespace ) {
165177 return true ;
166178 }
179+
180+ if ('' !== $ namespace && 0 !== strpos ($ name , $ namespace )) {
181+ continue ;
182+ }
183+
184+ $ nameParts = explode ('\\' , $ name );
185+ $ namespaceParts = explode ('\\' , $ namespace );
186+
187+ foreach ($ namespaceParts as $ index => $ namespacePart ) {
188+ if ($ nameParts [$ index ] !== $ namespacePart ) {
189+ return false ;
190+ }
191+ }
192+
193+ return true ;
167194 }
168195
169196 return false ;
@@ -184,17 +211,12 @@ public function isGlobalWhitelistedFunction(string $functionName): bool
184211
185212 public function recordWhitelistedFunction (FullyQualified $ original , FullyQualified $ alias ): void
186213 {
187- $ this ->whitelistedFunctions [] = [(string ) $ original , (string ) $ alias ];
214+ $ this ->whitelistedFunctions [( string ) $ original ] = [(string ) $ original , (string ) $ alias ];
188215 }
189216
190217 public function getRecordedWhitelistedFunctions (): array
191218 {
192- return array_values (
193- array_unique (
194- $ this ->whitelistedFunctions ,
195- SORT_REGULAR
196- )
197- );
219+ return array_values ($ this ->whitelistedFunctions );
198220 }
199221
200222 /**
@@ -225,12 +247,12 @@ public function isGlobalWhitelistedClass(string $className): bool
225247
226248 public function recordWhitelistedClass (FullyQualified $ original , FullyQualified $ alias ): void
227249 {
228- $ this ->whitelistedClasses [] = [(string ) $ original , (string ) $ alias ];
250+ $ this ->whitelistedClasses [( string ) $ original ] = [(string ) $ original , (string ) $ alias ];
229251 }
230252
231253 public function getRecordedWhitelistedClasses (): array
232254 {
233- return $ this ->whitelistedClasses ;
255+ return array_values ( $ this ->whitelistedClasses ) ;
234256 }
235257
236258 /**
@@ -307,4 +329,19 @@ private static function lowerConstantName(string $name): string
307329
308330 return implode ('\\' , $ parts );
309331 }
332+
333+ private function retrieveNameNamespace (string $ name ): string
334+ {
335+ $ name = strtolower ($ name );
336+
337+ if (0 === strpos ($ name , '\\' )) {
338+ $ name = substr ($ name , 1 );
339+ }
340+
341+ $ nameParts = explode ('\\' , $ name );
342+
343+ array_pop ($ nameParts );
344+
345+ return [] === $ nameParts ? '' : implode ('\\' , $ nameParts );
346+ }
310347}
0 commit comments