| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the humbug/php-scoper package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright (c) 2017 Théo FIDRY <[email protected]>, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *                    Pádraic Brady <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Humbug\PhpScoper\PhpParser\Node\NamedIdentifier; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use PhpParser\Node\Identifier; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use PhpParser\Node\Name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use PhpParser\Node\Name\FullyQualified; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use PhpParser\Node\Scalar\String_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use PhpParser\Node\Stmt\Function_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use PhpParser\NodeVisitor\NameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use function array_filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use function implode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use function ltrim; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * Attempts to resolve the identifier node into a fully qualified node. Returns a valid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * (non fully-qualified) name node on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @private | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | final class IdentifierResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     private NameResolver $nameResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function __construct(NameResolver $nameResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |         $this->nameResolver = $nameResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     public function resolveIdentifier(Identifier $identifier): Name | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $resolvedName = $identifier->getAttribute('resolvedName'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if (null !== $resolvedName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             return $resolvedName; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $parentNode = ParentNodeAppender::getParent($identifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if ($parentNode instanceof Function_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             return $this->resolveFunctionIdentifier($identifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $name = NamedIdentifier::create($identifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $this->nameResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             ->getNameContext() | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             ->getResolvedClassName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     public function resolveString(String_ $string): Name | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $name = new FullyQualified( | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             ltrim($string->value, '\\'), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             $string->getAttributes(), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         return $this->nameResolver | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ->getNameContext() | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ->getResolvedClassName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private function resolveFunctionIdentifier(Identifier $identifier): Name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $nameParts = array_filter([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->nameResolver->getNameContext()->getNamespace(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $identifier->toString(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return new FullyQualified( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             implode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 '\\', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $nameParts, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ), | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |             $identifier->getAttributes(), | 
            
                                                                        
                                                                
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |