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

Commit96094ad

Browse files
committed
feature #53160 [PropertyInfo] Deprecate PropertyInfo Type (mtarld)
This PR was merged into the 7.1 branch.Discussion----------[PropertyInfo] Deprecate PropertyInfo Type| Q | A| ------------- | ---| Branch? | 7.1| Bug fix? | no| New feature? | no| Deprecations? | yes| Issues || License | MITThis PR is a follow-up ofsymfony/symfony#52510.As the TypeInfo's `Type` aims to represent types in the Symfony ecosystem, the PropertyInfo's `Type` needs to be deprecated in favor of the first one.Commits-------d32e81c816 [PropertyInfo] Deprecate PropertyInfo Type
2 parents8d32db6 +41b5881 commit96094ad

21 files changed

+2114
-467
lines changed

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Introduce`PropertyDocBlockExtractorInterface` to extract a property's doc block
88
* Restrict access to`PhpStanExtractor` based on visibility
9+
* Deprecate the`Type` class, use`Symfony\Component\TypeInfo\Type` class of`symfony/type-info` component instead
10+
* Deprecate the`PropertyTypeExtractorInterface::getTypes()` method, use`PropertyTypeExtractorInterface::getType()` instead
911

1012
6.4
1113
---

‎Extractor/ConstructorArgumentTypeExtractorInterface.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespaceSymfony\Component\PropertyInfo\Extractor;
1313

14-
useSymfony\Component\PropertyInfo\Type;
14+
useSymfony\Component\PropertyInfo\TypeasLegacyType;
15+
useSymfony\Component\TypeInfo\Type;
1516

1617
/**
1718
* Infers the constructor argument type.
@@ -25,9 +26,20 @@ interface ConstructorArgumentTypeExtractorInterface
2526
/**
2627
* Gets types of an argument from constructor.
2728
*
28-
* @return Type[]|null
29+
* @return LegacyType[]|null
30+
*
31+
* @deprecated since Symfony 7.1, use "getTypeFromConstructor" instead
2932
*
3033
* @internal
3134
*/
3235
publicfunctiongetTypesFromConstructor(string$class,string$property): ?array;
36+
37+
/**
38+
* Gets type of an argument from constructor.
39+
*
40+
* @param class-string $class
41+
*
42+
* @internal
43+
*/
44+
publicfunctiongetTypeFromConstructor(string$class,string$property): ?Type;
3345
}

‎Extractor/ConstructorExtractor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\PropertyInfo\Extractor;
1313

1414
useSymfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
15+
useSymfony\Component\TypeInfo\Type;
1516

1617
/**
1718
* Extracts the constructor argument type using ConstructorArgumentTypeExtractorInterface implementations.
@@ -28,8 +29,24 @@ public function __construct(
2829
) {
2930
}
3031

32+
publicfunctiongetType(string$class,string$property,array$context = []): ?Type
33+
{
34+
foreach ($this->extractorsas$extractor) {
35+
if (null !==$value =$extractor->getTypeFromConstructor($class,$property)) {
36+
return$value;
37+
}
38+
}
39+
40+
returnnull;
41+
}
42+
43+
/**
44+
* @deprecated since Symfony 7.1, use "getType" instead
45+
*/
3146
publicfunctiongetTypes(string$class,string$property,array$context = []): ?array
3247
{
48+
trigger_deprecation('symfony/property-info','7.1','The "%s()" method is deprecated, use "%s::getType()" instead.',__METHOD__,self::class);
49+
3350
foreach ($this->extractorsas$extractor) {
3451
$value =$extractor->getTypesFromConstructor($class,$property);
3552
if (null !==$value) {

‎Extractor/PhpDocExtractor.php

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
useSymfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
2121
useSymfony\Component\PropertyInfo\PropertyDocBlockExtractorInterface;
2222
useSymfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
23-
useSymfony\Component\PropertyInfo\Type;
23+
useSymfony\Component\PropertyInfo\TypeasLegacyType;
2424
useSymfony\Component\PropertyInfo\Util\PhpDocTypeHelper;
25+
useSymfony\Component\TypeInfo\Exception\LogicException;
26+
useSymfony\Component\TypeInfo\Type;
27+
useSymfony\Component\TypeInfo\Type\ObjectType;
28+
useSymfony\Component\TypeInfo\TypeContext\TypeContextFactory;
2529

2630
/**
2731
* Extracts data using a PHPDoc parser.
@@ -48,6 +52,7 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
4852

4953
privateDocBlockFactoryInterface$docBlockFactory;
5054
privateContextFactory$contextFactory;
55+
privateTypeContextFactory$typeContextFactory;
5156
privatePhpDocTypeHelper$phpDocTypeHelper;
5257
privatearray$mutatorPrefixes;
5358
privatearray$accessorPrefixes;
@@ -66,6 +71,7 @@ public function __construct(?DocBlockFactoryInterface $docBlockFactory = null, ?
6671

6772
$this->docBlockFactory =$docBlockFactory ?: DocBlockFactory::createInstance();
6873
$this->contextFactory =newContextFactory();
74+
$this->typeContextFactory =newTypeContextFactory();
6975
$this->phpDocTypeHelper =newPhpDocTypeHelper();
7076
$this->mutatorPrefixes =$mutatorPrefixes ?? ReflectionExtractor::$defaultMutatorPrefixes;
7177
$this->accessorPrefixes =$accessorPrefixes ?? ReflectionExtractor::$defaultAccessorPrefixes;
@@ -112,8 +118,13 @@ public function getLongDescription(string $class, string $property, array $conte
112118
return'' ===$contents ?null :$contents;
113119
}
114120

121+
/**
122+
* @deprecated since Symfony 7.1, use "getType" instead
123+
*/
115124
publicfunctiongetTypes(string$class,string$property,array$context = []): ?array
116125
{
126+
trigger_deprecation('symfony/property-info','7.1','The "%s()" method is deprecated, use "%s::getType()" instead.',__METHOD__,self::class);
127+
117128
/** @var $docBlock DocBlock */
118129
[$docBlock,$source,$prefix] =$this->findDocBlock($class,$property);
119130
if (!$docBlock) {
@@ -149,7 +160,7 @@ public function getTypes(string $class, string $property, array $context = []):
149160
continue2;
150161
}
151162

152-
$types[] =newType(Type::BUILTIN_TYPE_OBJECT,$type->isNullable(),$resolvedClass,$type->isCollection(),$type->getCollectionKeyTypes(),$type->getCollectionValueTypes());
163+
$types[] =newLegacyType(LegacyType::BUILTIN_TYPE_OBJECT,$type->isNullable(),$resolvedClass,$type->isCollection(),$type->getCollectionKeyTypes(),$type->getCollectionValueTypes());
153164
}
154165
}
155166
}
@@ -162,11 +173,16 @@ public function getTypes(string $class, string $property, array $context = []):
162173
return$types;
163174
}
164175

165-
return [newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),$types[0])];
176+
return [newLegacyType(LegacyType::BUILTIN_TYPE_ARRAY,false,null,true,newLegacyType(LegacyType::BUILTIN_TYPE_INT),$types[0])];
166177
}
167178

179+
/**
180+
* @deprecated since Symfony 7.1, use "getTypeFromConstructor" instead
181+
*/
168182
publicfunctiongetTypesFromConstructor(string$class,string$property): ?array
169183
{
184+
trigger_deprecation('symfony/property-info','7.1','The "%s()" method is deprecated, use "%s::getTypeFromConstructor()" instead.',__METHOD__,self::class);
185+
170186
$docBlock =$this->getDocBlockFromConstructor($class,$property);
171187

172188
if (!$docBlock) {
@@ -188,6 +204,84 @@ public function getTypesFromConstructor(string $class, string $property): ?array
188204
returnarray_merge([], ...$types);
189205
}
190206

207+
publicfunctiongetType(string$class,string$property,array$context = []): ?Type
208+
{
209+
/** @var $docBlock DocBlock */
210+
[$docBlock,$source,$prefix] =$this->findDocBlock($class,$property);
211+
if (!$docBlock) {
212+
returnnull;
213+
}
214+
215+
$tag =match ($source) {
216+
self::PROPERTY =>'var',
217+
self::ACCESSOR =>'return',
218+
self::MUTATOR =>'param',
219+
};
220+
221+
$types = [];
222+
$typeContext =$this->typeContextFactory->createFromClassName($class);
223+
224+
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
225+
foreach ($docBlock->getTagsByName($tag)as$tag) {
226+
if ($taginstanceof InvalidTag || !$tagType =$tag->getType()) {
227+
continue;
228+
}
229+
230+
$type =$this->phpDocTypeHelper->getType($tagType);
231+
232+
if (!$typeinstanceof ObjectType) {
233+
$types[] =$type;
234+
235+
continue;
236+
}
237+
238+
$normalizedClassName =match ($type->getClassName()) {
239+
'self' =>$typeContext->getDeclaringClass(),
240+
'static' =>$typeContext->getCalledClass(),
241+
default =>$type->getClassName(),
242+
};
243+
244+
if ('parent' ===$normalizedClassName) {
245+
try {
246+
$normalizedClassName =$typeContext->getParentClass();
247+
}catch (LogicException) {
248+
// if there is no parent for the current class, we keep the "parent" raw string
249+
}
250+
}
251+
252+
$types[] =$type->isNullable() ? Type::nullable(Type::object($normalizedClassName)) : Type::object($normalizedClassName);
253+
}
254+
255+
if (null ===$type =$types[0] ??null) {
256+
returnnull;
257+
}
258+
259+
if (!\in_array($prefix,$this->arrayMutatorPrefixes,true)) {
260+
return$type;
261+
}
262+
263+
return Type::list($type);
264+
}
265+
266+
publicfunctiongetTypeFromConstructor(string$class,string$property): ?Type
267+
{
268+
if (!$docBlock =$this->getDocBlockFromConstructor($class,$property)) {
269+
returnnull;
270+
}
271+
272+
$types = [];
273+
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
274+
foreach ($docBlock->getTagsByName('param')as$tag) {
275+
if ($taginstanceof InvalidTag || !$tagType =$tag->getType()) {
276+
continue;
277+
}
278+
279+
$types[] =$this->phpDocTypeHelper->getType($tagType);
280+
}
281+
282+
return$types[0] ??null;
283+
}
284+
191285
publicfunctiongetDocBlock(string$class,string$property): ?DocBlock
192286
{
193287
$output =$this->findDocBlock($class,$property);

‎Extractor/PhpStanExtractor.php

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
usePHPStan\PhpDocParser\Parser\TypeParser;
2424
useSymfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
2525
useSymfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
26-
useSymfony\Component\PropertyInfo\Type;
26+
useSymfony\Component\PropertyInfo\TypeasLegacyType;
2727
useSymfony\Component\PropertyInfo\Util\PhpStanTypeHelper;
28+
useSymfony\Component\TypeInfo\Exception\UnsupportedException;
29+
useSymfony\Component\TypeInfo\Type;
30+
useSymfony\Component\TypeInfo\TypeContext\TypeContextFactory;
31+
useSymfony\Component\TypeInfo\TypeResolver\StringTypeResolver;
2832

2933
/**
3034
* Extracts data using PHPStan parser.
@@ -41,6 +45,9 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
4145
privateLexer$lexer;
4246
privateNameScopeFactory$nameScopeFactory;
4347

48+
privateStringTypeResolver$stringTypeResolver;
49+
privateTypeContextFactory$typeContextFactory;
50+
4451
/** @var array<string, array{PhpDocNode|null, int|null, string|null, string|null}> */
4552
privatearray$docBlocks = [];
4653
privatePhpStanTypeHelper$phpStanTypeHelper;
@@ -71,10 +78,17 @@ public function __construct(?array $mutatorPrefixes = null, ?array $accessorPref
7178
$this->phpDocParser =newPhpDocParser(newTypeParser(newConstExprParser()),newConstExprParser());
7279
$this->lexer =newLexer();
7380
$this->nameScopeFactory =newNameScopeFactory();
81+
$this->stringTypeResolver =newStringTypeResolver();
82+
$this->typeContextFactory =newTypeContextFactory($this->stringTypeResolver);
7483
}
7584

85+
/**
86+
* @deprecated since Symfony 7.1, use "getType" instead
87+
*/
7688
publicfunctiongetTypes(string$class,string$property,array$context = []): ?array
7789
{
90+
trigger_deprecation('symfony/property-info','7.1','The "%s()" method is deprecated, use "%s::getType()" instead.',__METHOD__,self::class);
91+
7892
/** @var PhpDocNode|null $docNode */
7993
[$docNode,$source,$prefix,$declaringClass] =$this->getDocBlock($class,$property);
8094
$nameScope =$this->nameScopeFactory->create($class,$declaringClass);
@@ -129,7 +143,7 @@ public function getTypes(string $class, string $property, array $context = []):
129143
continue2;
130144
}
131145

132-
$types[] =newType(Type::BUILTIN_TYPE_OBJECT,$type->isNullable(),$resolvedClass,$type->isCollection(),$type->getCollectionKeyTypes(),$type->getCollectionValueTypes());
146+
$types[] =newLegacyType(LegacyType::BUILTIN_TYPE_OBJECT,$type->isNullable(),$resolvedClass,$type->isCollection(),$type->getCollectionKeyTypes(),$type->getCollectionValueTypes());
133147
}
134148
}
135149

@@ -141,11 +155,18 @@ public function getTypes(string $class, string $property, array $context = []):
141155
return$types;
142156
}
143157

144-
return [newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),$types[0])];
158+
return [newLegacyType(LegacyType::BUILTIN_TYPE_ARRAY,false,null,true,newLegacyType(LegacyType::BUILTIN_TYPE_INT),$types[0])];
145159
}
146160

161+
/**
162+
* @deprecated since Symfony 7.1, use "getTypeFromConstructor" instead
163+
*
164+
* @return LegacyType[]|null
165+
*/
147166
publicfunctiongetTypesFromConstructor(string$class,string$property): ?array
148167
{
168+
trigger_deprecation('symfony/property-info','7.1','The "%s()" method is deprecated, use "%s::getTypeFromConstructor()" instead.',__METHOD__,self::class);
169+
149170
if (null ===$tagDocNode =$this->getDocBlockFromConstructor($class,$property)) {
150171
returnnull;
151172
}
@@ -162,6 +183,63 @@ public function getTypesFromConstructor(string $class, string $property): ?array
162183
return$types;
163184
}
164185

186+
publicfunctiongetType(string$class,string$property,array$context = []): ?Type
187+
{
188+
/** @var PhpDocNode|null $docNode */
189+
[$docNode,$source,$prefix,$declaringClass] =$this->getDocBlock($class,$property);
190+
191+
if (null ===$docNode) {
192+
returnnull;
193+
}
194+
195+
$typeContext =$this->typeContextFactory->createFromClassName($class,$declaringClass);
196+
197+
$tag =match ($source) {
198+
self::PROPERTY =>'@var',
199+
self::ACCESSOR =>'@return',
200+
self::MUTATOR =>'@param',
201+
default =>'invalid',
202+
};
203+
204+
$types = [];
205+
206+
foreach ($docNode->getTagsByName($tag)as$tagDocNode) {
207+
if ($tagDocNode->valueinstanceof InvalidTagValueNode) {
208+
continue;
209+
}
210+
211+
if ($tagDocNode->valueinstanceof ParamTagValueNode &&null ===$prefix &&$tagDocNode->value->parameterName !=='$'.$property) {
212+
continue;
213+
}
214+
215+
try {
216+
$types[] =$this->stringTypeResolver->resolve((string)$tagDocNode->value->type,$typeContext);
217+
}catch (UnsupportedException) {
218+
}
219+
}
220+
221+
if (!$type =$types[0] ??null) {
222+
returnnull;
223+
}
224+
225+
if (!\in_array($prefix,$this->arrayMutatorPrefixes,true)) {
226+
return$type;
227+
}
228+
229+
return Type::list($type);
230+
}
231+
232+
publicfunctiongetTypeFromConstructor(string$class,string$property): ?Type
233+
{
234+
if (!$tagDocNode =$this->getDocBlockFromConstructor($class,$property)) {
235+
returnnull;
236+
}
237+
238+
$typeContext =$this->typeContextFactory->createFromClassName($class);
239+
240+
return$this->stringTypeResolver->resolve((string)$tagDocNode->type,$typeContext);
241+
}
242+
165243
privatefunctiongetDocBlockFromConstructor(string$class,string$property): ?ParamTagValueNode
166244
{
167245
try {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp