Skip to content

Support for union/intersection types?Β #197

@jpirnat

Description

@jpirnat

Dice fails to instantiate classes which use union or intersection types. A quick demo:

<?php
declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

$dice = new \Dice\Dice();

$rule = [
	'constructParams' => [
		'one',
		'two',
		'three',
		'four',
		'five',
		'six',
		'seven',
		'eight',
		'nine',
		'ten',
	]
];
$dice = $dice->addRule(Foo::class, $rule);

class Foo
{
	public function __construct(
		string $one,
		string|int $two,
		string|int $three,
		string|int $four,
		string $five,
		string $six,
		string $seven,
		string $eight,
		string $nine,
		string $ten
	) {
		print_r([$one, $two, $three, $four, $five, $six, $seven, $eight, $nine, $ten]);
	}
}

$foo = $dice->create(Foo::class);

Output:

Fatal error: Uncaught Error: Call to undefined method ReflectionUnionType::getName() in dicetest/vendor/level-2/dice/Dice.php:257
Stack trace:
#0 dicetest/vendor/level-2/dice/Dice.php(119): Dice\Dice->Dice\{closure}(Array, Array)
#1 dicetest/vendor/level-2/dice/Dice.php(96): Dice\Dice->Dice\{closure}(Array, Array)
#2 dicetest/index.php(42): Dice\Dice->create('Foo')
#3 {main}
  thrown in dicetest/vendor/level-2/dice/Dice.php on line 257

I wouldn't have thought this would be worth asking for, but some popular libraries are apparently already using union types out in the wild (e.g. Symfony HttpFoundation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions