Skip to content

Commit

Permalink
Merge pull request #284 from ray-di/remove_doctrine_annotation
Browse files Browse the repository at this point in the history
Remove doctrine/annotation
  • Loading branch information
koriym authored Sep 15, 2023
2 parents 751e66b + 7d9b0d2 commit fac2d0a
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 296 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@next_stable
with:
old_stable: '["7.2", "7.3", "7.4", "8.0"]'
current_stable: 8.1
next_stable: 8.2
current_stable: 8.2
next_stable: 8.3
script: demo/run.php
24 changes: 10 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
],
"require": {
"php": "^7.2 || ^8.0",
"doctrine/annotations": "^1.12",
"doctrine/cache": "^1.10 | ^2.1",
"doctrine/cache": "^1.10 || ^2.1",
"koriym/attributes": "^1.0.4",
"koriym/null-object": "^1.0",
"koriym/param-reader": "^1.0",
"koriym/printo": "^1.0",
"nikic/php-parser": "^4.13.2",
"ray/aop": "^2.12.3",
"ray/aop": "^2.14",
"ray/compiler": "^1.9.1"
},
"require-dev": {
Expand All @@ -44,25 +43,22 @@
"files": ["tests/deleteFiles.php"]
},
"scripts": {
"post-install-cmd": ["@composer bin all install --ansi"],
"post-update-cmd": ["@composer bin all update --ansi"],
"test": "./vendor/bin/phpunit --log-junit=build/junit.xml",
"test": "phpunit --log-junit=build/junit.xml",
"tests": ["@cs", "@sa", "@test"],
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"pcov": ["php -dextension=pcov.so -d pcov.enabled=1 ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage --coverage-clover=coverage.xml"],
"cs": ["./vendor/bin/phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["./vendor/bin/phpcbf src tests"],
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["./vendor/bin/psalm -c psalm.xml --show-info=true", "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress "],
"metrics": ["@test", "./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["./vendor/bin/phpmd src/di text ./phpmd.xml"],
"cs": ["phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["phpcbf src tests"],
"clean": ["phpstan clear-result-cache", "psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["psalm -m -c psalm.xml --show-info=true", "phpstan analyse -c phpstan.neon --no-progress "],
"metrics": ["@test", "phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["phpmd src/di text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"target-directory": "vendor-bin",
"forward-command": false
"forward-command": true
}
}
}
13 changes: 4 additions & 9 deletions src/di/AnnotatedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

namespace Ray\Di;

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

final class AnnotatedClass
{
/** @var Reader */
private $reader;

/** @var AnnotatedClassMethods */
private $injectionMethod;

public function __construct(Reader $reader)
public function __construct()
{
$this->reader = $reader;
$this->injectionMethod = new AnnotatedClassMethods($reader);
$this->injectionMethod = new AnnotatedClassMethods();
}

/**
Expand Down Expand Up @@ -54,7 +49,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
21 changes: 8 additions & 13 deletions src/di/AnnotatedClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@

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;

final class AnnotatedClassMethods
{
/** @var Reader */
private $reader;

/** @var NameKeyVarString */
private $nameKeyVarString;

public function __construct(Reader $reader)
public function __construct()
{
$this->reader = $reader;
$this->nameKeyVarString = new NameKeyVarString($reader);
$this->nameKeyVarString = new NameKeyVarString();
}

/**
Expand All @@ -37,18 +32,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->getName(), $constructor->getName()));
if ($name !== null) {
return new Name($name);
}
Expand All @@ -58,7 +53,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 Down
3 changes: 1 addition & 2 deletions src/di/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Ray\Di;

use Ray\Di\Exception\Unbound;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionMethod;

final class Arguments
Expand Down Expand Up @@ -65,6 +64,6 @@ private function bindInjectionPoint(Container $container, Argument $argument): v
return;
}

(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get(), ServiceLocator::getReader()));
(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get()));
}
}
12 changes: 8 additions & 4 deletions src/di/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Ray\Di;

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

Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(Container $container, string $interface)
$this->container = $container;
$this->interface = $interface;
$this->validate = new BindValidator();
$bindUntarget = class_exists($interface) && ! (new ReflectionClass($interface))->isAbstract() && ! $this->isRegistered($interface);
$bindUntarget = class_exists($interface) && ! (new \ReflectionClass($interface))->isAbstract() && ! $this->isRegistered($interface);
$this->bound = new NullDependency();
if ($bindUntarget) {
$this->untarget = new Untarget($interface);
Expand Down Expand Up @@ -103,10 +103,12 @@ public function to(string $class): self
/**
* Bind to constructor
*
* @param class-string $class class name
* @param class-string<T> $class class name
* @param array<string, string>|string $name "varName=bindName,..." or [$varName => $bindName, $varName => $bindName...]
*
* @throws ReflectionException
*
* @template T of object
*/
public function toConstructor(string $class, $name, ?InjectionPoints $injectionPoints = null, ?string $postConstruct = null): self
{
Expand All @@ -116,7 +118,9 @@ public function toConstructor(string $class, $name, ?InjectionPoints $injectionP

$this->untarget = null;
$postConstructRef = $postConstruct ? new ReflectionMethod($class, $postConstruct) : null;
$this->bound = (new DependencyFactory())->newToConstructor(new ReflectionClass($class), $name, $injectionPoints, $postConstructRef);
/** @var ReflectionClass<object> $reflection */
$reflection = new ReflectionClass($class);
$this->bound = (new DependencyFactory())->newToConstructor($reflection, $name, $injectionPoints, $postConstructRef);
$this->container->add($this);

return $this;
Expand Down
12 changes: 7 additions & 5 deletions 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 Expand Up @@ -42,15 +42,16 @@ public function to(string $interface, string $class): ReflectionClass
throw new InvalidType("[{$class}] is no implemented [{$interface}] interface");
}

return new ReflectionClass($class);
return new ReflectionClass($class); // @phpstan-ignore-line
}

/**
* toProvider validator
*
* @phpstan-param class-string $provider
*
* @return ReflectionClass<object>
* @psalm-return ReflectionClass
* @phpstan-return ReflectionClass<object>
*
* @throws NotFound
*/
Expand All @@ -61,11 +62,12 @@ public function toProvider(string $provider): ReflectionClass
throw new NotFound($provider);
}

if (! (new ReflectionClass($provider))->implementsInterface(ProviderInterface::class)) {
$reflectioClass = new ReflectionClass($provider);
if (! $reflectioClass->implementsInterface(ProviderInterface::class)) {
throw new InvalidProvider($provider);
}

return new ReflectionClass($provider);
return $reflectioClass;
}

private function isNullInterceptorBinding(string $class, string $interface): bool
Expand Down
5 changes: 2 additions & 3 deletions src/di/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Ray\Di;

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

use function assert;
Expand All @@ -20,7 +19,7 @@ final class DependencyFactory
*/
public function newAnnotatedDependency(ReflectionClass $class): Dependency
{
$annotateClass = new AnnotatedClass(ServiceLocator::getReader());
$annotateClass = new AnnotatedClass();
$newInstance = $annotateClass->getNewInstance($class);
$postConstruct = $annotateClass->getPostConstruct($class);

Expand Down
Loading

0 comments on commit fac2d0a

Please sign in to comment.