8000 [PropertyInfo] Support for intersection types · symfony/symfony@9070047 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9070047

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

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

psalm.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@
1717
<directory name="vendor" />
1818
</ignoreFiles>
1919
</projectFiles>
20+
21+
<issueHandlers>
22+
<UndefinedClass>
23+
<errorLevel type="suppress">
24+
<!-- Class has been added in PHP 8.1 -->
25+
<referencedClass name="ReflectionIntersectionType"/>
26+
</errorLevel>
27+
</UndefinedClass>
28+
</issueHandlers>
2029
</psalm>

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 || 'never' === $phpTypeOrClass) {
339339
continue;
340340
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,20 @@ public function php80TypesProvider()
256256
}
257257

258258
/**
259+
* @dataProvider php81TypesProvider
259260
* @requires PHP 8.1
260261
*/
261-
public function testExtractPhp81Type()
262+
public function testExtractPhp81Type($property, array $type = null)
262263
{
263-
$this->assertNull($this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy', 'nothing', []));
264+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy', $property, []));
265+
}
266+
267+
public function php81TypesProvider()
268+
{
269+
return [
270+
['nothing', null],
271+
['collection', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Traversable'), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Countable')]],
272+
];
264273
}
265274

266275
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ public function getNothing(): never
88
{
99
throw new \Exception('Oops');
1010
}
11+
12+
public function getCollection(): \Traversable&\Countable
13+
{
14+
}
1115
}

0 commit comments

Comments
 (0)
0