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

Support for union/intersection types? #197

Open
jpirnat opened this issue Mar 7, 2022 · 2 comments
Open

Support for union/intersection types? #197

jpirnat opened this issue Mar 7, 2022 · 2 comments

Comments

@jpirnat
Copy link

jpirnat commented Mar 7, 2022

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).

@thisispiers
Copy link
Contributor

See pull request #204 for support for scalar union types

@cseufert
Copy link

I have merged a patch for this into a fork I maintain here:
https://packagist.org/packages/moddengine/dice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants