Exemplo n.º 1
0
 public function testInvalidBooleanValuesShouldReturnFalse()
 {
     $validator = new BoolType();
     $this->assertFalse($validator->__invoke(''));
     $this->assertFalse($validator->__invoke('foo'));
     $this->assertFalse($validator->__invoke(123123));
     $this->assertFalse($validator->__invoke(new \stdClass()));
     $this->assertFalse($validator->__invoke(array()));
     $this->assertFalse($validator->__invoke(1));
     $this->assertFalse($validator->__invoke(0));
     $this->assertFalse($validator->__invoke(null));
 }
Exemplo n.º 2
0
 /**
  * @param Node $node
  * A node to check types on
  *
  * @return UnionType
  * The resulting type(s) of the binary operation
  */
 private function visitBinaryOpCommon(Node $node)
 {
     $left = UnionType::fromNode($this->context, $this->code_base, $node->children['left']);
     $right = UnionType::fromNode($this->context, $this->code_base, $node->children['right']);
     if (!$left->genericArrayElementTypes()->isEmpty() && $left->nonGenericArrayTypes()->isEmpty() && !$right->canCastToUnionType(ArrayType::instance()->asUnionType())) {
         Issue::maybeEmit($this->code_base, $this->context, Issue::TypeComparisonFromArray, $node->lineno ?? 0, (string) $right);
     } elseif (!$right->genericArrayElementTypes()->isEmpty() && $right->nonGenericArrayTypes()->isEmpty() && !$left->canCastToUnionType(ArrayType::instance()->asUnionType())) {
         // and the same for the left side
         Issue::maybeEmit($this->code_base, $this->context, Issue::TypeComparisonToArray, $node->lineno ?? 0, (string) $left);
     }
     return BoolType::instance()->asUnionType();
 }
Exemplo n.º 3
0
 /**
  * @param Node $node
  * A node to check types on
  *
  * @return UnionType
  * The resulting type(s) of the binary operation
  */
 private function visitBinaryOpCommon(Node $node)
 {
     $left = UnionType::fromNode($this->context, $this->code_base, $node->children['left']);
     $right = UnionType::fromNode($this->context, $this->code_base, $node->children['right']);
     if (!$left->genericArrayElementTypes()->isEmpty() && $left->nonGenericArrayTypes()->isEmpty() && !$right->canCastToUnionType(ArrayType::instance()->asUnionType())) {
         Log::err(Log::ETYPE, "array to {$right} comparison", $this->context->getFile(), $node->lineno);
     } else {
         if (!$right->genericArrayElementTypes()->isEmpty() && $right->nonGenericArrayTypes()->isEmpty() && !$left->canCastToUnionType(ArrayType::instance()->asUnionType())) {
             // and the same for the right side
             Log::err(Log::ETYPE, "{$left} to array comparison", $this->context->getFile(), $node->lineno);
         }
     }
     return BoolType::instance()->asUnionType();
 }
Exemplo n.º 4
0
 /**
  * Visit a node with kind `\ast\AST_UNARY_OP`
  *
  * @param Node $node
  * A node of the type indicated by the method name that we'd
  * like to figure out the type that it produces.
  *
  * @return UnionType
  * The set of types that are possibly produced by the
  * given node
  */
 public function visitUnaryOp(Node $node) : UnionType
 {
     // Shortcut some easy operators
     switch ($node->flags) {
         case \ast\flags\UNARY_BOOL_NOT:
             return BoolType::instance()->asUnionType();
     }
     return UnionType::fromNode($this->context, $this->code_base, $node->children['expr']);
 }
Exemplo n.º 5
0
 public function testFromStringWithBool()
 {
     $this->assertSame(BoolType::getInstance(), MixedType::fromString("bool"));
 }