Пример #1
0
 public static function decideTypeFromReflection(\ReflectionType $reflectionType = null, Type $phpDocType = null, string $selfClass = null, bool $isVariadic = false) : Type
 {
     if ($reflectionType === null) {
         return $phpDocType !== null ? $phpDocType : new MixedType(true);
     }
     $reflectionTypeString = (string) $reflectionType;
     if ($isVariadic) {
         $reflectionTypeString .= '[]';
     }
     $type = self::getTypeObjectFromTypehint($reflectionTypeString, $reflectionType->allowsNull(), $selfClass);
     return self::decideType($type, $phpDocType);
 }
Пример #2
0
 public static function decideType(\ReflectionType $reflectionType = null, Type $phpDocType = null, string $selfClass = null, bool $isVariadic = false) : Type
 {
     if ($reflectionType === null) {
         return $phpDocType !== null ? $phpDocType : new MixedType(true);
     }
     $reflectionTypeString = (string) $reflectionType;
     if ($isVariadic) {
         $reflectionTypeString .= '[]';
     }
     $type = self::getTypeObjectFromTypehint($reflectionTypeString, $reflectionType->allowsNull(), $selfClass);
     if ($phpDocType !== null) {
         if ($type instanceof ArrayType && $phpDocType instanceof ArrayType) {
             $type = new ArrayType($phpDocType->getItemType(), $type->isNullable() || $phpDocType->isNullable());
         }
         if ($type->accepts($phpDocType)) {
             return $phpDocType;
         }
     }
     return $type;
 }
<?php

function foo(int $a) : bool
{
}
$rf = new ReflectionFunction('foo');
echo "--Parameter--\n\n";
$rp = $rf->getParameters()[0];
var_dump($rp->hasType());
$rt = $rp->getType();
var_dump($rt->isBuiltin());
var_dump($rt->__toString());
var_dump($rt->allowsNull());
echo "\n--Return--\n\n";
var_dump($rf->hasReturnType());
$rt = $rf->getReturnType();
var_dump($rt->isBuiltin());
var_dump($rt->__toString());
var_dump($rt->allowsNull());
echo "\n--Call Constructor Directly--\n\n";
// There is a public constructor; we differ with PHP 7 a bit here. In PHP 7
// you can call the public constructor from user code and then you fatal on
// the first method call to the instance. In HHVM, you can call the constructor
// directly, but you have to have an instance of ReflectionParameter or
// ReflectionFunctionAbstract.
$rt2 = new ReflectionType($rp);
// this returns false since we didn't pass any info to the constructor
var_dump($rt2->isBuiltin());
// This will trigger an error since it is not ReflectionParameter or
// ReflectionFunctionAbstract
$rt3 = new ReflectionType(new ReflectionClass('Exception'));
Пример #4
0
 public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
 {
     $prefix = Caster::PREFIX_VIRTUAL;
     $a += array($prefix . 'type' => $c->__toString(), $prefix . 'allowsNull' => $c->allowsNull(), $prefix . 'isBuiltin' => $c->isBuiltin());
     return $a;
 }
Пример #5
0
 /**
  * Extracts data from the PHP 7 reflection type.
  *
  * @param \ReflectionType $reflectionType
  *
  * @return Type
  */
 private function extractFromReflectionType(\ReflectionType $reflectionType)
 {
     $phpTypeOrClass = (string) $reflectionType;
     $nullable = $reflectionType->allowsNull();
     if ($reflectionType->isBuiltin()) {
         if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
             $type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
         } else {
             $type = new Type($phpTypeOrClass, $nullable);
         }
     } else {
         $type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass);
     }
     return $type;
 }
Пример #6
0
 public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
 {
     $prefix = Caster::PREFIX_VIRTUAL;
     $a += array($prefix . 'name' => method_exists('ReflectionType', 'getName') ? $c->getName() : $c->__toString(), $prefix . 'allowsNull' => $c->allowsNull(), $prefix . 'isBuiltin' => $c->isBuiltin());
     return $a;
 }
Пример #7
0
 public function isBuiltin()
 {
     return $this->hasType() && $this->type->isBuiltin();
 }
Пример #8
0
 public function allowsNull()
 {
     return $this->type->allowsNull();
 }
Пример #9
0
 /**
  * Extracts data from the PHP 7 reflection type.
  *
  * @param \ReflectionType $reflectionType
  *
  * @return Type
  */
 private function extractFromReflectionType(\ReflectionType $reflectionType)
 {
     $phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : $reflectionType->__toString();
     $nullable = $reflectionType->allowsNull();
     if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
         $type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
     } elseif ('void' === $phpTypeOrClass) {
         $type = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
     } elseif ($reflectionType->isBuiltin()) {
         $type = new Type($phpTypeOrClass, $nullable);
     } else {
         $type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass);
     }
     return $type;
 }
 public function testAllowsNull()
 {
     $this->assertTrue($this->t1->allowsNull());
     $this->assertFalse($this->t2->allowsNull());
 }