[PropertyInfo] Added support for extract type from default value
This commit is contained in:
parent
db6784bb09
commit
f6510cda40
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
4.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added the ability to extract property type based on its initial value
|
||||||
|
|
||||||
4.2.0
|
4.2.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -42,6 +42,12 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
|
|||||||
*/
|
*/
|
||||||
public static $defaultArrayMutatorPrefixes = ['add', 'remove'];
|
public static $defaultArrayMutatorPrefixes = ['add', 'remove'];
|
||||||
|
|
||||||
|
private const MAP_TYPES = [
|
||||||
|
'integer' => Type::BUILTIN_TYPE_INT,
|
||||||
|
'boolean' => Type::BUILTIN_TYPE_BOOL,
|
||||||
|
'double' => Type::BUILTIN_TYPE_FLOAT,
|
||||||
|
];
|
||||||
|
|
||||||
private $mutatorPrefixes;
|
private $mutatorPrefixes;
|
||||||
private $accessorPrefixes;
|
private $accessorPrefixes;
|
||||||
private $arrayMutatorPrefixes;
|
private $arrayMutatorPrefixes;
|
||||||
@ -117,6 +123,10 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
|
|||||||
) {
|
) {
|
||||||
return $fromConstructor;
|
return $fromConstructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($fromDefaultValue = $this->extractFromDefaultValue($class, $property)) {
|
||||||
|
return $fromDefaultValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -258,6 +268,25 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function extractFromDefaultValue(string $class, string $property)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$reflectionClass = new \ReflectionClass($class);
|
||||||
|
} catch (\ReflectionException $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$defaultValue = $reflectionClass->getDefaultProperties()[$property] ?? null;
|
||||||
|
|
||||||
|
if (null === $defaultValue) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$type = \gettype($defaultValue);
|
||||||
|
|
||||||
|
return [new Type(static::MAP_TYPES[$type] ?? $type)];
|
||||||
|
}
|
||||||
|
|
||||||
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod): Type
|
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod): Type
|
||||||
{
|
{
|
||||||
$phpTypeOrClass = $reflectionType->getName();
|
$phpTypeOrClass = $reflectionType->getName();
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\PropertyInfo\Tests\Extractor;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
||||||
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
|
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
|
||||||
|
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
|
||||||
use Symfony\Component\PropertyInfo\Tests\Fixtures\NotInstantiable;
|
use Symfony\Component\PropertyInfo\Tests\Fixtures\NotInstantiable;
|
||||||
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
|
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
|
||||||
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
|
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
|
||||||
@ -208,6 +209,25 @@ class ReflectionExtractorTest extends TestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider defaultValueProvider
|
||||||
|
*/
|
||||||
|
public function testExtractWithDefaultValue($property, $type)
|
||||||
|
{
|
||||||
|
$this->assertEquals($type, $this->extractor->getTypes(DefaultValue::class, $property, []));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function defaultValueProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['defaultInt', [new Type(Type::BUILTIN_TYPE_INT, false)]],
|
||||||
|
['defaultFloat', [new Type(Type::BUILTIN_TYPE_FLOAT, false)]],
|
||||||
|
['defaultString', [new Type(Type::BUILTIN_TYPE_STRING, false)]],
|
||||||
|
['defaultArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false)]],
|
||||||
|
['defaultNull', null],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getReadableProperties
|
* @dataProvider getReadableProperties
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 Tales Santos <tales.augusto.santos@gmail.com>
|
||||||
|
*/
|
||||||
|
class DefaultValue
|
||||||
|
{
|
||||||
|
public $defaultInt = 30;
|
||||||
|
public $defaultFloat = 30.5;
|
||||||
|
public $defaultString = 'foo';
|
||||||
|
public $defaultArray = [];
|
||||||
|
public $defaultNull = null;
|
||||||
|
}
|
Reference in New Issue
Block a user