Exemplo n.º 1
0
 protected function verifyReturn($function, State $state)
 {
     if (!$function->stmts) {
         // interface
         return [];
     }
     $errors = [];
     if ($function->returnType) {
         $type = Type::fromDecl($function->returnType->value);
     } else {
         $type = Type::extractTypeFromComment("return", $function->getAttribute('doccomment'));
         if (Type::mixed()->equals($type)) {
             // only verify actual types
             return $errors;
         }
     }
     $returns = $this->findReturnBlocks($function->stmts);
     foreach ($returns as $return) {
         if (!$return || !$return->expr) {
             // Default return, no
             if ($type->allowsNull()) {
                 continue;
             }
             if (!$return) {
                 $errors[] = ["Default return found for non-null type {$type}", $function];
             } else {
                 $errors[] = ["Explicit null return found for non-null type {$type}", $return];
             }
         } elseif (!$return->expr->type) {
             var_dump($return->expr);
             $errors[] = ["Could not resolve type for return", $return];
         } else {
             if (!$state->resolver->resolves($return->expr->type, $type)) {
                 $errors[] = ["Type mismatch on return value, found {$return->expr->type} expecting {$type}", $return];
             }
         }
     }
     return $errors;
 }
Exemplo n.º 2
0
 /**
  * @return array
  */
 protected function verifyCall($func, $call, $state, $name)
 {
     $errors = [];
     foreach ($func->params as $idx => $param) {
         if (!isset($call->args[$idx])) {
             if (!$param->defaultVar) {
                 $errors[] = ["Missing required argument {$idx} for call {$name}()", $call];
             }
             continue;
         }
         if ($param->type) {
             $type = Type::fromDecl($param->type->value);
         } else {
             $type = Type::extractTypeFromComment("param", $param->function->getAttribute('doccomment'), $param->name->value);
             if (Type::mixed()->equals($type)) {
                 continue;
             }
         }
         if (!$state->resolver->resolves($call->args[$idx]->type, $type)) {
             $errors[] = ["Type mismatch on {$name}() argument {$idx}, found {$call->args[$idx]->type} expecting {$type}", $call];
         }
     }
     return $errors;
 }
Exemplo n.º 3
0
 protected function processAssertion(Assertion $assertion, Operand $source, SplObjectStorage $resolved)
 {
     if ($assertion instanceof Assertion\TypeAssertion) {
         $tmp = $this->processTypeAssertion($assertion, $source, $resolved);
         if ($tmp) {
             return $tmp;
         }
     } elseif ($assertion instanceof Assertion\NegatedAssertion) {
         $op = $this->processAssertion($assertion->value[0], $source, $resolved);
         if ($op instanceof Type) {
             // negated type assertion
             if (isset($resolved[$source])) {
                 return $resolved[$source]->removeType($op);
             }
             // Todo, figure out how to wait for resolving
             return Type::mixed()->removeType($op);
         }
     }
     return false;
 }