|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespaceSymfony\Component\PropertyInfo\Extractor; |
| 13 | + |
| 14 | +usePHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode; |
| 15 | +usePHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 16 | +usePHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; |
| 17 | +usePHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 18 | +usePHPStan\PhpDocParser\Lexer\Lexer; |
| 19 | +usePHPStan\PhpDocParser\Parser\ConstExprParser; |
| 20 | +usePHPStan\PhpDocParser\Parser\PhpDocParser; |
| 21 | +usePHPStan\PhpDocParser\Parser\TokenIterator; |
| 22 | +usePHPStan\PhpDocParser\Parser\TypeParser; |
| 23 | +useSymfony\Component\PropertyInfo\PhpStan\NameScope; |
| 24 | +useSymfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
| 25 | +useSymfony\Component\PropertyInfo\Type; |
| 26 | +useSymfony\Component\PropertyInfo\Util\PhpStanTypeHelper; |
| 27 | + |
| 28 | +/** |
| 29 | + * Extracts data using PhpStan parser. |
| 30 | + * |
| 31 | + * @author Baptiste Leduc <baptiste.leduc@gmail.com> |
| 32 | + */ |
| 33 | +finalclass PhpStanExtractorimplements PropertyTypeExtractorInterface, ConstructorArgumentTypeExtractorInterface |
| 34 | +{ |
| 35 | +publicconstPROPERTY =0; |
| 36 | +publicconstACCESSOR =1; |
| 37 | +publicconstMUTATOR =2; |
| 38 | + |
| 39 | +/** @var PhpDocParser */ |
| 40 | +private$phpDocParser; |
| 41 | + |
| 42 | +/** @var Lexer */ |
| 43 | +private$lexer; |
| 44 | + |
| 45 | +private$docBlocks = []; |
| 46 | +private$phpStanTypeHelper; |
| 47 | +private$mutatorPrefixes; |
| 48 | +private$accessorPrefixes; |
| 49 | +private$arrayMutatorPrefixes; |
| 50 | + |
| 51 | +publicfunction__construct(array$mutatorPrefixes =null,array$accessorPrefixes =null,array$arrayMutatorPrefixes =null) |
| 52 | + { |
| 53 | +$this->phpStanTypeHelper =newPhpStanTypeHelper(); |
| 54 | +$this->mutatorPrefixes =null !==$mutatorPrefixes ?$mutatorPrefixes : ReflectionExtractor::$defaultMutatorPrefixes; |
| 55 | +$this->accessorPrefixes =null !==$accessorPrefixes ?$accessorPrefixes : ReflectionExtractor::$defaultAccessorPrefixes; |
| 56 | +$this->arrayMutatorPrefixes =null !==$arrayMutatorPrefixes ?$arrayMutatorPrefixes : ReflectionExtractor::$defaultArrayMutatorPrefixes; |
| 57 | + |
| 58 | +$constExprParser =newConstExprParser(); |
| 59 | +$this->phpDocParser =newPhpDocParser(newTypeParser($constExprParser),$constExprParser); |
| 60 | +$this->lexer =newLexer(); |
| 61 | + } |
| 62 | + |
| 63 | +/** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | +publicfunctiongetTypes(string$class,string$property,array$context = []) |
| 67 | + { |
| 68 | +/** @var $docNode PhpDocNode */ |
| 69 | + [$docNode,$source,$prefix] =$this->getDocBlock($class,$property); |
| 70 | +$nameScope =newNameScope($class); |
| 71 | +if (null ===$docNode) { |
| 72 | +returnnull; |
| 73 | + } |
| 74 | + |
| 75 | +switch ($source) { |
| 76 | +caseself::PROPERTY: |
| 77 | +$tag ='@var'; |
| 78 | +break; |
| 79 | + |
| 80 | +caseself::ACCESSOR: |
| 81 | +$tag ='@return'; |
| 82 | +break; |
| 83 | + |
| 84 | +caseself::MUTATOR: |
| 85 | +$tag ='@param'; |
| 86 | +break; |
| 87 | + } |
| 88 | + |
| 89 | +$parentClass =null; |
| 90 | +$types = []; |
| 91 | +foreach ($docNode->getTagsByName($tag)as$tagDocNode) { |
| 92 | +if (!$tagDocNode->valueinstanceof InvalidTagValueNode) { |
| 93 | +foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value,$nameScope)as$type) { |
| 94 | +switch ($type->getClassName()) { |
| 95 | +case'self': |
| 96 | +case'static': |
| 97 | +$resolvedClass =$class; |
| 98 | +break; |
| 99 | + |
| 100 | +case'parent': |
| 101 | +if (false !==$resolvedClass =$parentClass ??$parentClass =get_parent_class($class)) { |
| 102 | +break; |
| 103 | + } |
| 104 | +// no break |
| 105 | + |
| 106 | +default: |
| 107 | +$types[] =$type; |
| 108 | +continue2; |
| 109 | + } |
| 110 | + |
| 111 | +$types[] =newType(Type::BUILTIN_TYPE_OBJECT,$type->isNullable(),$resolvedClass,$type->isCollection(),$type->getCollectionKeyTypes(),$type->getCollectionValueTypes()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +if (!isset($types[0])) { |
| 117 | +returnnull; |
| 118 | + } |
| 119 | + |
| 120 | +if (!\in_array($prefix,$this->arrayMutatorPrefixes)) { |
| 121 | +return$types; |
| 122 | + } |
| 123 | + |
| 124 | +return [newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),$types[0])]; |
| 125 | + } |
| 126 | + |
| 127 | +/** |
| 128 | + * {@inheritdoc} |
| 129 | + */ |
| 130 | +publicfunctiongetTypesFromConstructor(string$class,string$property): ?array |
| 131 | + { |
| 132 | +$tagDocNode =$this->getDocBlockFromConstructor($class,$property); |
| 133 | +$nameScope =newNameScope($class); |
| 134 | +if (null ===$tagDocNode) { |
| 135 | +returnnull; |
| 136 | + } |
| 137 | + |
| 138 | +$types = []; |
| 139 | +foreach ($this->phpStanTypeHelper->getTypes($tagDocNode,$nameScope)as$type) { |
| 140 | +$types[] =$type; |
| 141 | + } |
| 142 | + |
| 143 | +if (!isset($types[0])) { |
| 144 | +returnnull; |
| 145 | + } |
| 146 | + |
| 147 | +return$types; |
| 148 | + } |
| 149 | + |
| 150 | +privatefunctiongetDocBlockFromConstructor(string$class,string$property): ?ParamTagValueNode |
| 151 | + { |
| 152 | +try { |
| 153 | +$reflectionClass =new \ReflectionClass($class); |
| 154 | + }catch (\ReflectionException$e) { |
| 155 | +returnnull; |
| 156 | + } |
| 157 | + |
| 158 | +$reflectionConstructor =$reflectionClass->getConstructor(); |
| 159 | +if (null ===$reflectionConstructor) { |
| 160 | +returnnull; |
| 161 | + } |
| 162 | + |
| 163 | +$rawDocNode =$reflectionConstructor->getDocComment(); |
| 164 | +$tokens =newTokenIterator($this->lexer->tokenize($rawDocNode)); |
| 165 | +$phpDocNode =$this->phpDocParser->parse($tokens); |
| 166 | +$tokens->consumeTokenType(Lexer::TOKEN_END); |
| 167 | + |
| 168 | +return$this->filterDocBlockParams($phpDocNode,$property); |
| 169 | + } |
| 170 | + |
| 171 | +privatefunctionfilterDocBlockParams(PhpDocNode$docNode,string$allowedParam): ?ParamTagValueNode |
| 172 | + { |
| 173 | +$tags =array_values(array_filter($docNode->getTagsByName('@param'),function ($tagNode)use ($allowedParam) { |
| 174 | +return$tagNodeinstanceof PhpDocTagNode &&sprintf('$%s',$allowedParam) ===$tagNode->value->parameterName; |
| 175 | + })); |
| 176 | + |
| 177 | +if (\count($tags) <=0) { |
| 178 | +returnnull; |
| 179 | + } |
| 180 | + |
| 181 | +return$tags[0]->value; |
| 182 | + } |
| 183 | + |
| 184 | +privatefunctiongetDocBlock(string$class,string$property):array |
| 185 | + { |
| 186 | +$propertyHash =sprintf('%s::%s',$class,$property); |
| 187 | + |
| 188 | +if (isset($this->docBlocks[$propertyHash])) { |
| 189 | +return$this->docBlocks[$propertyHash]; |
| 190 | + } |
| 191 | + |
| 192 | +$ucFirstProperty =ucfirst($property); |
| 193 | + |
| 194 | +switch (true) { |
| 195 | +case$docBlock =$this->getDocBlockFromProperty($class,$property): |
| 196 | +$data = [$docBlock,self::PROPERTY,null]; |
| 197 | +break; |
| 198 | + |
| 199 | +case [$docBlock] =$this->getDocBlockFromMethod($class,$ucFirstProperty,self::ACCESSOR): |
| 200 | +$data = [$docBlock,self::ACCESSOR,null]; |
| 201 | +break; |
| 202 | + |
| 203 | +case [$docBlock,$prefix] =$this->getDocBlockFromMethod($class,$ucFirstProperty,self::MUTATOR): |
| 204 | +$data = [$docBlock,self::MUTATOR,$prefix]; |
| 205 | +break; |
| 206 | + |
| 207 | +default: |
| 208 | +$data = [null,null,null]; |
| 209 | + } |
| 210 | + |
| 211 | +return$this->docBlocks[$propertyHash] =$data; |
| 212 | + } |
| 213 | + |
| 214 | +privatefunctiongetDocBlockFromProperty(string$class,string$property): ?PhpDocNode |
| 215 | + { |
| 216 | +// Use a ReflectionProperty instead of $class to get the parent class if applicable |
| 217 | +try { |
| 218 | +$reflectionProperty =new \ReflectionProperty($class,$property); |
| 219 | + }catch (\ReflectionException$e) { |
| 220 | +returnnull; |
| 221 | + } |
| 222 | + |
| 223 | +$rawDocNode =$reflectionProperty->getDocComment() ?:null; |
| 224 | +if (null ===$rawDocNode) { |
| 225 | +returnnull; |
| 226 | + } |
| 227 | + |
| 228 | +$tokens =newTokenIterator($this->lexer->tokenize($rawDocNode)); |
| 229 | +$phpDocNode =$this->phpDocParser->parse($tokens); |
| 230 | +$tokens->consumeTokenType(Lexer::TOKEN_END); |
| 231 | + |
| 232 | +return$phpDocNode; |
| 233 | + } |
| 234 | + |
| 235 | +privatefunctiongetDocBlockFromMethod(string$class,string$ucFirstProperty,int$type): ?array |
| 236 | + { |
| 237 | +$prefixes =self::ACCESSOR ===$type ?$this->accessorPrefixes :$this->mutatorPrefixes; |
| 238 | +$prefix =null; |
| 239 | + |
| 240 | +foreach ($prefixesas$prefix) { |
| 241 | +$methodName =$prefix.$ucFirstProperty; |
| 242 | + |
| 243 | +try { |
| 244 | +$reflectionMethod =new \ReflectionMethod($class,$methodName); |
| 245 | +if ($reflectionMethod->isStatic()) { |
| 246 | +continue; |
| 247 | + } |
| 248 | + |
| 249 | +if ( |
| 250 | + (self::ACCESSOR ===$type &&0 ===$reflectionMethod->getNumberOfRequiredParameters()) || |
| 251 | + (self::MUTATOR ===$type &&$reflectionMethod->getNumberOfParameters() >=1) |
| 252 | + ) { |
| 253 | +break; |
| 254 | + } |
| 255 | + }catch (\ReflectionException$e) { |
| 256 | +// Try the next prefix if the method doesn't exist |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | +if (!isset($reflectionMethod)) { |
| 261 | +returnnull; |
| 262 | + } |
| 263 | + |
| 264 | +$rawDocNode =$reflectionMethod->getDocComment() ?:null; |
| 265 | +if (null ===$rawDocNode) { |
| 266 | +returnnull; |
| 267 | + } |
| 268 | + |
| 269 | +$tokens =newTokenIterator($this->lexer->tokenize($rawDocNode)); |
| 270 | +$phpDocNode =$this->phpDocParser->parse($tokens); |
| 271 | +$tokens->consumeTokenType(Lexer::TOKEN_END); |
| 272 | + |
| 273 | +return [$phpDocNode,$prefix]; |
| 274 | + } |
| 275 | +} |