Skip to content

Commit

Permalink
One reflector to rule them all
Browse files Browse the repository at this point in the history
one reflector to find them
one reflector to bring them all
and in the reflection bind them
  • Loading branch information
kukulich committed Oct 11, 2021
1 parent 5cb859b commit 4be375f
Show file tree
Hide file tree
Showing 74 changed files with 1,198 additions and 1,413 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ $ composer require roave/better-reflection
use Roave\BetterReflection\BetterReflection;

$classInfo = (new BetterReflection())
->classReflector()
->reflect(\Foo\Bar\MyClass::class);
->reflector()
->reflectClass(\Foo\Bar\MyClass::class);
```

## Documentation
Expand Down
3 changes: 2 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ or backwards compatibility (BC) breakages occur.
## 5.0.0

### BC Breaks
* `ClassReflector`, `FunctionReflector` and `ConstantReflector` have been removed. Use `DefaultReflector` to reflect all types.
* Removed support for PHP4 style constructors
* Adapters don't have `::export()` method anymore because these methods were removed from PHP. These methods has been removed:
* Adapters don't have `::export()` method anymore because these methods were removed from PHP. These methods have been removed:
* `\Roave\BetterReflection\Reflection\Adapter\ReflectionClass::export()`
* `\Roave\BetterReflection\Reflection\Adapter\ReflectionFunction::export()`
* `\Roave\BetterReflection\Reflection\Adapter\ReflectionMethod::export()`
Expand Down
2 changes: 1 addition & 1 deletion demo/basic-reflection/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Roave\BetterReflection\BetterReflection;

$reflection = (new BetterReflection())->classReflector()->reflect(\stdClass::class);
$reflection = (new BetterReflection())->reflector()->reflectClass(\stdClass::class);

echo $reflection->getName() . "\n"; // stdClass
echo ($reflection->isInternal() === true ? 'internal' : 'not internal') . "\n"; // internal
2 changes: 1 addition & 1 deletion demo/basic-reflection/example2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require_once __DIR__ . '/../../vendor/autoload.php';

$reflection = (new BetterReflection())->classReflector()->reflect(ReflectionClass::class);
$reflection = (new BetterReflection())->reflector()->reflectClass(ReflectionClass::class);

echo $reflection->getName() . "\n"; // ReflectionClass
echo ($reflection->isInternal() === true ? 'internal' : 'not internal') . "\n"; // not internal
6 changes: 3 additions & 3 deletions demo/basic-reflection/example3.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
require_once __DIR__ . '/../../vendor/autoload.php';

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;

$reflector = new ClassReflector(new AggregateSourceLocator([
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new AggregateSourceLocator([
new SingleFileSourceLocator(__DIR__ . '/assets/MyClass.php', (new BetterReflection())->astLocator()),
]));

$reflection = $reflector->reflect('MyClass');
$reflection = $reflector->reflectClass('MyClass');

echo $reflection->getName() . "\n"; // MyClass
echo ($reflection->getProperty('foo')->isPrivate() === true ? 'private' : 'not private') . "\n"; // private
Expand Down
6 changes: 3 additions & 3 deletions demo/monkey-patching/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
require_once __DIR__ . '/../../vendor/autoload.php';

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
use Roave\BetterReflection\Util\Autoload\ClassLoader;
use Roave\BetterReflection\Util\Autoload\ClassLoaderMethod\FileCacheLoader;

$loader = new ClassLoader(FileCacheLoader::defaultFileCacheLoader(__DIR__));

// Create the reflection first (without loading)
$classInfo = (new ClassReflector(
$classInfo = (new \Roave\BetterReflection\Reflector\DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/MyClass.php', (new BetterReflection())->astLocator())
))->reflect('MyClass');
))->reflectClass('MyClass');
$loader->addClass($classInfo);

// Override the body...!
Expand Down
6 changes: 3 additions & 3 deletions demo/parsing-whole-directory/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_once __DIR__ . '/../../vendor/autoload.php';

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;

$directories = [__DIR__ . '/../../src'];
Expand All @@ -15,8 +15,8 @@
(new BetterReflection())->astLocator()
);

$classReflector = new ClassReflector($sourceLocator);
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector($sourceLocator);

$classReflections = $classReflector->getAllClasses();
$classReflections = $reflector->reflectAllClasses();

!empty($classReflections) && print 'success';
6 changes: 3 additions & 3 deletions demo/parsing-whole-directory/example2.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_once __DIR__ . '/../../vendor/autoload.php';

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
Expand All @@ -21,8 +21,8 @@
new AutoloadSourceLocator((new BetterReflection())->astLocator())
]);

$classReflector = new ClassReflector($sourceLocator);
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector($sourceLocator);

$classReflections = $classReflector->getAllClasses();
$classReflections = $reflector->reflectAllClasses();

!empty($classReflections) && print 'success';
8 changes: 4 additions & 4 deletions docs/ast-extraction.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ few lines of code:
<?php

$classInfo = (new \Roave\BetterReflection\BetterReflection())
->classReflector()
->reflect(\Foo\Bar\MyClass::class);
->reflector()
->reflectClass(\Foo\Bar\MyClass::class);

// Retrieves the AST statements array *within* the method's curly braces
$ast = $classInfo->getMethod('foo')->getBodyAst();
Expand All @@ -33,8 +33,8 @@ and `ReflectionFunction`.
<?php

$classInfo = (new \Roave\BetterReflection\BetterReflection())
->classReflector()
->reflect(\Foo\Bar\MyClass::class);
->reflector()
->reflectClass(\Foo\Bar\MyClass::class);

// Retrieves AST nodes for the entire class (including the class definition)
$ast = $classInfo->getAst();
Expand Down
7 changes: 4 additions & 3 deletions docs/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ The first entry point is a minimal service locator provided by the library:

$betterReflection = new \Roave\BetterReflection\BetterReflection();

$class = $betterReflection->classReflector()->reflect('ClassName');
$function = $betterReflection->functionReflector()->reflect('functionName');
$reflector = $betterReflection->reflector();
$class = $reflector->reflectClass('ClassName');
$function = $reflector->reflectFunction('functionName');
```

#### 1.2 The `Reflection*` static methods
Expand Down Expand Up @@ -45,7 +46,7 @@ use a default set of pre-configured Source Locators:

#### 1.3 Manually instantiating `*Reflector` objects

The second entry point is the `ClassReflector` and `FunctionReflector` objects, to which you must provide specific
The second entry point is the `Reflector` objects, to which you must provide specific
instructions on how Better Reflection is to find code, in the form of the Source Locators.

## 2. Source Locators
Expand Down
2 changes: 1 addition & 1 deletion docs/reflection-modification.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can replace the body of the function like so:

use Roave\BetterReflection\BetterReflection;

$classInfo = (new BetterReflection())->classReflector()->reflect('MyClass');
$classInfo = (new BetterReflection())->reflector()->reflectClass('MyClass');

$classInfo->getMethod('foo')->setBody(function () {
return 4;
Expand Down
2 changes: 1 addition & 1 deletion docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MyClass

use Roave\BetterReflection\BetterReflection;

$classInfo = (new BetterReflection())->classReflector()->reflect('MyClass');
$classInfo = (new BetterReflection())->reflector()->reflectClass('MyClass');
$methodInfo = $classInfo->getMethod('myMethod');
$parameterInfo = $methodInfo->getParameter('myParameter');

Expand Down
53 changes: 26 additions & 27 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Basic Usage

The starting point for creating a reflection class does not match the typical core reflection API. Instead of
instantiating a `new \ReflectionClass`, you must use the appropriate `\Roave\BetterReflection\Reflector\ClassReflector`
instantiating a `new \ReflectionClass`, you must use the appropriate `\Roave\BetterReflection\Reflector\Reflector`
helper.

All `*Reflector` classes require a class that implements the `SourceLocator` interface as a dependency.
Expand All @@ -19,8 +19,8 @@ autoloader that Composer provides will work with this technique.
use Roave\BetterReflection\BetterReflection;

$classInfo = (new BetterReflection)
->classReflector()
->reflect(\Foo\Bar\MyClass::class);
->reflector()
->reflectClass(\Foo\Bar\MyClass::class);
```

If this instantiation technique is not possible - for example, your autoloader does not load classes from file, then
Expand Down Expand Up @@ -103,7 +103,7 @@ A `SourceLocator` is a callable, which when invoked must be given an `Identifier

## Reflecting Classes

The `ClassReflector` is used to create Better Reflection `ReflectionClass` instances. You may pass it any
The `Reflector` is used to create Better Reflection `ReflectionClass` instances. You may pass it any
`SourceLocator` to reflect on any class that can be located using the given that `SourceLocator`.

### Using the AutoloadSourceLocator
Expand Down Expand Up @@ -137,18 +137,18 @@ Here's an example of `MakeLocatorForComposerJsonAndInstalledJson` usage:
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForComposerJsonAndInstalledJson;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(new AggregateSourceLocator([
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new AggregateSourceLocator([
(new MakeLocatorForComposerJsonAndInstalledJson)('path/to/the/project', $astLocator),
new PhpInternalSourceLocator($astLocator)
]));

$classes = $reflector->getAllClasses();
$classes = $reflector->reflectAllClasses();
```

### Using the Composer autoloader directly
Expand All @@ -157,14 +157,14 @@ $classes = $reflector->getAllClasses();
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator;

$classLoader = require 'vendor/autoload.php';

$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(new ComposerSourceLocator($classLoader, $astLocator));
$reflectionClass = $reflector->reflect('Foo\Bar\MyClass');
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new ComposerSourceLocator($classLoader, $astLocator));
$reflectionClass = $reflector->reflectClass('Foo\Bar\MyClass');

echo $reflectionClass->getShortName(); // MyClass
echo $reflectionClass->getName(); // Foo\Bar\MyClass
Expand All @@ -177,12 +177,12 @@ echo $reflectionClass->getNamespaceName(); // Foo\Bar
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(new SingleFileSourceLocator('path/to/MyApp/MyClass.php', $astLocator));
$reflectionClass = $reflector->reflect('MyApp\MyClass');
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new SingleFileSourceLocator('path/to/MyApp/MyClass.php', $astLocator));
$reflectionClass = $reflector->reflectClass('MyApp\MyClass');

echo $reflectionClass->getShortName(); // MyClass
echo $reflectionClass->getName(); // MyApp\MyClass
Expand All @@ -195,14 +195,14 @@ echo $reflectionClass->getNamespaceName(); // MyApp
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;

$code = '<?php class Foo {};';

$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(new StringSourceLocator($code, $astLocator));
$reflectionClass = $reflector->reflect('Foo');
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new StringSourceLocator($code, $astLocator));
$reflectionClass = $reflector->reflectClass('Foo');

echo $reflectionClass->getShortName(); // Foo
```
Expand All @@ -213,12 +213,12 @@ echo $reflectionClass->getShortName(); // Foo
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new ClassReflector(new SingleFileSourceLocator('path/to/file.php', $astLocator));
$classes = $reflector->getAllClasses();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new SingleFileSourceLocator('path/to/file.php', $astLocator));
$classes = $reflector->reflectAllClasses();
```

### Fetch reflections of all the classes in one or more directories
Expand All @@ -227,19 +227,19 @@ $classes = $reflector->getAllClasses();
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$directoriesSourceLocator = new DirectoriesSourceLocator(['path/to/directory1'], $astLocator);
$reflector = new ClassReflector($directoriesSourceLocator);
$classes = $reflector->getAllClasses();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector($directoriesSourceLocator);
$classes = $reflector->reflectAllClasses();
```


## Reflecting Functions

The `FunctionReflector` is used to create Better Reflection `ReflectionFunction` instances. You may pass it any
The `Reflector` is used to create Better Reflection `ReflectionFunction` instances. You may pass it any
`SourceLocator` to reflect on any class that can be located using the given `SourceLocator`.

### Using the AutoloadSourceLocator
Expand All @@ -252,16 +252,15 @@ See example in "Reflecting Classes" section on the same subheading.
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\FunctionReflector;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;

$configuration = new BetterReflection();
$astLocator = $configuration->astLocator();
$classReflector = $configuration->classReflector();
$reflector = $configuration->reflector();

$directoriesSourceLocator = new DirectoriesSourceLocator(['path/to/directory1'], $astLocator);
$reflector = new FunctionReflector($directoriesSourceLocator, $classReflector);
$functions = $reflector->getAllFunctions();
$functions = $reflector->reflectAllFunctions();
```

### Reflecting a Closure
Expand Down
Loading

0 comments on commit 4be375f

Please sign in to comment.