PHP code example of hediet / type-reflection

1. Go to this page and download the library: Download hediet/type-reflection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hediet / type-reflection example snippets

 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();

 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))));