Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
15d4de2
Add lang.reflection.Closure to reflect callable closures
thekid Nov 7, 2026
a139ede
Rename Closure -> Invocable
thekid Nov 9, 2026
b8c2f5b
QA: Use imports for global classes
thekid Nov 9, 2026
9483a5b
Add Invocable::name()
thekid Nov 9, 2026
f8dd92d
Allow creating Invocable instances from callables
thekid Nov 9, 2026
6e7bb7d
Add annotation support for invocables
thekid Nov 9, 2026
7aceae2
Annotations on invocables raise an exception in PHP 7
thekid Nov 9, 2026
596d3ce
PHP 7.4 syntax
thekid Nov 9, 2026
3bb212d
Wrap closure annotations in `eval()`
thekid Nov 9, 2026
01cffa2
Fully qualifiy annotation types
thekid Nov 9, 2026
dd21db6
Add Method::invocable()
thekid Nov 14, 2026
00bd816
Add Reflection::invocable()
thekid Nov 14, 2026
78a5137
Make parameters work with both functions and methods
thekid Nov 14, 2026
5bcbd1d
Add package()
thekid Nov 14, 2026
67ddd8a
Consistently make anonymous invocables return NULL for their name
thekid Nov 14, 2026
16f21bb
Implement Annotated and Value
thekid Nov 14, 2026
4dd0256
Add Invocation::literal() and ::declaredName()
thekid Nov 14, 2026
9ae247c
Make it more efficient to create invocables from methods
thekid Nov 14, 2026
83b0166
Verify instance
thekid Nov 14, 2026
d7d9669
Make Reflection::of() accept closures and reflection functions
thekid Nov 14, 2026
d125652
Make Reflection::of() accept closures, invocables and reflection func…
thekid Nov 14, 2026
6ac0858
Make anonymous classes and invocables consistent
thekid Nov 14, 2026
6e6e518
Use setAccessible() to invoke private method references
thekid Nov 14, 2026
823d3eb
Use compound name in hashCode(), toString(), compareTo()
thekid Nov 14, 2026
916fdf8
Add anonymous() to Type and Invocable
thekid Nov 14, 2026
5a0d661
Fix "Calling ReflectionMethod::invokeArgs() for static method [...] d…
thekid Nov 14, 2026
3c04efa
Honor any given receiver
thekid Nov 14, 2026
86a158a
Simplify Invocable constructor
thekid Nov 14, 2026
ba14d2b
QA: Document invocables [skip ci]
thekid Nov 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use org\example\{Base, Inject, Fixture};

$type= Reflection::type(Fixture::class);

$type->anonymous(); // FALSE
$type->name(); // org.example.Fixture
$type->declaredName(); // Fixture
$type->literal(); // Fixture::class
Expand Down Expand Up @@ -144,6 +145,7 @@ foreach ($type->methods() as $name => $method) {
$method->parameters(); // Parameters
$method->parameter(0); // Parameter or NULL
$method->closure($instance); // Closure instance
$method->invocable($instance); // Invocable
$method->invoke($instance, []); // (method return value)
}
```
Expand Down Expand Up @@ -175,12 +177,33 @@ foreach ($parameters as $name => $parameter) {
}
```

Packages can be reflected upon by passing namespace names to `Reflection::of()`.
Closure can be reflected upon via the *Invocable* class:

```php
use lang\Reflection;

$package= Reflection::of('org.example');
$invocable= Reflection::invocable('strlen');

$invocable->anonymous(); // FALSE
$invocable->name(); // strlen
$invocable->declaredName(); // strlen
$invocable->literal(); // strlen
$invocable->comment(); // (api doc comment)
$invocable->package(); // Package or NULL
$invocable->annotations(); // Annotations
$invocable->annotation(Inject::class); // Annotation or NULL
$invocable->returns(); // Constraint
$invocable->parameters(); // Parameters
$invocable->parameter(0); // Parameter or NULL
$invocable->invoke(['Test']); // strlen("Test") = 4
```

Packages can be reflected upon by passing namespace names to `Reflection::package()`.

```php
use lang\Reflection;

$package= Reflection::package('org.example');

$package->name(); // org.example
$package->literal(); // 'org\example'
Expand Down
50 changes: 32 additions & 18 deletions src/main/php/lang/Reflection.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace lang;

use Closure, ReflectionClass, ReflectionObject, ReflectionFunction, ReflectionException;
use lang\meta\{MetaInformation, FromSyntaxTree, FromAttributes};
use lang\reflection\{Type, Package};
use lang\reflection\{Invocable, Type, Package};
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};

/**
Expand All @@ -22,6 +23,7 @@
abstract class Reflection {
private static $meta= null;

/** Returns annotation parser */
public static function annotations($version) {
return $version >= 80000 ? new FromAttributes() : new FromSyntaxTree();
}
Expand All @@ -34,23 +36,33 @@ public static function meta(): MetaInformation {
/**
* Returns a reflection type for a given argument.
*
* @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg
* @param string|callable $arg
* @return lang.reflection.Invocable
*/
public static function invocable($arg) {
return $arg instanceof Invocable ? $arg : new Invocable($arg);
}

/**
* Returns a reflection type for a given argument.
*
* @param string|object $arg
* @return lang.reflection.Type
* @throws lang.ClassNotFoundException
*/
public static function type($arg) {
if ($arg instanceof XPClass) {
return new Type($arg->reflect());
} else if ($arg instanceof \ReflectionClass) {
} else if ($arg instanceof ReflectionClass) {
return new Type($arg);
} else if ($arg instanceof Type) {
return $arg;
} else if (is_object($arg)) {
return new Type(new \ReflectionObject($arg));
return new Type(new ReflectionObject($arg));
} else {
try {
return new Type(new \ReflectionClass(strtr($arg, '.', '\\')));
} catch (\ReflectionException $e) {
return new Type(new ReflectionClass(strtr($arg, '.', '\\')));
} catch (ReflectionException $e) {
throw new ClassNotFoundException($arg, [ClassLoader::getDefault()]);
}
}
Expand All @@ -59,16 +71,16 @@ public static function type($arg) {
/**
* Returns a reflection package for a given argument.
*
* @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg
* @param string|object $arg
* @return lang.reflection.Type
* @throws lang.IllegalArgumentException
*/
public static function package($arg) {
if ($arg instanceof XPClass) {
return new Package($arg->reflect()->getNamespaceName());
} else if ($arg instanceof \ReflectionClass) {
} else if ($arg instanceof ReflectionClass || $arg instanceof ReflectionFunction) {
return new Package($arg->getNamespaceName());
} else if ($arg instanceof Type) {
} else if ($arg instanceof Type || $arg instanceof Invocable) {
return $arg->package();
} else if (is_object($arg)) {
$class= get_class($arg);
Expand All @@ -86,34 +98,36 @@ public static function package($arg) {
}

/**
* Creates a new reflection instance, which may either refer to a type
* or to a package.
* Creates a new reflection instance, which may either refer to an invocable,
* a type or to a package.
*
* @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg
* @return lang.reflection.Type|lang.reflection.Package
* @param string|object $arg
* @return lang.reflection.Invocable|lang.reflection.Type|lang.reflection.Package
* @throws lang.ClassNotFoundException
*/
public static function of($arg) {
if ($arg instanceof XPClass) {
return new Type($arg->reflect());
} else if ($arg instanceof \ReflectionClass) {
} else if ($arg instanceof ReflectionClass) {
return new Type($arg);
} else if ($arg instanceof Type) {
return $arg;
} else if ($arg instanceof Closure || $arg instanceof ReflectionFunction) {
return new Invocable($arg);
} else if (is_object($arg)) {
return new Type(new \ReflectionObject($arg));
return new Type(new ReflectionObject($arg));
} else {
$cl= ClassLoader::getDefault();
$name= strtr($arg, '\\', '.');
if ($cl->providesClass($name)) {
return new Type(new \ReflectionClass($cl->loadClass0($name)));
return new Type(new ReflectionClass($cl->loadClass0($name)));
} else if ($cl->providesPackage($name)) {
return new Package($name);
}

try {
return new Type(new \ReflectionClass(strtr($arg, '.', '\\')));
} catch (\ReflectionException $e) {
return new Type(new ReflectionClass(strtr($arg, '.', '\\')));
} catch (ReflectionException $e) {
throw new ClassNotFoundException($name, [$cl]);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/php/lang/meta/FromAttributes.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace lang\meta;

use PhpToken;
use PhpToken, ReflectionMethod;
use lang\reflection\Annotation;

/**
Expand Down Expand Up @@ -30,6 +30,11 @@ private function annotations($reflect, $context) {
return $r;
}

/** @return iterable */
public function ofInvocable($reflect) {
return $this->annotations($reflect, $reflect);
}

/** @return iterable */
public function ofType($reflect) {
return $this->annotations($reflect, $reflect);
Expand All @@ -52,7 +57,7 @@ public function ofMethod($reflect) {

/** @return iterable */
public function ofParameter($method, $reflect) {
return $this->annotations($reflect, $method->getDeclaringClass());
return $this->annotations($reflect, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/php/lang/meta/FromSyntaxTree.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public function imports($reflect) {
return $imports;
}

/** @return iterable */
public function ofInvocable($reflect) {
throw new IllegalStateException('Invocables cannot have annotations in PHP 7');
}

/** @return iterable */
public function ofType($reflect) {
$tree= $this->tree($reflect, $reflect->getFileName());
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/meta/MetaInformation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* it to another source otherwise.
*/
class MetaInformation {
private $annotations;
public $annotations;

public function __construct($annotations) {
$this->annotations= $annotations;
Expand Down
Loading