Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Added support for extract type from default value #27570

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 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* Added the ability to extract property type based on its initial value

4.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
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 $accessorPrefixes;
private $arrayMutatorPrefixes;
Expand Down Expand Up @@ -117,6 +123,10 @@ public function getTypes($class, $property, array $context = [])
) {
return $fromConstructor;
}

if ($fromDefaultValue = $this->extractFromDefaultValue($class, $property)) {
return $fromDefaultValue;
}
}

/**
Expand Down Expand Up @@ -258,6 +268,25 @@ private function extractFromConstructor(string $class, string $property): ?array
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
{
$phpTypeOrClass = $reflectionType->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
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\Php71Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
Expand Down Expand Up @@ -208,6 +209,25 @@ public function php71TypesProvider()
];
}

/**
* @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
*/
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/DefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

/*
* 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\Tests\Fixtures;

/**
* @author Tales Santos <[email protected]>
*/
class DefaultValue
{
public $defaultInt = 30;
public $defaultFloat = 30.5;
public $defaultString = 'foo';
public $defaultArray = [];
public $defaultNull = null;
}