public function juggle(Type $type) { if (array_filter($this->types, function (Type $element) use($type) { return array_filter($type->getTypes(), function (Type $element) use($type) { return $element->juggle($type); }); })) { return $type; } return null; }
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); }