Skip to content

Commit a04e023

Browse files
Merge branch '3.4' into 4.4
* 3.4: Added Unit tests for php 8 union types.
2 parents 2ebe5a0 + 7b5c6a3 commit a04e023

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
@@ -326,12 +326,15 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
326326

327327
foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
328328
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
329+
if ('null' === $phpTypeOrClass) {
330+
continue;
331+
}
329332

330333
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
331334
$types[] = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
332-
} elseif ('void' === $phpTypeOrClass || 'null' === $phpTypeOrClass) {
335+
} elseif ('void' === $phpTypeOrClass) {
333336
$types[] = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
334-
} elseif ($reflectionType->isBuiltin()) {
337+
} elseif ($type->isBuiltin()) {
335338
$types[] = new Type($phpTypeOrClass, $nullable);
336339
} else {
337340
$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
@@ -219,6 +219,26 @@ public function php71TypesProvider()
219219
];
220220
}
221221

222+
/**
223+
* * @dataProvider php80TypesProvider
224+
* @requires PHP 8
225+
*/
226+
public function testExtractPhp80Type($property, array $type = null)
227+
{
228+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
229+
}
230+
231+
public function php80TypesProvider()
232+
{
233+
return [
234+
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]],
235+
['bar', [new Type(Type::BUILTIN_TYPE_INT, true)]],
236+
['timeout', [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)]],
237+
['optional', [new Type(Type::BUILTIN_TYPE_INT, true), new Type(Type::BUILTIN_TYPE_FLOAT, true)]],
238+
['string', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Stringable'), new Type(Type::BUILTIN_TYPE_STRING)]],
239+
];
240+
}
241+
222242
/**
223243
* @dataProvider defaultValueProvider
224244
*/

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)