Phan\AST\UnionTypeVisitor::visitConditional PHP Method

visitConditional() public method

Visit a node with kind \ast\AST_CONDITIONAL
public visitConditional ( ast\Node $node ) : UnionType
$node ast\Node A node of the type indicated by the method name that we'd like to figure out the type that it produces.
return Phan\Language\UnionType The set of types that are possibly produced by the given node
    public function visitConditional(Node $node) : UnionType
    {
        $true_type = UnionType::fromNode($this->context, $this->code_base, $node->children['trueExpr'] ?? $node->children['true'] ?? '');
        $false_type = UnionType::fromNode($this->context, $this->code_base, $node->children['falseExpr'] ?? $node->children['false'] ?? '');
        $union_type = new UnionType();
        // Add the type for the 'true' side
        $union_type->addUnionType($true_type);
        // Add the type for the 'false' side
        $union_type->addUnionType($false_type);
        // If one side has an unknown type but the other doesn't
        // we can't let the unseen type get erased. Unfortunately,
        // we need to add 'mixed' in so that we know it could be
        // anything at all.
        //
        // See Issue #104
        if ($true_type->isEmpty() xor $false_type->isEmpty()) {
            $union_type->addUnionType(MixedType::instance()->asUnionType());
        }
        return $union_type;
    }