-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] convert legacy types to TypeInfo types if getType() is not implemented #57459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,13 @@ | |
namespace Symfony\Component\PropertyInfo\Tests; | ||
|
||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy; | ||
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy; | ||
use Symfony\Component\TypeInfo\Type; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
|
@@ -76,4 +82,90 @@ public function testIsInitializable() | |
parent::testIsInitializable(); | ||
parent::testIsInitializable(); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* | ||
* @dataProvider provideNestedExtractorWithoutGetTypeImplementationData | ||
*/ | ||
public function testNestedExtractorWithoutGetTypeImplementation(string $property, ?Type $expectedType) | ||
xabbuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$propertyInfoCacheExtractor = new PropertyInfoCacheExtractor(new class() implements PropertyInfoExtractorInterface { | ||
private PropertyTypeExtractorInterface $propertyTypeExtractor; | ||
|
||
public function __construct() | ||
{ | ||
$this->propertyTypeExtractor = new PhpDocExtractor(); | ||
} | ||
|
||
public function getTypes(string $class, string $property, array $context = []): ?array | ||
{ | ||
return $this->propertyTypeExtractor->getTypes($class, $property, $context); | ||
} | ||
|
||
public function isReadable(string $class, string $property, array $context = []): ?bool | ||
{ | ||
return null; | ||
} | ||
|
||
public function isWritable(string $class, string $property, array $context = []): ?bool | ||
{ | ||
return null; | ||
} | ||
|
||
public function getShortDescription(string $class, string $property, array $context = []): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getLongDescription(string $class, string $property, array $context = []): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getProperties(string $class, array $context = []): ?array | ||
{ | ||
return null; | ||
} | ||
}, new ArrayAdapter()); | ||
|
||
if (null === $expectedType) { | ||
$this->assertNull($propertyInfoCacheExtractor->getType(Dummy::class, $property)); | ||
} else { | ||
$this->assertEquals($expectedType, $propertyInfoCacheExtractor->getType(Dummy::class, $property)); | ||
} | ||
} | ||
|
||
public function provideNestedExtractorWithoutGetTypeImplementationData() | ||
{ | ||
yield ['bar', Type::string()]; | ||
yield ['baz', Type::int()]; | ||
yield ['bal', Type::object(\DateTimeImmutable::class)]; | ||
yield ['parent', Type::object(ParentDummy::class)]; | ||
yield ['collection', Type::array(Type::object(\DateTimeImmutable::class), Type::int())]; | ||
yield ['nestedCollection', Type::array(Type::array(Type::string(), Type::int()), Type::int())]; | ||
yield ['mixedCollection', Type::array()]; | ||
yield ['B', Type::object(ParentDummy::class)]; | ||
yield ['Id', Type::int()]; | ||
yield ['Guid', Type::string()]; | ||
yield ['g', Type::nullable(Type::array())]; | ||
yield ['h', Type::nullable(Type::string())]; | ||
yield ['i', Type::nullable(Type::union(Type::string(), Type::int()))]; | ||
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))]; | ||
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::array(Type::int(), Type::int()))]; | ||
yield ['nonNullableCollectionOfNullableElements', Type::array(Type::nullable(Type::int()), Type::int())]; | ||
yield ['nullableCollectionOfMultipleNonNullableElementTypes', Type::nullable(Type::array(Type::union(Type::int(), Type::string()), Type::int()))]; | ||
yield ['xTotals', Type::array()]; | ||
yield ['YT', Type::string()]; | ||
yield ['emptyVar', null]; | ||
yield ['iteratorCollection', Type::collection(Type::object(\Iterator::class), Type::string(), Type::union(Type::string(), Type::int()))]; | ||
yield ['iteratorCollectionWithKey', Type::collection(Type::object(\Iterator::class), Type::string(), Type::int())]; | ||
yield ['nestedIterators', Type::collection(Type::object(\Iterator::class), Type::collection(Type::object(\Iterator::class), Type::string(), Type::int()), Type::int())]; | ||
yield ['arrayWithKeys', Type::array(Type::string(), Type::string())]; | ||
yield ['arrayWithKeysAndComplexValue', Type::array(Type::nullable(Type::array(Type::nullable(Type::string()), Type::int())), Type::string())]; | ||
yield ['arrayOfMixed', Type::array(Type::mixed(), Type::string())]; | ||
yield ['noDocBlock', null]; | ||
yield ['listOfStrings', Type::array(Type::string(), Type::int())]; | ||
yield ['parentAnnotation', Type::object(ParentDummy::class)]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,76 @@ | |
|
||
namespace Symfony\Component\PropertyInfo\Tests; | ||
|
||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy; | ||
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy; | ||
use Symfony\Component\TypeInfo\Type; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
class PropertyInfoExtractorTest extends AbstractPropertyInfoExtractorTest | ||
{ | ||
/** | ||
* @group legacy | ||
* | ||
* @dataProvider provideNestedExtractorWithoutGetTypeImplementationData | ||
*/ | ||
public function testNestedExtractorWithoutGetTypeImplementation(string $property, ?Type $expectedType) | ||
{ | ||
$propertyInfoExtractor = new PropertyInfoExtractor([], [new class() implements PropertyTypeExtractorInterface { | ||
private PropertyTypeExtractorInterface $propertyTypeExtractor; | ||
|
||
public function __construct() | ||
{ | ||
$this->propertyTypeExtractor = new PhpDocExtractor(); | ||
} | ||
|
||
public function getTypes(string $class, string $property, array $context = []): ?array | ||
{ | ||
return $this->propertyTypeExtractor->getTypes($class, $property, $context); | ||
} | ||
}]); | ||
|
||
if (null === $expectedType) { | ||
$this->assertNull($propertyInfoExtractor->getType(Dummy::class, $property)); | ||
} else { | ||
$this->assertEquals($expectedType, $propertyInfoExtractor->getType(Dummy::class, $property)); | ||
} | ||
} | ||
|
||
public function provideNestedExtractorWithoutGetTypeImplementationData() | ||
{ | ||
yield ['bar', Type::string()]; | ||
yield ['baz', Type::int()]; | ||
yield ['bal', Type::object(\DateTimeImmutable::class)]; | ||
yield ['parent', Type::object(ParentDummy::class)]; | ||
yield ['collection', Type::array(Type::object(\DateTimeImmutable::class), Type::int())]; | ||
yield ['nestedCollection', Type::array(Type::array(Type::string(), Type::int()), Type::int())]; | ||
yield ['mixedCollection', Type::array()]; | ||
yield ['B', Type::object(ParentDummy::class)]; | ||
yield ['Id', Type::int()]; | ||
yield ['Guid', Type::string()]; | ||
yield ['g', Type::nullable(Type::array())]; | ||
yield ['h', Type::nullable(Type::string())]; | ||
yield ['i', Type::nullable(Type::union(Type::string(), Type::int()))]; | ||
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))]; | ||
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::array(Type::int(), Type::int()))]; | ||
yield ['nonNullableCollectionOfNullableElements', Type::array(Type::nullable(Type::int()), Type::int())]; | ||
yield ['nullableCollectionOfMultipleNonNullableElementTypes', Type::nullable(Type::array(Type::union(Type::int(), Type::string()), Type::int()))]; | ||
yield ['xTotals', Type::array()]; | ||
yield ['YT', Type::string()]; | ||
yield ['emptyVar', null]; | ||
yield ['iteratorCollection', Type::collection(Type::object(\Iterator::class), Type::string(), Type::union(Type::string(), Type::int()))]; | ||
yield ['iteratorCollectionWithKey', Type::collection(Type::object(\Iterator::class), Type::string(), Type::int())]; | ||
yield ['nestedIterators', Type::collection(Type::object(\Iterator::class), Type::collection(Type::object(\Iterator::class), Type::string(), Type::int()), Type::int())]; | ||
yield ['arrayWithKeys', Type::array(Type::string(), Type::string())]; | ||
yield ['arrayWithKeysAndComplexValue', Type::array(Type::nullable(Type::array(Type::nullable(Type::string()), Type::int())), Type::string())]; | ||
yield ['arrayOfMixed', Type::array(Type::mixed(), Type::string())]; | ||
yield ['noDocBlock', null]; | ||
yield ['listOfStrings', Type::array(Type::string(), Type::int())]; | ||
yield ['parentAnnotation', Type::object(ParentDummy::class)]; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Symfony/Component/PropertyInfo/Util/LegacyTypeConverter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo\Util; | ||
|
||
use Symfony\Component\PropertyInfo\Type as LegacyType; | ||
use Symfony\Component\TypeInfo\Type; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class LegacyTypeConverter | ||
{ | ||
/** | ||
* @param LegacyType[]|null $legacyTypes | ||
*/ | ||
public static function toTypeInfoType(?array $legacyTypes): ?Type | ||
{ | ||
if (null === $legacyTypes || [] === $legacyTypes) { | ||
return null; | ||
} | ||
|
||
$nullable = false; | ||
$types = []; | ||
|
||
foreach ($legacyTypes as $legacyType) { | ||
switch ($legacyType->getBuiltinType()) { | ||
case LegacyType::BUILTIN_TYPE_ARRAY: | ||
$typeInfoType = Type::array(self::toTypeInfoType($legacyType->getCollectionValueTypes()), self::toTypeInfoType($legacyType->getCollectionKeyTypes())); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_BOOL: | ||
$typeInfoType = Type::bool(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_CALLABLE: | ||
$typeInfoType = Type::callable(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_FALSE: | ||
$typeInfoType = Type::false(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_FLOAT: | ||
$typeInfoType = Type::float(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_INT: | ||
$typeInfoType = Type::int(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_ITERABLE: | ||
$typeInfoType = Type::iterable(self::toTypeInfoType($legacyType->getCollectionValueTypes()), self::toTypeInfoType($legacyType->getCollectionKeyTypes())); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_OBJECT: | ||
if ($legacyType->isCollection()) { | ||
$typeInfoType = Type::collection(Type::object($legacyType->getClassName()), self::toTypeInfoType($legacyType->getCollectionValueTypes()), self::toTypeInfoType($legacyType->getCollectionKeyTypes())); | ||
Check failure on line 59 in src/Symfony/Component/PropertyInfo/Util/LegacyTypeConverter.php
|
||
} else { | ||
$typeInfoType = Type::object($legacyType->getClassName()); | ||
} | ||
|
||
break; | ||
case LegacyType::BUILTIN_TYPE_RESOURCE: | ||
$typeInfoType = Type::resource(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_STRING: | ||
$typeInfoType = Type::string(); | ||
break; | ||
case LegacyType::BUILTIN_TYPE_TRUE: | ||
$typeInfoType = Type::true(); | ||
break; | ||
default: | ||
$typeInfoType = null; | ||
break; | ||
} | ||
|
||
if (LegacyType::BUILTIN_TYPE_NULL === $legacyType->getBuiltinType() || $legacyType->isNullable()) { | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$nullable = true; | ||
} | ||
|
||
if (null !== $typeInfoType) { | ||
$types[] = $typeInfoType; | ||
} | ||
} | ||
|
||
if (1 === \count($types)) { | ||
return $nullable ? Type::nullable($types[0]) : $types[0]; | ||
} | ||
|
||
return $nullable ? Type::nullable(Type::union(...$types)) : Type::union(...$types); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.