Skip to content

Commit 69d72d0

Browse files
committed
[PropertyInfo] Support for intersection types
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 3b21091 commit 69d72d0

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
333333
$types = [];
334334
$nullable = $reflectionType->allowsNull();
335335

336-
foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
337-
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
336+
foreach (($reflectionType instanceof \ReflectionUnionType || $reflectionType instanceof \ReflectionIntersectionType) ? $reflectionType->getTypes() : [$reflectionType] as $type) {
337+
$phpTypeOrClass = $type->getName();
338338
if ('null' === $phpTypeOrClass || 'mixed' === $phpTypeOrClass) {
339339
continue;
340340
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function php71TypesProvider()
233233
}
234234

235235
/**
236-
* * @dataProvider php80TypesProvider
236+
* @dataProvider php80TypesProvider
237237
* @requires PHP 8
238238
*/
239239
public function testExtractPhp80Type($property, array $type = null)
@@ -255,6 +255,22 @@ public function php80TypesProvider()
255255
];
256256
}
257257

258+
/**
259+
* @dataProvider php81TypesProvider
260+
* @requires PHP 8.1
261+
*/
262+
public function testExtractPhp81Type($property, array $type = null)
263+
{
264+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy', $property, []));
265+
}
266+
267+
public function php81TypesProvider()
268+
{
269+
return [
270+
['collection', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Traversable'), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Countable')]],
271+
];
272+
}
273+
258274
/**
259275
* @dataProvider defaultValueProvider
260276
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
4+
5+
class Php81Dummy
6+
{
7+
public function getCollection(): \Traversable&\Countable
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)