Make ReflectionExtractor compatible with ReflectionType changes in PHP 7.1

This commit is contained in:
Teoh Han Hui 2016-09-05 15:19:46 +08:00
parent 356a0ac7e4
commit 4a041e9a75
No known key found for this signature in database
GPG Key ID: 729783C96A30C9B8
3 changed files with 50 additions and 1 deletions

View File

@ -231,7 +231,7 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
private function extractFromReflectionType(\ReflectionType $reflectionType)
{
$phpTypeOrClass = (string) $reflectionType;
$phpTypeOrClass = method_exists($reflectionType, 'getName') ? $reflectionType->getName() : (string) $reflectionType;
$nullable = $reflectionType->allowsNull();
if ($reflectionType->isBuiltin()) {

View File

@ -94,6 +94,25 @@ class ReflectionExtractorTest extends \PHPUnit_Framework_TestCase
);
}
/**
* @dataProvider php71TypesProvider
* @requires PHP 7.1
*/
public function testExtractPhp71Type($property, array $type = null)
{
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy', $property, array()));
}
public function php71TypesProvider()
{
return array(
array('foo', array(new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true))),
array('bar', array(new Type(Type::BUILTIN_TYPE_INT, true))),
array('baz', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
array('donotexist', null),
);
}
public function testIsReadable()
{
$this->assertFalse($this->extractor->isReadable('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', 'bar', array()));

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
/**
* @author Teoh Han Hui <teohhanhui@gmail.com>
*/
class Php71Dummy
{
public function getFoo(): ?array
{
}
public function setBar(?int $bar)
{
}
public function addBaz(string $baz)
{
}
}