public function juggle(Type $type) { /* * This is a predicate to determine whether the CURRENT type (the child class) * can be juggled into the Type $type. */ $predicate = function (Type $element) { return array_any($this->getAllowedJuggleTypes(), function (Type $value, $index, $collection) use($element) { return (bool) Type::intersect($element, $value); }); }; $x = array_filter($type->getTypes(), $predicate); if (!$x) { return null; } if (count($x) === 1) { return first($x); } return UnionType::createIfNotDuplicate(array_shift($x), ...$x); }
public function testJuggleScalar() { $x = new UnionType(new StringType(), new IntType()); $this->assertInstanceOf(FloatType::class, $x->juggle(new FloatType())); // e.g. not NULL }