Skip to content

Commit fac2d0a

Browse files
authored
Merge pull request #284 from ray-di/remove_doctrine_annotation
Remove doctrine/annotation
2 parents 751e66b + 7d9b0d2 commit fac2d0a

16 files changed

+396
-296
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ jobs:
1010
uses: ray-di/.github/.github/workflows/continuous-integration.yml@next_stable
1111
with:
1212
old_stable: '["7.2", "7.3", "7.4", "8.0"]'
13-
current_stable: 8.1
14-
next_stable: 8.2
13+
current_stable: 8.2
14+
next_stable: 8.3
1515
script: demo/run.php

composer.json

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
],
1212
"require": {
1313
"php": "^7.2 || ^8.0",
14-
"doctrine/annotations": "^1.12",
15-
"doctrine/cache": "^1.10 | ^2.1",
14+
"doctrine/cache": "^1.10 || ^2.1",
1615
"koriym/attributes": "^1.0.4",
1716
"koriym/null-object": "^1.0",
1817
"koriym/param-reader": "^1.0",
1918
"koriym/printo": "^1.0",
2019
"nikic/php-parser": "^4.13.2",
21-
"ray/aop": "^2.12.3",
20+
"ray/aop": "^2.14",
2221
"ray/compiler": "^1.9.1"
2322
},
2423
"require-dev": {
@@ -44,25 +43,22 @@
4443
"files": ["tests/deleteFiles.php"]
4544
},
4645
"scripts": {
47-
"post-install-cmd": ["@composer bin all install --ansi"],
48-
"post-update-cmd": ["@composer bin all update --ansi"],
49-
"test": "./vendor/bin/phpunit --log-junit=build/junit.xml",
46+
"test": "phpunit --log-junit=build/junit.xml",
5047
"tests": ["@cs", "@sa", "@test"],
5148
"coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
5249
"pcov": ["php -dextension=pcov.so -d pcov.enabled=1 ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage --coverage-clover=coverage.xml"],
53-
"cs": ["./vendor/bin/phpcs --standard=./phpcs.xml src tests"],
54-
"cs-fix": ["./vendor/bin/phpcbf src tests"],
55-
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
56-
"sa": ["./vendor/bin/psalm -c psalm.xml --show-info=true", "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress "],
57-
"metrics": ["@test", "./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
58-
"phpmd": ["./vendor/bin/phpmd src/di text ./phpmd.xml"],
50+
"cs": ["phpcs --standard=./phpcs.xml src tests"],
51+
"cs-fix": ["phpcbf src tests"],
52+
"clean": ["phpstan clear-result-cache", "psalm --clear-cache", "rm -rf tests/tmp/*.php"],
53+
"sa": ["psalm -m -c psalm.xml --show-info=true", "phpstan analyse -c phpstan.neon --no-progress "],
54+
"metrics": ["@test", "phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
55+
"phpmd": ["phpmd src/di text ./phpmd.xml"],
5956
"build": ["@cs", "@sa", "@pcov", "@metrics"]
6057
},
6158
"extra": {
6259
"bamarni-bin": {
6360
"bin-links": true,
64-
"target-directory": "vendor-bin",
65-
"forward-command": false
61+
"forward-command": true
6662
}
6763
}
6864
}

src/di/AnnotatedClass.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44

55
namespace Ray\Di;
66

7-
use Doctrine\Common\Annotations\Reader;
7+
use Ray\Aop\ReflectionClass;
88
use Ray\Di\Di\PostConstruct;
9-
use ReflectionClass;
109
use ReflectionMethod;
1110

1211
final class AnnotatedClass
1312
{
14-
/** @var Reader */
15-
private $reader;
16-
1713
/** @var AnnotatedClassMethods */
1814
private $injectionMethod;
1915

20-
public function __construct(Reader $reader)
16+
public function __construct()
2117
{
22-
$this->reader = $reader;
23-
$this->injectionMethod = new AnnotatedClassMethods($reader);
18+
$this->injectionMethod = new AnnotatedClassMethods();
2419
}
2520

2621
/**
@@ -54,7 +49,7 @@ public function getPostConstruct(ReflectionClass $class): ?ReflectionMethod
5449
{
5550
$methods = $class->getMethods();
5651
foreach ($methods as $method) {
57-
$annotation = $this->reader->getMethodAnnotation($method, PostConstruct::class);
52+
$annotation = $method->getAnnotation(PostConstruct::class);
5853
if ($annotation instanceof PostConstruct) {
5954
return $method;
6055
}

src/di/AnnotatedClassMethods.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,21 @@
44

55
namespace Ray\Di;
66

7-
use Doctrine\Common\Annotations\Reader;
7+
use Ray\Aop\ReflectionClass;
8+
use Ray\Aop\ReflectionMethod;
89
use Ray\Di\Di\InjectInterface;
910
use Ray\Di\Di\Named;
10-
use ReflectionClass;
11-
use ReflectionMethod;
1211

1312
use const PHP_VERSION_ID;
1413

1514
final class AnnotatedClassMethods
1615
{
17-
/** @var Reader */
18-
private $reader;
19-
2016
/** @var NameKeyVarString */
2117
private $nameKeyVarString;
2218

23-
public function __construct(Reader $reader)
19+
public function __construct()
2420
{
25-
$this->reader = $reader;
26-
$this->nameKeyVarString = new NameKeyVarString($reader);
21+
$this->nameKeyVarString = new NameKeyVarString();
2722
}
2823

2924
/**
@@ -37,18 +32,18 @@ public function getConstructorName(ReflectionClass $class): Name
3732
}
3833

3934
if (PHP_VERSION_ID >= 80000) {
40-
$name = Name::withAttributes(new ReflectionMethod($class->getName(), '__construct'));
35+
$name = Name::withAttributes(new \ReflectionMethod($class->getName(), '__construct'));
4136
if ($name) {
4237
return $name;
4338
}
4439
}
4540

46-
$named = $this->reader->getMethodAnnotation($constructor, Named::class);
41+
$named = $constructor->getAnnotation(Named::class);
4742
if ($named instanceof Named) {
4843
return new Name($named->value);
4944
}
5045

51-
$name = ($this->nameKeyVarString)($constructor);
46+
$name = ($this->nameKeyVarString)(new ReflectionMethod($class->getName(), $constructor->getName()));
5247
if ($name !== null) {
5348
return new Name($name);
5449
}
@@ -58,7 +53,7 @@ public function getConstructorName(ReflectionClass $class): Name
5853

5954
public function getSetterMethod(ReflectionMethod $method): ?SetterMethod
6055
{
61-
$inject = $this->reader->getMethodAnnotation($method, InjectInterface::class);
56+
$inject = $method->getAnnotation(InjectInterface::class);
6257
if (! $inject instanceof InjectInterface) {
6358
return null;
6459
}

src/di/Arguments.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Ray\Di;
66

77
use Ray\Di\Exception\Unbound;
8-
use Ray\ServiceLocator\ServiceLocator;
98
use ReflectionMethod;
109

1110
final class Arguments
@@ -65,6 +64,6 @@ private function bindInjectionPoint(Container $container, Argument $argument): v
6564
return;
6665
}
6766

68-
(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get(), ServiceLocator::getReader()));
67+
(new Bind($container, InjectionPointInterface::class))->toInstance(new InjectionPoint($argument->get()));
6968
}
7069
}

src/di/Bind.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Ray\Di;
66

77
use Ray\Aop\MethodInterceptor;
8+
use Ray\Aop\ReflectionClass;
89
use Ray\Di\Exception\InvalidToConstructorNameParameter;
9-
use ReflectionClass;
1010
use ReflectionException;
1111
use ReflectionMethod;
1212

@@ -51,7 +51,7 @@ public function __construct(Container $container, string $interface)
5151
$this->container = $container;
5252
$this->interface = $interface;
5353
$this->validate = new BindValidator();
54-
$bindUntarget = class_exists($interface) && ! (new ReflectionClass($interface))->isAbstract() && ! $this->isRegistered($interface);
54+
$bindUntarget = class_exists($interface) && ! (new \ReflectionClass($interface))->isAbstract() && ! $this->isRegistered($interface);
5555
$this->bound = new NullDependency();
5656
if ($bindUntarget) {
5757
$this->untarget = new Untarget($interface);
@@ -103,10 +103,12 @@ public function to(string $class): self
103103
/**
104104
* Bind to constructor
105105
*
106-
* @param class-string $class class name
106+
* @param class-string<T> $class class name
107107
* @param array<string, string>|string $name "varName=bindName,..." or [$varName => $bindName, $varName => $bindName...]
108108
*
109109
* @throws ReflectionException
110+
*
111+
* @template T of object
110112
*/
111113
public function toConstructor(string $class, $name, ?InjectionPoints $injectionPoints = null, ?string $postConstruct = null): self
112114
{
@@ -116,7 +118,9 @@ public function toConstructor(string $class, $name, ?InjectionPoints $injectionP
116118

117119
$this->untarget = null;
118120
$postConstructRef = $postConstruct ? new ReflectionMethod($class, $postConstruct) : null;
119-
$this->bound = (new DependencyFactory())->newToConstructor(new ReflectionClass($class), $name, $injectionPoints, $postConstructRef);
121+
/** @var ReflectionClass<object> $reflection */
122+
$reflection = new ReflectionClass($class);
123+
$this->bound = (new DependencyFactory())->newToConstructor($reflection, $name, $injectionPoints, $postConstructRef);
120124
$this->container->add($this);
121125

122126
return $this;

src/di/BindValidator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use Ray\Aop\MethodInterceptor;
88
use Ray\Aop\NullInterceptor;
9+
use Ray\Aop\ReflectionClass;
910
use Ray\Di\Exception\InvalidProvider;
1011
use Ray\Di\Exception\InvalidType;
1112
use Ray\Di\Exception\NotFound;
12-
use ReflectionClass;
1313

1414
use function class_exists;
1515
use function interface_exists;
@@ -42,15 +42,16 @@ public function to(string $interface, string $class): ReflectionClass
4242
throw new InvalidType("[{$class}] is no implemented [{$interface}] interface");
4343
}
4444

45-
return new ReflectionClass($class);
45+
return new ReflectionClass($class); // @phpstan-ignore-line
4646
}
4747

4848
/**
4949
* toProvider validator
5050
*
5151
* @phpstan-param class-string $provider
5252
*
53-
* @return ReflectionClass<object>
53+
* @psalm-return ReflectionClass
54+
* @phpstan-return ReflectionClass<object>
5455
*
5556
* @throws NotFound
5657
*/
@@ -61,11 +62,12 @@ public function toProvider(string $provider): ReflectionClass
6162
throw new NotFound($provider);
6263
}
6364

64-
if (! (new ReflectionClass($provider))->implementsInterface(ProviderInterface::class)) {
65+
$reflectioClass = new ReflectionClass($provider);
66+
if (! $reflectioClass->implementsInterface(ProviderInterface::class)) {
6567
throw new InvalidProvider($provider);
6668
}
6769

68-
return new ReflectionClass($provider);
70+
return $reflectioClass;
6971
}
7072

7173
private function isNullInterceptorBinding(string $class, string $interface): bool

src/di/DependencyFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Ray\Di;
66

7-
use Ray\ServiceLocator\ServiceLocator;
8-
use ReflectionClass;
7+
use Ray\Aop\ReflectionClass;
98
use ReflectionMethod;
109

1110
use function assert;
@@ -20,7 +19,7 @@ final class DependencyFactory
2019
*/
2120
public function newAnnotatedDependency(ReflectionClass $class): Dependency
2221
{
23-
$annotateClass = new AnnotatedClass(ServiceLocator::getReader());
22+
$annotateClass = new AnnotatedClass();
2423
$newInstance = $annotateClass->getNewInstance($class);
2524
$postConstruct = $annotateClass->getPostConstruct($class);
2625

0 commit comments

Comments
 (0)