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

Skip to content

Commit 4bc0bdb

Browse files
committed
[PropertyInfo] Added support for extracting type from constructor
1 parent 07766b3 commit 4bc0bdb

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,16 @@ public function getProperties($class, array $context = array())
100100
*/
101101
public function getTypes($class, $property, array $context = array())
102102
{
103-
if ($fromMutator = $this->extractFromMutator($class, $property)) {
104-
return $fromMutator;
103+
if ($type = $this->extractFromMutator($class, $property)) {
104+
return $type;
105105
}
106106

107-
if ($fromAccessor = $this->extractFromAccessor($class, $property)) {
108-
return $fromAccessor;
107+
if ($type = $this->extractFromAccessor($class, $property)) {
108+
return $type;
109+
}
110+
111+
if ($type = $this->extractFromConstructor($class, $property)) {
112+
return $type;
109113
}
110114
}
111115

@@ -185,6 +189,35 @@ private function extractFromAccessor(string $class, string $property): ?array
185189
return null;
186190
}
187191

192+
/**
193+
* Tries to extract type information from constructor.
194+
*
195+
* @return Type[]|null
196+
*/
197+
private function extractFromConstructor(string $class, string $property): ?array
198+
{
199+
try {
200+
$class = new \ReflectionClass($class);
201+
$constructor = $class->getConstructor();
202+
} catch (\ReflectionException $e) {
203+
return null;
204+
}
205+
206+
if (!$constructor) {
207+
return null;
208+
}
209+
210+
foreach ($constructor->getParameters() as $name => $parameter) {
211+
if ($property !== $parameter->name) {
212+
continue;
213+
}
214+
215+
return array($this->extractFromReflectionType($parameter->getType()));
216+
}
217+
218+
return null;
219+
}
220+
188221
private function extractFromReflectionType(\ReflectionType $reflectionType): Type
189222
{
190223
$phpTypeOrClass = $reflectionType->getName();

src/Symfony/Component/PropertyInfo/Tests/Extractors/ReflectionExtractorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ public function testGetPropertiesWithNoPrefixes()
113113
);
114114
}
115115

116+
public function testGetPropertiesPhp71()
117+
{
118+
$noPrefixExtractor = new ReflectionExtractor();
119+
120+
$this->assertSame(
121+
array(
122+
'string',
123+
'stringOrNull',
124+
'foo',
125+
'buz',
126+
'bar',
127+
'baz',
128+
'intWithAccessor',
129+
),
130+
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy')
131+
);
132+
}
133+
116134
/**
117135
* @dataProvider typesProvider
118136
*/
@@ -170,6 +188,10 @@ public function php71TypesProvider()
170188
array('bar', array(new Type(Type::BUILTIN_TYPE_INT, true))),
171189
array('baz', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
172190
array('donotexist', null),
191+
array('string', array(new Type(Type::BUILTIN_TYPE_STRING, false))),
192+
array('stringOrNull', array(new Type(Type::BUILTIN_TYPE_STRING, true))),
193+
array('intPrivate', array(new Type(Type::BUILTIN_TYPE_INT, false))),
194+
array('intWithAccessor', array(new Type(Type::BUILTIN_TYPE_INT, false))),
173195
);
174196
}
175197

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php71Dummy.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
*/
1717
class Php71Dummy
1818
{
19+
public $string;
20+
21+
public $stringOrNull;
22+
23+
private $intPrivate;
24+
25+
private $intWithAccessor;
26+
27+
public function __construct(string $string, ?string $stringOrNull, int $intPrivate, int $intWithAccessor)
28+
{
29+
$this->string = $string;
30+
$this->stringOrNull = $stringOrNull;
31+
$this->intPrivate = $intPrivate;
32+
$this->intWithAccessor = $intWithAccessor;
33+
}
34+
1935
public function getFoo(): ?array
2036
{
2137
}
@@ -31,4 +47,9 @@ public function setBar(?int $bar)
3147
public function addBaz(string $baz)
3248
{
3349
}
50+
51+
public function getIntWithAccessor()
52+
{
53+
return $this->intWithAccessor;
54+
}
3455
}

0 commit comments

Comments
 (0)