Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e08a5b

Browse files
committed
Add PhpStanExtractor
1 parent6c0102c commit2e08a5b

File tree

6 files changed

+761
-2
lines changed

6 files changed

+761
-2
lines changed

‎composer.json‎

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp