Skip to content

Commit

Permalink
Do not use annotation reader
Browse files Browse the repository at this point in the history
Update native ReflectionClass/Method annotation readable
  • Loading branch information
koriym committed Sep 7, 2023
1 parent 751e66b commit 17b58a6
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/di/AnnotatedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(Reader $reader)
*
* @phpstan-param ReflectionClass<object> $class Target class reflection
*/
public function getNewInstance(ReflectionClass $class): NewInstance
public function getNewInstance(\Ray\Aop\ReflectionClass $class): NewInstance
{
$setterMethods = new SetterMethods([]);
$methods = $class->getMethods();
Expand All @@ -54,7 +54,7 @@ public function getPostConstruct(ReflectionClass $class): ?ReflectionMethod
{
$methods = $class->getMethods();
foreach ($methods as $method) {
$annotation = $this->reader->getMethodAnnotation($method, PostConstruct::class);
$annotation = $method->getAnnotation(PostConstruct::class);
if ($annotation instanceof PostConstruct) {
return $method;
}
Expand Down
14 changes: 7 additions & 7 deletions src/di/AnnotatedClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\InjectInterface;
use Ray\Di\Di\Named;
use ReflectionClass;
use ReflectionMethod;

use const PHP_VERSION_ID;

Expand Down Expand Up @@ -37,18 +37,18 @@ public function getConstructorName(ReflectionClass $class): Name
}

if (PHP_VERSION_ID >= 80000) {
$name = Name::withAttributes(new ReflectionMethod($class->getName(), '__construct'));
$name = Name::withAttributes(new \ReflectionMethod($class->getName(), '__construct'));
if ($name) {
return $name;
}
}

$named = $this->reader->getMethodAnnotation($constructor, Named::class);
$named = $constructor->getAnnotation(Named::class);
if ($named instanceof Named) {
return new Name($named->value);
}

$name = ($this->nameKeyVarString)($constructor);
$name = ($this->nameKeyVarString)(new ReflectionMethod($class->name, $constructor->name));
if ($name !== null) {
return new Name($name);
}
Expand All @@ -58,7 +58,7 @@ public function getConstructorName(ReflectionClass $class): Name

public function getSetterMethod(ReflectionMethod $method): ?SetterMethod
{
$inject = $this->reader->getMethodAnnotation($method, InjectInterface::class);
$inject = $method->getAnnotation(InjectInterface::class);
if (! $inject instanceof InjectInterface) {
return null;
}
Expand All @@ -72,7 +72,7 @@ public function getSetterMethod(ReflectionMethod $method): ?SetterMethod
return $setterMethod;
}

private function getName(ReflectionMethod $method): Name
private function getName(\Ray\Aop\ReflectionMethod $method): Name
{
if (PHP_VERSION_ID >= 80000) {
$name = Name::withAttributes($method);
Expand Down
4 changes: 2 additions & 2 deletions src/di/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Ray\Di;

use Ray\Aop\MethodInterceptor;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Exception\InvalidToConstructorNameParameter;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;

use function array_keys;
use function array_reduce;
Expand Down
2 changes: 1 addition & 1 deletion src/di/BindValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Ray\Aop\MethodInterceptor;
use Ray\Aop\NullInterceptor;
use Ray\Aop\ReflectionClass;
use Ray\Di\Exception\InvalidProvider;
use Ray\Di\Exception\InvalidType;
use Ray\Di\Exception\NotFound;
use ReflectionClass;

use function class_exists;
use function interface_exists;
Expand Down
2 changes: 1 addition & 1 deletion src/di/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Ray\Di;

use Ray\Aop\ReflectionClass;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionClass;
use ReflectionMethod;

use function assert;
Expand Down
20 changes: 6 additions & 14 deletions src/di/InjectionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Qualifier;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
use Serializable;

Expand All @@ -21,9 +21,6 @@ final class InjectionPoint implements InjectionPointInterface, Serializable
/** @var ?ReflectionParameter */
private $parameter;

/** @var Reader */
private $reader;

/** @var string */
private $pClass;

Expand Down Expand Up @@ -58,7 +55,6 @@ public function getMethod(): ReflectionMethod
{
$this->parameter = $this->getParameter();
$class = $this->parameter->getDeclaringClass();
assert($class instanceof ReflectionClass);
$method = $this->parameter->getDeclaringFunction()->getShortName();
assert(class_exists($class->name));

Expand All @@ -72,9 +68,8 @@ public function getClass(): ReflectionClass
{
$this->parameter = $this->getParameter();
$class = $this->parameter->getDeclaringClass();
assert($class instanceof ReflectionClass);

return $class;
return new ReflectionClass($class->name);
}

/**
Expand All @@ -83,13 +78,10 @@ public function getClass(): ReflectionClass
public function getQualifiers(): array
{
$qualifiers = [];
$annotations = $this->reader->getMethodAnnotations($this->getMethod());
$annotations = $this->getMethod()->getAnnotations();
foreach ($annotations as $annotation) {
$qualifier = $this->reader->getClassAnnotation(
new ReflectionClass($annotation),
Qualifier::class
);
if ($qualifier instanceof Qualifier) {
$maybeQualifier = (new ReflectionClass($annotation))->getAnnotation(Qualifier::class);
if ($maybeQualifier instanceof Qualifier) {
$qualifiers[] = $annotation;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/di/InjectionPointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Ray\Di;

use ReflectionClass;
use ReflectionMethod;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use ReflectionParameter;

interface InjectionPointInterface
Expand Down
10 changes: 5 additions & 5 deletions src/di/NameKeyVarString.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Ray\Di;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Named;
use Ray\Di\Di\Qualifier;
use ReflectionClass;
use ReflectionMethod;

use function get_class;
use function implode;
Expand All @@ -27,7 +27,7 @@ public function __construct(Reader $reader)
public function __invoke(ReflectionMethod $method): ?string
{
$keyVal = [];
$named = $this->reader->getMethodAnnotation($method, Named::class);
$named = $method->getAnnotation(Named::class);
if ($named instanceof Named) {
$keyVal[] = $named->value;
}
Expand All @@ -42,10 +42,10 @@ public function __invoke(ReflectionMethod $method): ?string

private function getQualifierKeyVarString(ReflectionMethod $method): string
{
$annotations = $this->reader->getMethodAnnotations($method);
$annotations = $method->getAnnotations();
$names = [];
foreach ($annotations as $annotation) {
$qualifier = $this->reader->getClassAnnotation(new ReflectionClass($annotation), Qualifier::class);
$qualifier = (new ReflectionClass($annotation))->getAnnotation(Qualifier::class);
if ($qualifier instanceof Qualifier) {
/** @var ?scalar $annotation->value */
$value = $annotation->value ?? Name::ANY; // @phpstan-ignore-line
Expand Down
2 changes: 1 addition & 1 deletion src/di/Untarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Ray\Di;

use ReflectionClass;
use Ray\Aop\ReflectionClass;

final class Untarget
{
Expand Down
2 changes: 1 addition & 1 deletion tests/di/AnnotatedClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\Common\Annotations\AnnotationReader;
use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Ray\Aop\ReflectionClass;

class AnnotatedClassTest extends TestCase
{
Expand Down

0 comments on commit 17b58a6

Please sign in to comment.