hediet/type-reflection

一个库,提供类以统一方式反映PHP类型。

v0.1.2 2015-03-23 23:44 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:00:52 UTC


README

此库提供类以统一方式反映PHP类型。

安装

您可以使用 Composer 下载并安装PHP Type Reflection。要将PHP Type Reflection添加到您的项目中,只需将hediet/type-reflection添加到项目中的composer.json文件中。

以下是一个仅定义对PHP Type Reflection依赖的最小composer.json文件示例

{
    "require": {
        "hediet/type-reflection": "^0.1.2"
    }
}

使用方法

类型类的静态方法

有几种静态方法可以获取类型实例

<?php

use Hediet\Types\Type;

// Primitive types
Type::ofInteger();
Type::ofFloat();
Type::ofString();
Type::ofBoolean();
Type::ofMixed();
Type::ofResource();
Type::ofObject();
Type::ofNull();

// Non-primitive types
Type::ofArray($itemType); //$itemType must be a Type
Type::ofObjectType($fullName); //can be either a class or interface
Type::ofClass($fullName);
Type::ofInterface($fullName);
Type::ofUnion($types); //$types must be a Type array
Type::ofNullable($type); //$type must be a Type. Returns $type|null.

Type::byValue($value);
Type::byReflectionClass($reflectionClass);
Type::of($typeName); //$typeName can be an arbitrary type name
Type::of($typeName, $resolver); //A resolver can be used to resolve relative class names.

?>

类型类的成员方法

类型实例有几种定义的方法

<?php

// Common methods
$type->getName(); //Gets an unique name of $type. Two equal types have the same name.
$type->isAssignableFrom($otherType); //Checks whether values of $otherType can be assigned to $type.
$type->isAssignableFromValue($value); //Checks whether $value can be assigned to $type.
$type->equals($otherType); //Checks whether $type is equal to $otherType.

// Methods for ObjectType
$type->getReflectionClass();
$type->getMethods(); //Gets a MethodInfo array.
$type->getMethod($name);
$type->isSubtypeOf($otherType);

// Methods for ClassType
$type->getProperties(); //Gets a PropertyInfo array.
$type->getProperty();
$type->isImplementorOf($interfaceType);
$type->getImplementedInterfaces();

// Methods for ArrayType
$type->getKeyType();
$type->getItemType();

// Methods for UnionType
$type->getUnitedTypes();

?>

MethodInfo, ParameterInfo, ResultInfo 和 PropertyInfo

所有这些信息类都通过解析PHP文档注释提供类型信息和描述。

示例

<?php

namespace Hediet\Types\Test;

use Hediet\Types\Type;
use Hediet\Types\ArrayType;

interface FooInterface
{
    
}

class Foo implements FooInterface
{
    
}

abstract class MyClass
{
    /**
     * The name.
     * @var MyClass|string
     */
    public $name;
    
    /**
     * Does some stuff.
     * @param Type|MyClass|ArrayType $myArg Some input.
     * @return string[] The return value.
     */
    public abstract function myMethod($myArg);
}

$p = Type::ofClass("Hediet\\Types\\Test\\MyClass")->getProperty("name");
$this->assertEquals("The name.", $p->getDescription());
$this->assertEquals("Hediet\\Types\\Test\\MyClass|string", $p->getType()->getName());


$m = Type::ofObjectType("Hediet\Types\Test\MyClass")->getMethod("myMethod");

$this->assertEquals("Does some stuff.", $m->getDescription());
$this->assertEquals("The return value.", $m->getResultInfo()->getDescription());

/* @var $resultType ArrayType */
$resultType = $m->getResultInfo()->getType();
$this->assertEquals("string[]", $resultType->getName());
$this->assertEquals("string", $resultType->getItemType()->getName());
$this->assertTrue($resultType->isAssignableFromValue(array("str1", "str2")));
$this->assertFalse($resultType->isAssignableFromValue(array(1, 2)));

$this->assertFalse($resultType->isAssignableFrom(Type::ofMixed()));
$this->assertTrue(Type::ofMixed()->isAssignableFrom($resultType));

$myArgParam = $m->getParameters()[0];
$this->assertEquals("myArg", $myArgParam->getName());
$this->assertEquals("Some input.", $myArgParam->getDescription());
$this->assertEquals("Hediet\Types\Test\MyClass|Hediet\Types\Type", $myArgParam->getType()->getName());


//Union Types
$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))));

?>

类图

class diagram

待办事项

  • 添加对 FunctionInfo 的支持。
    这需要手动解析使用语句,因为 StaticReflectionParser 不支持反射函数。
  • 添加对泛型类、方法和函数的支持(如 )。
  • 启用对未加载的类型进行反射。

作者

Henning Dieterichs - henning.dieterichs@hediet.de

许可证

PHP Expect 根据 MIT 许可证授权。