2017 © Pedro Peláez
 

library type-reflection

A library which provide classes to reflect php types in an unified way.

image

hediet/type-reflection

A library which provide classes to reflect php types in an unified way.

  • Tuesday, March 24, 2015
  • by Henning Dieterichs
  • Repository
  • 0 Watchers
  • 1 Stars
  • 43 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Type Reflection

This library provides classes to reflect php types in an unified way., (*1)

Installation

You can use Composer to download and install PHP Type Reflection. To add PHP Type Reflection to your project, simply add a dependency on hediet/type-reflection to your project's composer.json file., (*2)

Here is a minimal example of a composer.json file that just defines a dependency on PHP Type Reflection:, (*3)

``` json { "require": { "hediet/type-reflection": "^0.1.2" } }, (*4)


Usage ----- ### Static Methods of the Type Class ### There are several static methods to get a type instance: ``` php

Methods of the Type Class

There are several methods defined for type instances: ``` php 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(); ?>, (*5)


### MethodInfo, ParameterInfo, ResultInfo and PropertyInfo ### Alls these info classes provide type information and a description by parsing the php doc comments. ### Examples ### ``` php 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

class diagram, (*6)

TODO

  • Add support for FunctionInfo.
    This requires to parse the use statements manually, as the StaticReflectionParser does not support reflecting functions.
  • Add support for generic classes, methods and functions (like this).
  • Enable reflecting types which are not loaded.

Author

Henning Dieterichs - henning.dieterichs@hediet.de, (*7)

License

PHP Expect is licensed under the MIT License., (*8)

The Versions

24/03 2015

dev-master

9999999-dev

A library which provide classes to reflect php types in an unified way.

  Sources   Download

MIT

The Requires

 

by Avatar Henning Dieterichs

reflection class metadata type primitive runtime

24/03 2015

v0.1.2

0.1.2.0

A library which provide classes to reflect php types in an unified way.

  Sources   Download

MIT

The Requires

 

by Avatar Henning Dieterichs

reflection class metadata type primitive runtime

14/03 2015

v0.1.1

0.1.1.0

A library which provide classes to reflect php types in an unified way.

  Sources   Download

MIT

The Requires

 

by Avatar Henning Dieterichs

reflection class metadata type primitive runtime

14/03 2015

v0.1.0

0.1.0.0

A library which provide classes to reflect php types in an unified way.

  Sources   Download

MIT

The Requires

 

by Avatar Henning Dieterichs

reflection class metadata type primitive runtime