-
-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1192 from schmittjoh/infer-types-from-php-7.4
Infer types from PHP 7.4 type declarations
- Loading branch information
Showing
10 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Metadata\Driver; | ||
|
||
use JMS\Serializer\Metadata\ClassMetadata as SerializerClassMetadata; | ||
use JMS\Serializer\Metadata\ExpressionPropertyMetadata; | ||
use JMS\Serializer\Metadata\PropertyMetadata; | ||
use JMS\Serializer\Metadata\StaticPropertyMetadata; | ||
use JMS\Serializer\Metadata\VirtualPropertyMetadata; | ||
use JMS\Serializer\Type\Parser; | ||
use JMS\Serializer\Type\ParserInterface; | ||
use Metadata\ClassMetadata; | ||
use Metadata\Driver\DriverInterface; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use ReflectionProperty; | ||
|
||
class TypedPropertiesDriver implements DriverInterface | ||
{ | ||
/** | ||
* @var DriverInterface | ||
*/ | ||
protected $delegate; | ||
|
||
/** | ||
* @var ParserInterface | ||
*/ | ||
protected $typeParser; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $whiteList; | ||
|
||
/** | ||
* @param string[] $whiteList | ||
*/ | ||
public function __construct(DriverInterface $delegate, ?ParserInterface $typeParser = null, array $whiteList = []) | ||
{ | ||
$this->delegate = $delegate; | ||
$this->typeParser = $typeParser ?: new Parser(); | ||
$this->whiteList = array_merge($whiteList, $this->getDefaultWhiteList()); | ||
} | ||
|
||
private function getDefaultWhiteList(): array | ||
{ | ||
return [ | ||
'int', | ||
'float', | ||
'bool', | ||
'boolean', | ||
'string', | ||
'double', | ||
'iterable', | ||
'resource', | ||
]; | ||
} | ||
|
||
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata | ||
{ | ||
/** @var SerializerClassMetadata $classMetadata */ | ||
$classMetadata = $this->delegate->loadMetadataForClass($class); | ||
|
||
if (null === $classMetadata) { | ||
return null; | ||
} | ||
// We base our scan on the internal driver's property list so that we | ||
// respect any internal white/blacklisting like in the AnnotationDriver | ||
foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) { | ||
/** @var $propertyMetadata PropertyMetadata */ | ||
|
||
// If the inner driver provides a type, don't guess anymore. | ||
if ($propertyMetadata->type || $this->isVirtualProperty($propertyMetadata)) { | ||
continue; | ||
} | ||
|
||
try { | ||
$propertyReflection = $this->getReflection($propertyMetadata); | ||
if ($this->shouldTypeHint($propertyReflection)) { | ||
$propertyMetadata->setType($this->typeParser->parse($propertyReflection->getType()->getName())); | ||
} | ||
} catch (ReflectionException $e) { | ||
continue; | ||
} | ||
} | ||
|
||
return $classMetadata; | ||
} | ||
|
||
private function shouldTypeHint(ReflectionProperty $propertyReflection): bool | ||
{ | ||
if (null === $propertyReflection->getType()) { | ||
return false; | ||
} | ||
|
||
if (in_array($propertyReflection->getType()->getName(), $this->whiteList, true)) { | ||
return true; | ||
} | ||
|
||
if (class_exists($propertyReflection->getType()->getName())) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getReflection(PropertyMetadata $propertyMetadata): ReflectionProperty | ||
{ | ||
return new ReflectionProperty($propertyMetadata->class, $propertyMetadata->name); | ||
} | ||
|
||
private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $propertyMetadata instanceof VirtualPropertyMetadata | ||
|| $propertyMetadata instanceof StaticPropertyMetadata | ||
|| $propertyMetadata instanceof ExpressionPropertyMetadata; | ||
} | ||
} |
This file contains 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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Fixtures\TypedProperties; | ||
|
||
class Role | ||
{ | ||
public int $id; | ||
} |
This file contains 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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Fixtures\TypedProperties; | ||
|
||
use JMS\Serializer\Annotation as Serializer; | ||
|
||
class User | ||
{ | ||
public int $id; | ||
public Role $role; | ||
public \DateTime $created; | ||
|
||
/** | ||
* @Serializer\ReadOnly() | ||
*/ | ||
public ?\DateTimeInterface $updated = null; | ||
/** | ||
* @Serializer\ReadOnly() | ||
*/ | ||
public iterable $tags = []; | ||
} |
This file contains 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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Metadata\Driver; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use JMS\Serializer\Builder\DefaultDriverFactory; | ||
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
use JMS\Serializer\Tests\Fixtures\TypedProperties\User; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DefaultDriverFactoryTest extends TestCase | ||
{ | ||
public function testDefaultDriverFactoryLoadsTypedPropertiesDriver() | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped(sprintf('%s requires PHP 7.4', __METHOD__)); | ||
} | ||
|
||
$factory = new DefaultDriverFactory(new IdenticalPropertyNamingStrategy()); | ||
|
||
$driver = $factory->createDriver([], new AnnotationReader()); | ||
|
||
$m = $driver->loadMetadataForClass(new \ReflectionClass(User::class)); | ||
self::assertNotNull($m); | ||
|
||
$expectedPropertyTypes = [ | ||
'id' => 'int', | ||
'role' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Role', | ||
'created' => 'DateTime', | ||
'tags' => 'iterable', | ||
]; | ||
|
||
foreach ($expectedPropertyTypes as $property => $type) { | ||
self::assertEquals(['name' => $type, 'params' => []], $m->propertyMetadata[$property]->type); | ||
} | ||
} | ||
} |
This file contains 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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Metadata\Driver; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use JMS\Serializer\Metadata\Driver\AnnotationDriver; | ||
use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver; | ||
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
use JMS\Serializer\Tests\Fixtures\TypedProperties\User; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TypedPropertiesDriverTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped(sprintf('%s requires PHP 7.4', TypedPropertiesDriver::class)); | ||
} | ||
} | ||
|
||
public function testInferPropertiesFromTypes() | ||
{ | ||
$baseDriver = new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); | ||
$driver = new TypedPropertiesDriver($baseDriver); | ||
|
||
$m = $driver->loadMetadataForClass(new \ReflectionClass(User::class)); | ||
|
||
self::assertNotNull($m); | ||
|
||
$expectedPropertyTypes = [ | ||
'id' => 'int', | ||
'role' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Role', | ||
'created' => 'DateTime', | ||
'tags' => 'iterable', | ||
]; | ||
|
||
foreach ($expectedPropertyTypes as $property => $type) { | ||
self::assertEquals(['name' => $type, 'params' => []], $m->propertyMetadata[$property]->type); | ||
} | ||
} | ||
} |
This file contains 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 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 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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<result> | ||
<id>1</id> | ||
<role> | ||
<id>5</id> | ||
</role> | ||
<created><![CDATA[2010-10-01T00:00:00+00:00]]></created> | ||
<updated><![CDATA[2011-10-01T00:00:00+00:00]]></updated> | ||
<tags> | ||
<entry><![CDATA[a]]></entry> | ||
<entry><![CDATA[b]]></entry> | ||
</tags> | ||
</result> |