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 all commits
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
@@ -0,0 +1,51 @@
<?php
namespace Neos\Flow\Tests\Functional\Reflection\Fixtures;

/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

/**
* Dummy class for the Reflection tests
*
*/
class DummyClassWithUnionTypeAnnotations
{
/**
* @param string|false $parameterA
* @param false|DummyClassWithUnionTypeAnnotations $parameterB
* @param false | DummyClassWithUnionTypeAnnotations $parameterB1 Same as B but with spaces in between
* @param DummyClassWithUnionTypeAnnotations|null $parameterC
* @return void
*/
public function methodWithUnionParameters($parameterA, $parameterB, $parameterB1, $parameterC)
{
}

/**
* @return string|false
*/
public function methodWithUnionReturnTypeA()
{
}

/**
* @return false|DummyClassWithUnionTypeAnnotations
*/
public function methodWithUnionReturnTypesB()
{
}

/**
* @return DummyClassWithUnionTypeAnnotations|null
*/
public function methodWithUnionReturnTypesC()
{
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Neos\Flow\Tests\Functional\Reflection\Fixtures\PHP8;
namespace Neos\Flow\Tests\Functional\Reflection\Fixtures;

/*
* This file is part of the Neos.Flow package.
Expand All @@ -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
128 changes: 117 additions & 11 deletions Neos.Flow/Tests/Functional/Reflection/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
*/

use Neos\Flow\Reflection\ReflectionService;
use Neos\Flow\Tests\Functional\Persistence;
use Neos\Flow\Tests\Functional\Reflection;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubEntity;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubSubEntity;
use Neos\Flow\Tests\Functional\Reflection\Fixtures\Model\SubSubSubEntity;
use Neos\Flow\Tests\FunctionalTestCase;
use Neos\Flow\Tests\Functional\Reflection;
use Neos\Flow\Tests\Functional\Persistence;

/**
* Functional tests for the Reflection Service features
Expand Down Expand Up @@ -356,15 +356,121 @@ public function annotatedArrayTypeHintsWorkCorrectly()
*/
public function unionReturnTypesWorkCorrectly()
{
if (PHP_MAJOR_VERSION < 8) {
$this->markTestSkipped('Only for PHP 8 with UnionTypes');
}
$returnTypeA = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA');
$returnTypeB = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB');
$returnTypeC = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\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
);
}

/**
* @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
);
}

self::assertEquals('string|false', $returnTypeA);
self::assertEquals('\Neos\Flow\Tests\Functional\Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints|false', $returnTypeB);
self::assertEquals('?\Neos\Flow\Tests\Functional\Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints', $returnTypeC);
/**
* @test
*/
public function unionReturnTypeAnnotationsWorkCorrectly()
{
$returnTypes = [
'returnTypeA' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypeA'),
'returnTypeB' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypesB'),
'returnTypeC' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypesC'),
];

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

Choose a reason for hiding this comment

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

See earlier comment about leading backslash…

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

/**
* @test
*/
public function unionParameterTypeAnnotationsWorkCorrectly()
{
$methodWithUnionParameters = $this->reflectionService->getMethodParameters(Fixtures\DummyClassWithUnionTypeAnnotations::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\DummyClassWithUnionTypeAnnotations|false',
'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false',
'allowsNull' => false,
],
'parameterB1' => [
'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false',
'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false',
'allowsNull' => false,
],
'parameterC' => [
'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations',
'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations',
'allowsNull' => true,
],
],
$methodWithUnionParametersReduced
);
}
}
Loading