All Projects → goaop → parser-reflection

goaop / parser-reflection

Licence: MIT license
Parser Reflection API - Provides source code analysis without loading classes into the PHP memory

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to parser-reflection

React Monocle
A developer tool to visualize a React application's component hierarchy.
Stars: ✭ 2,440 (+2415.46%)
Mutual labels:  ast
Gengen
A Go source transformation tool for generics
Stars: ✭ 253 (+160.82%)
Mutual labels:  ast
java-ast
Java Parser for JavaScript/TypeScript (based on antlr4ts)
Stars: ✭ 58 (-40.21%)
Mutual labels:  ast
Php Parser
A PHP parser written in PHP
Stars: ✭ 15,101 (+15468.04%)
Mutual labels:  ast
Gulp Strip Debug
Strip console, alert, and debugger statements from JavaScript code
Stars: ✭ 242 (+149.48%)
Mutual labels:  ast
stutter
Implement a Lisp, in C, from scratch, no libs
Stars: ✭ 65 (-32.99%)
Mutual labels:  ast
Escaya
An blazing fast 100% spec compliant, incremental javascript parser written in Typescript
Stars: ✭ 217 (+123.71%)
Mutual labels:  ast
gox
JSX for Go
Stars: ✭ 165 (+70.1%)
Mutual labels:  ast
Esdoc
ESDoc - Good Documentation for JavaScript
Stars: ✭ 2,706 (+2689.69%)
Mutual labels:  ast
sast
Parse CSS, Sass, SCSS, and Less into a unist syntax tree
Stars: ✭ 51 (-47.42%)
Mutual labels:  ast
Cppast.net
CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
Stars: ✭ 228 (+135.05%)
Mutual labels:  ast
Tsutils
utility functions for working with typescript's AST
Stars: ✭ 240 (+147.42%)
Mutual labels:  ast
html5parser
A super tiny and fast html5 AST parser.
Stars: ✭ 153 (+57.73%)
Mutual labels:  ast
Ast Query
Tentative to a simple JavaScript AST modification library
Stars: ✭ 221 (+127.84%)
Mutual labels:  ast
awesome-ruby-ast
A list of awesome tools and libraries which deals with ASTs in Ruby
Stars: ✭ 24 (-75.26%)
Mutual labels:  ast
Vermin
Concurrently detect the minimum Python versions needed to run code
Stars: ✭ 218 (+124.74%)
Mutual labels:  ast
php2python
Convert PHP code to Python under CGI (beta)
Stars: ✭ 44 (-54.64%)
Mutual labels:  ast
rehype-dom
HTML processor to parse and compile with browser APIs, powered by plugins
Stars: ✭ 20 (-79.38%)
Mutual labels:  ast
venusscript
A dynamic, interpreted, scripting language written in Java.
Stars: ✭ 17 (-82.47%)
Mutual labels:  ast
astexplorer-go
No description or website provided.
Stars: ✭ 17 (-82.47%)
Mutual labels:  ast

Parser Reflection API Library

This library is deprecated. Please use BetterReflection.

Parser Reflection API library provides a set of classes that extend original internal Reflection classes, but powered by PHP-Parser library thus allowing to create a reflection instance without loading classes into the memory.

This library can be used for analysing the source code; for automatic proxy creation and much more.

Build Status Code Coverage Total Downloads Daily Downloads Scrutinizer Code Quality PHP Version License

Installation

Library can be installed with Composer. Installation is quite easy:

$ composer require goaop/parser-reflection

Composer will install the library to your project's vendor/goaop/parser-reflection directory.

Usage

Initialization

Prior to the first use library can be optionally initialized. If you use Composer for installing packages and loading classes, then you shouldn't worry about initialization, library will be initialized automatically.

If project uses a custom autoloader then you should follow the next steps:

  1. Create a new class that implements \Go\ParserReflection\LocatorInterface
  2. Create an instance of that class and pass it to the ReflectionEngine::init() method for initial configuration

Reflecting concrete classes/methods/properties without loading them

Just use Go\ParserReflection package reflection classes like traditional ones:

$parsedClass = new \Go\ParserReflection\ReflectionClass(SomeClass::class);
var_dump($parsedClass->getMethods());

$parsedMethod = new \Go\ParserReflection\ReflectionMethod(SomeClass::class, 'someMethod');
echo (string)$parsedMethod;

Or you can use an additional classes ReflectionFile and ReflectionFileNamespace to analyse a raw PHP files:

$parsedFile     = new \Go\ParserReflection\ReflectionFile('SomeClass.php');
$fileNameSpaces = $parsedFile->getFileNamespaces();
// We can iterate over namespaces in the file
foreach ($fileNameSpaces as $namespace) {
    $classes = $namespace->getClasses();
    // Iterate over the classes in the namespace
    foreach ($classes as $class) {
        echo "Found class: ", $class->getName(), PHP_EOL;
        // Now let's show all methods in the class
        foreach ($class->getMethods() as $method) {
            echo "Found class method: ", $class->getName(), '::', $method->getName(), PHP_EOL;
        }
        
        // ...all properties in the class
        foreach ($class->getProperties() as $property) {
            echo "Found class property: ", $class->getName(), '->', $property->getName(), PHP_EOL;
        }
    }
}

How it works?

To understand how library works let's look at what happens during the call to the new \Go\ParserReflection\ReflectionClass(SomeClass::class)

  • \Go\ParserReflection\ReflectionClass asks reflection engine to give an AST node for the given class name
  • Reflection engine asks a locator to locate a filename for the given class
  • ComposerLocator instance asks the Composer to find a filename for the given class and returns this result back to the reflection engine
  • Reflection engine loads the content of file and passes it to the PHP-Parser for tokenization and processing
  • PHP-Parser returns an AST (Abstract Syntax Tree)
  • Reflection engine then analyse this AST to extract specific nodes and wrap them into corresponding reflection classes.

Compatibility

All parser reflection classes extend PHP internal reflection classes, this means that you can use \Go\ParserReflection\ReflectionClass instance in any place that asks for \ReflectionClass instance. All reflection methods should be compatible with original ones, providing an except methods that requires object manipulation, such as invoke(), invokeArgs(), setAccessible(), etc. These methods will trigger the process of class loading and switching to the internal reflection.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].