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

Skip to content

Commit 3bf2d14

Browse files
bug #42098 [PropertyInfo] Support for intersection types (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] Support for intersection types | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #41552 | License | MIT | Doc PR | N/A I was a bit unsure how to fix this one. PropertyInfo does not know about the concept of intersection types yet. With this PR, the component will behave exactly the same for an intersection type as for a union: You will get an array of all atomic types. Commits ------- 9070047 [PropertyInfo] Support for intersection types
2 parents ba46a07 + 9070047 commit 3bf2d14

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)