How to create UnionType with 3+ sub-types? #7028
Answered
by
TomasVotruba
brandonkelly
asked this question in
Q&A
|
There are a few places I need to add union type declarations via - public $plugin = null;
+ public PluginInterface|string|null $plugin = null;However use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;
$pluginType = new ObjectType('craft\base\PluginInterface');
$stringType = new StringType();
$nullType = new NullType();
// causes ShouldNotHappenException exception:
$unionType = new UnionType([$pluginType, $stringType, $nullType]);Is there a different way I could go about this with Rector? Or does this just need to be fixed in PHPStan first? |
Answered by
TomasVotruba
Feb 27, 2022
Replies: 1 comment 1 reply
|
At the moment it's limitation of PHPStan We go around it like this: |
1 reply
Answer selected by
samsonasik
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the moment it's limitation of PHPStan
UnionTypeobject.We go around it like this:
https://github.com/rectorphp/rector-symfony/blob/91fd3f3882171c6f0c7e60c44e689e8d7d8ad0a4/config/sets/symfony/symfony6/symfony-return-types.php#L56-L63