Phan\Language\UnionType::genericArrayElementTypes PHP Method

genericArrayElementTypes() public method

Takes "a|b[]|c|d[]|e" and returns "b|d"
public genericArrayElementTypes ( ) : UnionType
return UnionType The subset of types in this
    public function genericArrayElementTypes() : UnionType
    {
        $union_type = new UnionType($this->type_set->filter(function (Type $type) : bool {
            return $type->isGenericArray();
        })->map(function (Type $type) : Type {
            return $type->genericArrayElementType();
        }));
        // If array is in there, then it can be any type
        // Same for mixed
        if ($this->hasType(ArrayType::instance()) || $this->hasType(MixedType::instance())) {
            $union_type->addType(MixedType::instance());
        }
        if ($this->hasType(ArrayType::instance())) {
            $union_type->addType(NullType::instance());
        }
        return $union_type;
    }

Usage Example

Example #1
0
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitList(Node $node) : Context
 {
     // Figure out the type of elements in the list
     $element_type = $this->right_type->genericArrayElementTypes();
     foreach ($node->children ?? [] as $child_node) {
         // Some times folks like to pass a null to
         // a list to throw the element away. I'm not
         // here to judge.
         if (!$child_node instanceof Node) {
             continue;
         }
         $variable = Variable::fromNodeInContext($child_node, $this->context, $this->code_base, false);
         // Set the element type on each element of
         // the list
         $variable->setUnionType($element_type);
         // Note that we're not creating a new scope, just
         // adding variables to the existing scope
         $this->context->addScopeVariable($variable);
     }
     return $this->context;
 }
All Usage Examples Of Phan\Language\UnionType::genericArrayElementTypes