private function initialize() { $this->initialized = true; $parseResult = DocBlockParser::parsePropertyDocBlock($this->reflector); $this->description = $parseResult->description; $resolver = new Helper\ParserClassNameResolver($this->getDeclaringType()->getName()); $this->type = Type::of($parseResult->type, $resolver); }
/** * Checks whether the provided type is assignable to this type. * Returns true, if the provided type is equal to this instance, or * if this instance is "mixed", or if this is "object" and type is an object type * or if this is "callable" and type is a class with a method __invoke. * * @param Type $type * @return boolean */ public function isAssignableFrom(Type $type) { if ($type->getName() === $this->name) { return true; } if ($this->name === self::MIXED_NAME) { return true; } if ($this->name === self::OBJECT_NAME) { return $type instanceof ObjectType; } if ($this->name === self::CALLABLE_NAME) { if ($type instanceof ObjectType) { return $type->getReflectionClass()->hasMethod("__invoke"); } } return false; }
public function testUnionType() { $fooInterface = Type::of("Hediet\\Types\\Test\\FooInterface"); $foo = Type::of("Hediet\\Types\\Test\\Foo"); $fooOrFooInterface = Type::ofUnion(array($fooInterface, $foo)); $this->assertTrue($fooOrFooInterface->equals($fooInterface)); $this->assertTrue(Type::ofUnion(array($fooInterface, Type::ofBoolean()))->isAssignableFrom($foo)); $this->assertTrue(Type::ofUnion(array($fooInterface, Type::ofBoolean()))->isAssignableFrom(Type::ofBoolean())); $this->assertFalse(Type::ofUnion(array($foo, Type::ofBoolean()))->isAssignableFrom($fooInterface)); $this->assertFalse(Type::ofUnion(array($foo, Type::ofBoolean()))->isAssignableFrom(Type::ofUnion(array($foo, Type::ofBoolean(), Type::ofNull())))); $this->assertTrue(Type::ofUnion(array($foo, Type::ofBoolean(), Type::ofNull()))->isAssignableFrom(Type::ofUnion(array(Type::ofBoolean(), $foo)))); }
/** * @param ReflectionParameter $parameter * @return Type */ function __internal_getParameterType(ReflectionParameter $parameter) { if (!isset($this->parameterTypeCache[$parameter->name])) { if (!$this->initialized) { $this->initialize(); } if (isset($this->parseResult->parameter[$parameter->getName()])) { $typeStr = $this->parseResult->parameter[$parameter->getName()]->type; $type = Type::of($typeStr, $this->resolver); } else { $typeHintedClass = $parameter->getClass(); if ($typeHintedClass !== null) { $type = Type::byReflectionClass($typeHintedClass); } else { $type = Type::ofMixed(); } if ($parameter->allowsNull()) { $type = Type::ofNullable($type); } } $this->parameterTypeCache[$parameter->name] = $type; } return $this->parameterTypeCache[$parameter->name]; }
/** * @param ReflectionMethod $method * @return MethodInfo */ public static function create(ReflectionMethod $method) { $declaringType = Type::byReflectionClass($method->getDeclaringClass()); return new MethodInfo($declaringType, $method); }
/** * Checks whether the type is equal to $other. * * @param Type $other The other type. * @return boolean */ public function equals(Type $other) { $result = $other->isAssignableFrom($this) && $this->isAssignableFrom($other); $result2 = $this->getName() === $other->getName(); //Are there any types which do not have the same name, but are assignable to each other? //(United types are sorted by their names) if ($result !== $result2) { throw new \Exception("This should not happen - please report a bug!"); } return $result; }