2017 © Pedro Peláez
 

library worse-reflection

Lazy AST reflector that is much worse than better

image

phpactor/worse-reflection

Lazy AST reflector that is much worse than better

  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 3 Forks
  • 3 Open issues
  • 34 Versions
  • 23 % Grown

The README.md

Worse Reflection

This package is not actively developed here but in the main phpactor repository. It is occasionally updated. Raise any issues about this repository there., (*1)

This library aims to provide a light class-based AST based "reflection" library., (*2)

Worse than Better

It is influenced by BetterReflection, diffrerences are as follows:, (*3)

  • Can reflect variables.
  • Does not aim to implement built-in PHP reflection API.
  • Uses the amazing Tolerant Parser.
  • Uses the PHPStorm stubs to provide reflections of internal classes.

It is being developed to provide support for the Phpactor introspection and refactoring tool. And is therefore driven by that use case., (*4)

If you want comprehsnsive reflection, use BetterReflection. If you want faster reflection including type/value flow with no support and frequent BC breaks, then you can use this one (note that I havn't benchmarked BR in sometime, it may well be faster now)., (*5)

Usage

$reflector = ReflectorBuilder::create()
    ->addSource('<?php ...')
    ->build();

$class = $reflector->reflectClass('Foobar');

$class->methods()->get('foobar')->visiblity()    == Visibility::public();
$class->properties()->get('barbar')->visiblity() == Visibility::public();

/** @var ReflectionMethod */
foreach ($class->methods() as $method) {
    echo $method->name();                                  // methodName
    echo $method->returnType()->short();                   // Foobar
    echo (string) $method->returnType();                   // This\Is\Foobar
    echo (string) $method->inferredReturnTypes()->best(); // from docblock if it exists

    foreach ($method->parameters() as $parameter) {
        $parameter->name();                      // paramName
        (string) $parameter->inferredType();     // Fully\Qualified\ParamType
    }

}

foreach ($class->traits() as $trait) {
    // ...
}

foreach ($class->interfaes() as $interface) {
    // ...
}

foreach ($class->method('foobar')->frame()->locals() as $variable) {
    $variable->offset()->toInt(); // byte offset
    $variable->type();            // variable type (if available )
    $variable->value();           // variable value (if available)
}

$offset = $reflection->reflectOffset(
    SourceCode::fromString('...'), 
    Offset::fromInt(1234)
);

$offset->value()->type();    // type at offset (if available)
$offset->value()->value();   // value (e.g. 1234)
$offset->frame();            // return frame

See tests for more examples..., (*6)

Contributing

This package is open source and welcomes contributions! Feel free to open a pull request on this repository., (*7)

Support

  • Create an issue on the main Phpactor repository.
  • Join the #phpactor channel on the Slack Symfony Devs channel.

The Versions

31/10 2017
28/10 2017

dev-remove_implicit_factory_rubbish

dev-remove_implicit_factory_rubbish

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

23/10 2017

dev-accept_sourcecode_as_argument

dev-accept_sourcecode_as_argument

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

01/09 2017

dev-explicit_reflector_methods

dev-explicit_reflector_methods

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

08/08 2017
06/08 2017

dev-traits

dev-traits

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

04/08 2017

dev-node_resolve_unknown_classes

dev-node_resolve_unknown_classes

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

30/07 2017

dev-service_locator

dev-service_locator

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

07/07 2017

dev-stub_locator

dev-stub_locator

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

06/07 2017

dev-constants

dev-constants

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

05/07 2017

dev-class_interface_parents

dev-class_interface_parents

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires

26/02 2017

dev-hotfixes

dev-hotfixes

Lazy AST reflector that is much worse than better

  Sources   Download

MIT

The Requires

 

The Development Requires