| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ComposerRequireChecker\NodeVisitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PhpParser\NodeVisitorAbstract; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | final class UsedSymbolCollector extends NodeVisitorAbstract | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * @var mixed[] | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $collectedSymbols = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 24 |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @return string[] | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 24 |  |     public function getCollectedSymbols(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 24 |  |         return array_keys($this->collectedSymbols); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * {@inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 5 |  |     public function beforeTraverse(array $nodes) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 5 |  |         $this->collectedSymbols = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 5 |  |         return parent::beforeTraverse($nodes); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * {@inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 24 |  |     public function enterNode(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 24 |  |         $this->recordExtendsUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 24 |  |         $this->recordImplementsUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 24 |  |         $this->recordClassExpressionUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 24 |  |         $this->recordCatchUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 24 |  |         $this->recordFunctionCallUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 24 |  |         $this->recordFunctionParameterTypesUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 24 |  |         $this->recordFunctionReturnTypeUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 24 |  |         $this->recordConstantFetchUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 24 |  |         $this->recordTraitUsage($node); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 24 |  |         return parent::enterNode($node); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 24 |  |     private function recordExtendsUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 24 |  |         if ($node instanceof Node\Stmt\Class_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 7 |  |             array_map([$this, 'recordUsageOf'], array_filter([$node->extends])); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 24 |  |         if ($node instanceof Node\Stmt\Interface_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 5 |  |             array_map([$this, 'recordUsageOf'], array_filter($node->extends)); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 24 |  |     private function recordImplementsUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 24 |  |         if ($node instanceof Node\Stmt\Class_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 7 |  |             array_map([$this, 'recordUsageOf'], $node->implements); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 24 |  |     private function recordClassExpressionUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 24 |  |         if (($node instanceof Node\Expr\StaticCall | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 23 |  |                 || $node instanceof Node\Expr\StaticPropertyFetch | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 22 |  |                 || $node instanceof Node\Expr\ClassConstFetch | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 21 |  |                 || $node instanceof Node\Expr\New_ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 24 |  |                 || $node instanceof Node\Expr\Instanceof_ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 24 |  |             && $node->class instanceof Node\Name | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 9 |  |             $this->recordUsageOf($node->class); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 24 |  |     private function recordCatchUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 24 |  |         if ($node instanceof Node\Stmt\Catch_) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 5 |  |             foreach ($node->types as $type) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 5 |  |                 $this->recordUsageOf($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 24 |  |     private function recordFunctionCallUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 24 |  |         if ($node instanceof Node\Expr\FuncCall | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 24 |  |             && $node->name instanceof Node\Name | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 5 |  |             $this->recordUsageOf($node->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 24 |  |     private function recordFunctionParameterTypesUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 24 |  |         if ($node instanceof Node\Stmt\Function_ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 24 |  |             || $node instanceof Node\Stmt\ClassMethod | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 10 |  |             foreach ($node->getParams() as $param) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 8 |  |                 if ($param->type instanceof Node\Name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 6 |  |                     $this->recordUsageOf($param->type); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 8 |  |                 if (is_string($param->type) || $param->type instanceof Node\Identifier) { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 6 |  |                     $this->recordUsageOfByString($param->type); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 24 |  |     private function recordFunctionReturnTypeUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 24 |  |         if ($node instanceof Node\Stmt\Function_ | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 24 |  |             || $node instanceof Node\Stmt\ClassMethod | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 10 |  |             $returnType = $node->getReturnType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 10 |  |             if ($returnType instanceof Node\Name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 6 |  |                 $this->recordUsageOf($returnType); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 10 |  |             if ($returnType instanceof Node\Identifier) { | 
            
                                                                        
                            
            
                                    
            
            
                | 132 | 4 |  |                 $this->recordUsageOfByString($returnType->toString()); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 24 |  |     private function recordConstantFetchUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 24 |  |         if ($node instanceof Node\Expr\ConstFetch) { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 5 |  |             $this->recordUsageOf($node->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 24 |  |     private function recordTraitUsage(Node $node) | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 24 |  |         if (!$node instanceof Node\Stmt\TraitUse) { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 22 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 2 |  |         array_map([$this, 'recordUsageOf'], $node->traits); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 2 |  |         foreach ($node->adaptations as $adaptation) { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 1 |  |             if (null !== $adaptation->trait) { | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |                 $this->recordUsageOf($adaptation->trait); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 1 |  |             if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Precedence) { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |                 array_map([$this, 'recordUsageOf'], $adaptation->insteadof); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      * @param Node\Name $symbol | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 168 | 22 |  |     private function recordUsageOf(Node\Name $symbol) | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 22 |  |         $this->collectedSymbols[(string)$symbol] = $symbol; | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 22 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |      * @param string $symbol | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 6 |  |     private function recordUsageOfByString(string $symbol) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 | 6 |  |         $this->collectedSymbols[$symbol] = $symbol; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 181 | 6 |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 182 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 183 |  |  |  | 
            
                        
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.