Skip to content

Commit 7b5c6a3

Browse files
committed
Added Unit tests for php 8 union types.
1 parent 0700b7f commit 7b5c6a3

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

Extractor/ReflectionExtractor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,15 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
243243

244244
foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
245245
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
246+
if ('null' === $phpTypeOrClass) {
247+
continue;
248+
}
246249

247250
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
248251
$types[] = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
249-
} elseif ('void' === $phpTypeOrClass || 'null' === $phpTypeOrClass) {
252+
} elseif ('void' === $phpTypeOrClass) {
250253
$types[] = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
251-
} elseif ($reflectionType->isBuiltin()) {
254+
} elseif ($type->isBuiltin()) {
252255
$types[] = new Type($phpTypeOrClass, $nullable);
253256
} else {
254257
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));

Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ public function php71TypesProvider()
204204
];
205205
}
206206

207+
/**
208+
* @dataProvider php80TypesProvider
209+
* @requires PHP 8
210+
*/
211+
public function testExtractPhp80Type($property, array $type = null)
212+
{
213+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
214+
}
215+
216+
public function php80TypesProvider()
217+
{
218+
return [
219+
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]],
220+
['bar', [new Type(Type::BUILTIN_TYPE_INT, true)]],
221+
['timeout', [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)]],
222+
['optional', [new Type(Type::BUILTIN_TYPE_INT, true), new Type(Type::BUILTIN_TYPE_FLOAT, true)]],
223+
['string', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Stringable'), new Type(Type::BUILTIN_TYPE_STRING)]],
224+
];
225+
}
226+
207227
/**
208228
* @dataProvider getReadableProperties
209229
*/

Tests/Fixtures/Php80Dummy.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
4+
5+
class Php80Dummy
6+
{
7+
public function getFoo(): array|null
8+
{
9+
}
10+
11+
public function setBar(int|null $bar)
12+
{
13+
}
14+
15+
public function setTimeout(int|float $timeout)
16+
{
17+
}
18+
19+
public function getOptional(): int|float|null
20+
{
21+
}
22+
23+
public function setString(string|\Stringable $string)
24+
{
25+
}
26+
}

0 commit comments

Comments
 (0)