Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer PHP 8 attributes over xp::$meta #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
os: [ubuntu-latest, windows-latest]

steps:
Expand Down
5 changes: 1 addition & 4 deletions src/main/php/lang/Reflection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
abstract class Reflection {
private static $meta= null;

public static function annotations($version) {
return $version >= 80000 ? new FromAttributes() : new FromSyntaxTree();
}

/** Lazy-loads meta information extraction */
public static function meta(): MetaInformation {
return self::$meta ?? self::$meta= new MetaInformation(self::annotations(PHP_VERSION_ID));
return self::$meta ?? self::$meta= new MetaInformation();
}

/**
Expand Down
73 changes: 58 additions & 15 deletions src/main/php/lang/meta/FromSyntaxTree.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function evaluate($reflect, $code) {
* @param lang.ast.nodes.Annotated $annotated
* @return [:var]
*/
private function annotations($tree, $annotated) {
private function treeAnnotations($tree, $annotated) {
if (null === $annotated->annotations) return [];

$r= [];
Expand All @@ -130,6 +130,20 @@ private function annotations($tree, $annotated) {
return $r;
}

/**
* Constructs annotations from meta information
*
* @param [:var] $meta
* @return [:var]
*/
private function metaAnnotations($meta) {
$r= [];
foreach ($meta[DETAIL_ANNOTATIONS] as $name => $value) {
$r[$meta[DETAIL_TARGET_ANNO][$name] ?? $name]= (array)$value;
}
return $r;
}

public function imports($reflect) {
$resolver= $this->tree($reflect->name)->resolver();
$imports= [];
Expand All @@ -141,35 +155,64 @@ public function imports($reflect) {

/** @return iterable */
public function ofType($reflect) {
$tree= $this->tree($reflect->name);
return $this->annotations($tree, $tree->type());
if ($meta= \xp::$meta[strtr($reflect->name, '\\', '.')]['class'] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($reflect->name);
return $this->treeAnnotations($tree, $tree->type());
}
}

/** @return iterable */
public function ofConstant($reflect) {
$tree= $this->tree($reflect->getDeclaringClass()->name);
return $this->annotations($tree, $tree->type()->constant($reflect->name));
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][2][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->constant($reflect->name));
}
}

/** @return iterable */
public function ofProperty($reflect) {
$tree= $this->tree($reflect->getDeclaringClass()->name);
return $this->annotations($tree, $tree->type()->property($reflect->name));
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][0][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->property($reflect->name));
}
}

/** @return iterable */
public function ofMethod($reflect) {
$tree= $this->tree($reflect->getDeclaringClass()->name);
return $this->annotations($tree, $tree->type()->method($reflect->name));
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][1][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->method($reflect->name));
}
}

/** @return iterable */
public function ofParameter($method, $reflect) {
$tree= $this->tree($method->getDeclaringClass()->name);
return $this->annotations($tree, $tree->type()
->method($method->name)
->signature
->parameters[$reflect->getPosition()]
);
$type= $reflect->getDeclaringClass()->name;

if ($target= \xp::$meta[strtr($type, '\\', '.')][1][$method->name][DETAIL_TARGET_ANNO] ?? null) {
$r= [];
foreach ($target['$'.$reflect->name] ?? [] as $name => $value) {
$r[$target[$name] ?? $name]= (array)$value;
}
return $r;
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()
->method($method->name)
->signature
->parameters[$reflect->getPosition()]
);
}
}
}
59 changes: 10 additions & 49 deletions src/main/php/lang/meta/MetaInformation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@
class MetaInformation {
private $annotations;

public function __construct($annotations) {
$this->annotations= $annotations;
public function __construct($annotations= null) {
$this->annotations= $annotations ?? self::annotations(PHP_VERSION_ID);
}

public function evaluate($reflect, $code) {
return $this->annotations->evaluate($reflect, $code);
public static function annotations($version) {
return $version >= 80000 ? new FromAttributes() : new FromSyntaxTree();
}

/**
* Constructs annotations from meta information
*
* @param [:var] $meta
* @return [:var]
*/
private function annotations($meta) {
$r= [];
foreach ($meta[DETAIL_ANNOTATIONS] as $name => $value) {
$r[$meta[DETAIL_TARGET_ANNO][$name] ?? $name]= (array)$value;
}
return $r;
public function evaluate($reflect, $code) {
return $this->annotations->evaluate($reflect, $code);
}

/**
Expand Down Expand Up @@ -64,11 +54,7 @@ public function scopeImports($reflect) {
* @return [:var[]]
*/
public function typeAnnotations($reflect) {
if ($meta= \xp::$meta[strtr($reflect->name, '\\', '.')]['class'] ?? null) {
return $this->annotations($meta);
} else {
return $this->annotations->ofType($reflect);
}
return $this->annotations->ofType($reflect);
}

/**
Expand All @@ -94,12 +80,7 @@ public function typeComment($reflect) {
* @return [:var[]]
*/
public function constantAnnotations($reflect) {
$c= strtr($reflect->getDeclaringClass()->name, '\\', '.');
if ($meta= \xp::$meta[$c][2][$reflect->name] ?? null) {
return $this->annotations($meta);
} else {
return $this->annotations->ofConstant($reflect);
}
return $this->annotations->ofConstant($reflect);
}

/**
Expand All @@ -126,12 +107,7 @@ public function constantComment($reflect) {
* @return [:var[]]
*/
public function propertyAnnotations($reflect) {
$c= strtr($reflect->getDeclaringClass()->name, '\\', '.');
if ($meta= \xp::$meta[$c][0][$reflect->name] ?? null) {
return $this->annotations($meta);
} else {
return $this->annotations->ofProperty($reflect);
}
return $this->annotations->ofProperty($reflect);
}

/**
Expand Down Expand Up @@ -174,12 +150,7 @@ public function propertyComment($reflect) {
* @return [:var[]]
*/
public function methodAnnotations($reflect) {
$c= strtr($reflect->getDeclaringClass()->name, '\\', '.');
if ($meta= \xp::$meta[$c][1][$reflect->name] ?? null) {
return $this->annotations($meta);
} else {
return $this->annotations->ofMethod($reflect);
}
return $this->annotations->ofMethod($reflect);
}

/**
Expand Down Expand Up @@ -239,16 +210,6 @@ public function methodParameterTypes($method) {
* @return [:var[]]
*/
public function parameterAnnotations($method, $reflect) {
$c= strtr($method->getDeclaringClass()->name, '\\', '.');
if ($target= \xp::$meta[$c][1][$method->name][DETAIL_TARGET_ANNO] ?? null) {
if ($param= $target['$'.$reflect->name] ?? null) {
$r= [];
foreach ($param as $name => $value) {
$r[$target[$name] ?? $name]= (array)$value;
}
return $r;
}
}
return $this->annotations->ofParameter($method, $reflect);
}

Expand Down
11 changes: 10 additions & 1 deletion src/test/php/lang/reflection/unittest/Fixture.class.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php namespace lang\reflection\unittest;

#[Annotated('test')]
class Fixture {

#[Annotated('test')]
const TEST = 'test';

#[Annotated('test')]
public static $DEFAULT = null;

private $value;

/** @param var */
public function __construct($value= null) {
#[Annotated('test')]
public function __construct(
#[Annotated('test')]
$value= null
) {
$this->value= $value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace lang\reflection\unittest;

use lang\meta\MetaInformation;
use ReflectionClass;
use lang\meta\{MetaInformation, FromAttributes, FromSyntaxTree};
use unittest\{Assert, Before, After, Test};

class MetaInformationTest {
Expand Down Expand Up @@ -37,14 +38,24 @@ public function initialize() {
]
]
];
$this->reflect= new \ReflectionClass(Fixture::class);
$this->reflect= new ReflectionClass(Fixture::class);
}

#[After]
public function finalize() {
unset(\xp::$meta['lang.reflection.unittest.Fixture']);
}

#[Test, Values([70000, 70100, 70200, 70300, 70400])]
public function parser_for_php7($versionId) {
Assert::instance(FromSyntaxTree::class, MetaInformation::annotations($versionId));
}

#[Test, Values([80000, 80100, 80200, 80300])]
public function parser_for_php8($versionId) {
Assert::instance(FromAttributes::class, MetaInformation::annotations($versionId));
}

#[Test]
public function type_comment() {
Assert::equals('Test', (new MetaInformation(null))->typeComment($this->reflect));
Expand Down
11 changes: 0 additions & 11 deletions src/test/php/lang/reflection/unittest/ReflectionTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace lang\reflection\unittest;

use ReflectionClass, ReflectionObject;
use lang\meta\{FromAttributes, FromSyntaxTree};
use lang\reflection\Package;
use lang\{Reflection, Type, ClassNotFoundException};
use unittest\{Assert, Test, Values, Expect};
Expand Down Expand Up @@ -34,16 +33,6 @@ public function of_package_name() {
Assert::instance(Package::class, Reflection::of('lang.reflection.unittest'));
}

#[Test, Values([70000, 70100, 70200, 70300, 70400])]
public function parser_for_php7($versionId) {
Assert::instance(FromSyntaxTree::class, Reflection::annotations($versionId));
}

#[Test, Values([80000, 80100, 80200])]
public function parser_for_php8($versionId) {
Assert::instance(FromAttributes::class, Reflection::annotations($versionId));
}

#[Test, Expect(ClassNotFoundException::class)]
public function of_non_existant() {
Reflection::of('non.existant.Type');
Expand Down