Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f6510cd

Browse filesBrowse files
tsantos84fabpot
authored andcommitted
[PropertyInfo] Added support for extract type from default value
1 parent db6784b commit f6510cd
Copy full SHA for f6510cd

File tree

4 files changed

+80
-0
lines changed
Filter options

4 files changed

+80
-0
lines changed

‎src/Symfony/Component/PropertyInfo/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Added the ability to extract property type based on its initial value
8+
49
4.2.0
510
-----
611

‎src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
4242
*/
4343
public static $defaultArrayMutatorPrefixes = ['add', 'remove'];
4444

45+
private const MAP_TYPES = [
46+
'integer' => Type::BUILTIN_TYPE_INT,
47+
'boolean' => Type::BUILTIN_TYPE_BOOL,
48+
'double' => Type::BUILTIN_TYPE_FLOAT,
49+
];
50+
4551
private $mutatorPrefixes;
4652
private $accessorPrefixes;
4753
private $arrayMutatorPrefixes;
@@ -117,6 +123,10 @@ public function getTypes($class, $property, array $context = [])
117123
) {
118124
return $fromConstructor;
119125
}
126+
127+
if ($fromDefaultValue = $this->extractFromDefaultValue($class, $property)) {
128+
return $fromDefaultValue;
129+
}
120130
}
121131

122132
/**
@@ -258,6 +268,25 @@ private function extractFromConstructor(string $class, string $property): ?array
258268
return null;
259269
}
260270

271+
private function extractFromDefaultValue(string $class, string $property)
272+
{
273+
try {
274+
$reflectionClass = new \ReflectionClass($class);
275+
} catch (\ReflectionException $e) {
276+
return null;
277+
}
278+
279+
$defaultValue = $reflectionClass->getDefaultProperties()[$property] ?? null;
280+
281+
if (null === $defaultValue) {
282+
return null;
283+
}
284+
285+
$type = \gettype($defaultValue);
286+
287+
return [new Type(static::MAP_TYPES[$type] ?? $type)];
288+
}
289+
261290
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod): Type
262291
{
263292
$phpTypeOrClass = $reflectionType->getName();

‎src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1616
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
17+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
1718
use Symfony\Component\PropertyInfo\Tests\Fixtures\NotInstantiable;
1819
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
1920
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
@@ -208,6 +209,25 @@ public function php71TypesProvider()
208209
];
209210
}
210211

212+
/**
213+
* @dataProvider defaultValueProvider
214+
*/
215+
public function testExtractWithDefaultValue($property, $type)
216+
{
217+
$this->assertEquals($type, $this->extractor->getTypes(DefaultValue::class, $property, []));
218+
}
219+
220+
public function defaultValueProvider()
221+
{
222+
return [
223+
['defaultInt', [new Type(Type::BUILTIN_TYPE_INT, false)]],
224+
['defaultFloat', [new Type(Type::BUILTIN_TYPE_FLOAT, false)]],
225+
['defaultString', [new Type(Type::BUILTIN_TYPE_STRING, false)]],
226+
['defaultArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false)]],
227+
['defaultNull', null],
228+
];
229+
}
230+
211231
/**
212232
* @dataProvider getReadableProperties
213233
*/
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <fabien@symfony.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
15+
16+
/**
17+
* @author Tales Santos <tales.augusto.santos@gmail.com>
18+
*/
19+
class DefaultValue
20+
{
21+
public $defaultInt = 30;
22+
public $defaultFloat = 30.5;
23+
public $defaultString = 'foo';
24+
public $defaultArray = [];
25+
public $defaultNull = null;
26+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.