Skip to content

Commit e1672aa

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

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

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

Lines changed: 36 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,34 @@ 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+
$constructor = (new \ReflectionClass($class))->getConstructor();
201+
} catch (\ReflectionException $e) {
202+
return null;
203+
}
204+
205+
if (!$constructor) {
206+
return null;
207+
}
208+
209+
foreach ($constructor->getParameters() as $parameter) {
210+
if ($property !== $parameter->name) {
211+
continue;
212+
}
213+
214+
return array($this->extractFromReflectionType($parameter->getType()));
215+
}
216+
217+
return null;
218+
}
219+
188220
private function extractFromReflectionType(\ReflectionType $reflectionType): Type
189221
{
190222
$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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy