2017 © Pedro PelĆ”ez
 

library trex-reflection

Reflection helpers for callables and types

image

raphhh/trex-reflection

Reflection helpers for callables and types

  • Tuesday, August 2, 2016
  • by raphhh
  • Repository
  • 3 Watchers
  • 14 Stars
  • 27,677 Installations
  • PHP
  • 7 Dependents
  • 1 Suggesters
  • 2 Forks
  • 0 Open issues
  • 9 Versions
  • 15 % Grown

The README.md

TRex Reflection

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage Dependency Status Total Downloads Reference Status License, (*1)

PHP tool to reflect callables an types., (*2)

Installation

Use composer command:, (*3)

$ composer require raphhh/trex-reflection

Documentation

CallableReflection

You can use CallableReflection to inspect and call a callable, like a callback or a Closure for example., (*4)

All kind of callable

You can know which kind of callable is given., (*5)

Closure
$reflect = new CallableReflection(function(){});
$reflect->isClosure(); //true
Function
$reflect = CallableReflection('in_array')
$reflect->isFunction(); //true
Static method
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
Instance method
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->isMethod(); //true
$reflect->isInstanceMethod(); //true
Invoked object
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->isInvokedObject(); //true

Retrieve contexts

You can retrieve the callable, like the object or the method name for example., (*6)

Closure
$reflect = new CallableReflection(function(){});
$reflect->getClosure(); //closure
Function
$reflect = new CallableReflection('in_array')
$reflect->getFunctionName(); //'in_array'
Static method
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
Instance method
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->getClassName(); //'DateTime'
$reflect->getObject(); //DateTime instance
$reflect->getMethodName(); //'modify'
Invoked object
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->getClassName(); //'Bar'
$reflect->getObject(); //Bar instance

Invoke callable

You can invoke every kind of callable in a same way., (*7)

With a list of args

This method calls the method and give to it all its args., (*8)

$reflect = new CallableReflection('in_array')
$reflect->invoke(1, [0, 1]); //true

With an array of args

This method allows to map each value of an array with every params of a function. Useful to use dynamic args with func_get_args()., (*9)

$reflect = new CallableReflection('in_array')
$reflect->invoke([1, [0, 1]]); //true

With a map of args

This method allows to map the keys of an array with the name of the params of a function. So, the order of the args has no importance., (*10)


$closure = function($arg1, $arg2){ return [$arg1, $arg2]; } $reflect = new CallableReflection($closure) $reflect->invokeA(['arg2' => 'arg2', 'arg1' => 'arg1'])); //['arg1', 'arg2']

Retrieve the associated reflection class

Retrieve the ReflectionFunctionAbstract of the callable., (*11)

For a function or a closure
$reflect = new CallableReflection('in_array');
$reflect->getReflector(); //ReflectionFunction
For a method
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getReflector(); //ReflectionMethod
For a class

Note that for a class, we get a ReflectionMethod for the __invoke method of the current object, and not a ReflectionClass., (*12)


class Bar{ function __invoke(){} } $reflect = new CallableReflection(new Bar()); $reflect->getReflector(); //ReflectionMethod

TypeReflection

Reflection on the type of a variable or function., (*13)

What is a type?

A type is a string defined by a specific value related to one of the PHP types., (*14)

$typeReflection = new TypeReflection('int');
$typeReflection->getType(); //"int"
$typeReflection->getStandardizedType(); //"integer"

If you work with a variable, you can use TypeReflection::createFromVariable($variable), (*15)

$foo = new stdClass();
$typeReflection = TypeReflection::createFromVariable($foo);
$typeReflection->getType(); //"stdClass"
$typeReflection->getStandardizedType(); //"object"

Note the TypeReflection is case insensitive., (*16)

Standardized types

TypeReflection standardizes types with following values:, (*17)

  • void
  • mixed
  • null
  • boolean
  • string
  • integer
  • float
  • number
  • scalar
  • array
  • object
  • resource
  • callable
  • unknown type

Valid any type

Recognition
$typeReflection = new TypeReflection('string');
$typeReflection->isValid(); //true
$typeReflection = new TypeReflection('foo');
$typeReflection->isValid(); //false
boolean
$typeReflection = new TypeReflection('bool');
$typeReflection->isBoolean(); //true
$typeReflection = new TypeReflection('boolean');
$typeReflection->isBoolean(); //true
string
$typeReflection = new TypeReflection('string');
$typeReflection->isString(); //true
integer
$typeReflection = new TypeReflection('int');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('integer');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('long');
$typeReflection->isInteger(); //true
float
$typeReflection = new TypeReflection('float');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('double');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('real');
$typeReflection->isFloat(); //true
number

Any integer or float value., (*18)

scalar

Any boolean, string or number value., (*19)

array
$typeReflection = new TypeReflection('array');
$typeReflection->isArray(); //true
$typeReflection = new TypeReflection('int[]');
$typeReflection->isArray(); //true
object
$typeReflection = new TypeReflection('object');
$typeReflection->isObject(); //true
$typeReflection = new TypeReflection('Datetime');
$typeReflection->isObject(); //true
resource
$typeReflection = new TypeReflection('resource');
$typeReflection->isResource(); //true
callable
$typeReflection = new TypeReflection('callable');
$typeReflection->isCallable(); //true
void
$typeReflection = new TypeReflection('void');
$typeReflection->isVoid(); //true
null
$typeReflection = new TypeReflection('null');
$typeReflection->isNull(); //true
mixed
$typeReflection = new TypeReflection('mixed');
$typeReflection->isMixed(); //true

Retrieve a standard notation

$typeReflection = new TypeReflection('bool');
$typeReflection->getStandardizedType(); //boolean
$typeReflection = new TypeReflection('int');
$typeReflection->getStandardizedType(); //integer
$typeReflection = new TypeReflection('real');
$typeReflection->getStandardizedType(); //float
$typeReflection = new TypeReflection('int[]');
$typeReflection->getStandardizedType(); //array
$typeReflection = new TypeReflection('Datetime');
$typeReflection->getStandardizedType(); //object

The Versions

02/08 2016

dev-master

9999999-dev https://github.com/Raphhh/trex-reflection

Reflection helpers for callables and types

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection callable type

02/08 2016

1.0.0

1.0.0.0 https://github.com/Raphhh/trex-reflection

Reflection helpers for callables and types

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection callable type

28/12 2015

0.2.3

0.2.3.0 https://github.com/Raphhh/trex-reflection

Reflection helpers for callables and types

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection callable type

28/12 2015

0.2.2

0.2.2.0 https://github.com/Raphhh/trex-reflection

Reflection helpers for callables and types

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection callable type

28/12 2015

0.2.1

0.2.1.0 https://github.com/Raphhh/trex-reflection

Reflection helpers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection

27/12 2015

0.2.0

0.2.0.0 https://github.com/Raphhh/trex-reflection

Reflection helpers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection

14/04 2015

0.1.1

0.1.1.0 https://github.com/Raphhh/trex-reflection

Reflection helpers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection

20/01 2015

0.1.0

0.1.0.0 https://github.com/Raphhh/trex-reflection

Reflection helpers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection

20/01 2015

0.0.1

0.0.1.0 https://github.com/Raphhh/trex-reflection

Reflection helpers

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Raphaƫl Lefebvre

reflection