Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Union type handling #3439

Draft
wants to merge 3 commits into
base: 8.3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
class DummyClassWithUnionTypeHints
{
public function methodWithUnionParameters(
string|false $parameterA,
false|DummyClassWithUnionTypeHints $parameterB,
null|DummyClassWithUnionTypeHints $parameterC
): void {
}

public function methodWithUnionReturnTypeA(): string|false
{
}
Expand Down
57 changes: 51 additions & 6 deletions Neos.Flow/Tests/Functional/Reflection/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,57 @@ public function annotatedArrayTypeHintsWorkCorrectly()
*/
public function unionReturnTypesWorkCorrectly()
{
$returnTypeA = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA');
$returnTypeB = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB');
$returnTypeC = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesC');
$returnTypes = [
'returnTypeA' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA'),
'returnTypeB' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB'),
'returnTypeC' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesC'),
];

self::assertEquals(
[
'returnTypeA' => 'string|false',
'returnTypeB' => '\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHP FQCN in strings never have a leading backslash – should we handle that the same way? At least consistency would be great, seeing the next test expects those without a leading backslash…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests just took what the type hints created for granted. May aswell be buggy.

'returnTypeC' => '?\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints',
],
$returnTypes
);
}

self::assertEquals('string|false', $returnTypeA);
self::assertEquals('\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false', $returnTypeB);
self::assertEquals('?\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints', $returnTypeC);
/**
* @test
*/
public function unionParameterTypesWorkCorrectly()
{
$methodWithUnionParameters = $this->reflectionService->getMethodParameters(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionParameters');

$methodWithUnionParametersReduced = array_map(
fn (array $item) => [
'type' => $item['type'],
'class' => $item['class'],
'allowsNull' => $item['allowsNull'],
],
$methodWithUnionParameters
);

self::assertEquals(
[
'parameterA' => [
'type' => 'string|false',
'class' => 'string|false',
'allowsNull' => false,
],
'parameterB' => [
'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false',
'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false',
'allowsNull' => false,
],
'parameterC' => [
'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints',
'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints',
'allowsNull' => true,
],
],
$methodWithUnionParametersReduced
);
}
}
Loading