Skip to content
New issue

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

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

Already on GitHub? # to your account

Remove doctrine/annotation #286

Merged
merged 12 commits into from
Sep 15, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
ci:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
with:
old_stable: '["8.0", "8.1"]'
current_stable: 8.2
old_stable: '["8.0", "8.1", "8.2"]'
current_stable: 8.3
script: demo/run.php
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"require": {
"php": "^8.0",
"ext-filter": "*",
"doctrine/annotations": "^1.12",
"justinrainbow/json-schema": "^5.2.10",
"koriym/attributes": "^1.0",
"koriym/http-constants": "^1.1",
Expand Down Expand Up @@ -66,16 +65,16 @@
"files": ["tests/Fake/AppInjector.php"]
},
"scripts" :{
"test": ["./vendor/bin/phpunit"],
"test": ["phpunit"],
"tests": ["@cs", "@test", "@sa"],
"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 --monochrome --show-info=true", "./vendor/bin/phpstan --memory-limit=-1 analyse -c phpstan.neon"],
"metrics": ["./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["./vendor/bin/phpmd --exclude src/Annotation src 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 --monochrome --show-info=true", "phpstan --memory-limit=-1 analyse -c phpstan.neon"],
"metrics": ["phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"phpmd": ["phpmd --exclude src/Annotation src text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
},
"config": {
Expand Down
3 changes: 2 additions & 1 deletion php-require-checker.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"static", "self", "parent",
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object",
"Attribute", "ReflectionAttribute", "Stringable",
"BEAR\\Resource\\HalLink", "BEAR\\Resource\\NullReverseLink", "BEAR\\Resource\\ReverseLinkInterface"
"BEAR\\Resource\\HalLink", "BEAR\\Resource\\NullReverseLink", "BEAR\\Resource\\ReverseLinkInterface",
"Doctrine\\Common\\Annotations\\Reader", "Doctrine\\Common\\Annotations\\AnnotationReader"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*
* Reader-annotation
* Reader-attribute
*
* @deprecated See https://github.com/bearsunday/BEAR.Resource/wiki/Doctrine-annotation-deprecation-notice
*/
final class AnnotationModule extends AbstractModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Ray\Di\AbstractModule;

/** @codeCoverageIgnore */

/**
* @deprecated See https://github.com/bearsunday/BEAR.Resource/wiki/Doctrine-annotation-deprecation-notice
*/
final class AttributeModule extends AbstractModule
{
/**
Expand Down
11 changes: 2 additions & 9 deletions src/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use BEAR\Resource\Annotation\Link as LinkAnnotation;
use BEAR\Resource\Exception\LinkException;
use Doctrine\Common\Annotations\Reader;
use ReflectionMethod;
use Ray\Aop\ReflectionMethod;

use function array_merge;
use function assert;
Expand All @@ -17,11 +16,6 @@

final class Anchor implements AnchorInterface
{
public function __construct(
private Reader $reader,
) {
}

/**
* {@inheritDoc}
*
Expand All @@ -30,8 +24,7 @@ public function __construct(
public function href(string $rel, AbstractRequest $request, array $query): array
{
$classMethod = 'on' . ucfirst($request->method);
/** @var list<object> $annotations */
$annotations = $this->reader->getMethodAnnotations(new ReflectionMethod($request->resourceObject::class, $classMethod));
$annotations = (new ReflectionMethod($request->resourceObject::class, $classMethod))->getAnnotations();
foreach ($annotations as $annotation) {
if ($this->isValidLinkAnnotation($annotation, $rel)) {
assert($annotation instanceof LinkAnnotation);
Expand Down
2 changes: 2 additions & 0 deletions src/AssistedParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ final class AssistedParam implements ParamInterface
{
/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public function __invoke(string $varName, array $query, InjectorInterface $injector): void
{
Expand Down
5 changes: 1 addition & 4 deletions src/EmbedInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use BEAR\Resource\Exception\BadRequestException;
use BEAR\Resource\Exception\EmbedException;
use BEAR\Resource\Exception\LinkException;
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

Expand All @@ -23,7 +22,6 @@ final class EmbedInterceptor implements MethodInterceptor

public function __construct(
ResourceInterface $resource,
private Reader $reader,
) {
$this->resource = clone $resource;
}
Expand All @@ -37,9 +35,8 @@ public function invoke(MethodInvocation $invocation)
{
$ro = $invocation->getThis();
assert($ro instanceof ResourceObject);
$method = $invocation->getMethod();
$query = $this->getArgsByInvocation($invocation);
$embeds = $this->reader->getMethodAnnotations($method);
$embeds = $invocation->getMethod()->getAnnotations();
$this->embedResource($embeds, $ro, $query);

return $invocation->proceed();
Expand Down
7 changes: 2 additions & 5 deletions src/HalRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace BEAR\Resource;

use Doctrine\Common\Annotations\Reader;
use Nocarrier\Hal;
use ReflectionMethod;
use Ray\Aop\ReflectionMethod;
use RuntimeException;

use function assert;
Expand All @@ -28,7 +27,6 @@
final class HalRenderer implements RenderInterface
{
public function __construct(
private Reader $reader,
private HalLinker $linker,
) {
}
Expand All @@ -54,8 +52,7 @@ public function renderHal(ResourceObject $ro): void
[$ro, $body] = $this->valuate($ro);
$method = 'on' . ucfirst($ro->uri->method);
$hasMethod = method_exists($ro, $method);
/** @var list<object> $annotations */
$annotations = $hasMethod ? $this->reader->getMethodAnnotations(new ReflectionMethod($ro, $method)) : [];
$annotations = $hasMethod ? (new ReflectionMethod($ro, $method))->getAnnotations() : [];
$hal = $this->getHal($ro->uri, $body, $annotations);
$json = $hal->asJson(true);
assert(is_string($json));
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/Annotation/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @Annotation
* @Target("METHOD")
* @NamedArgumentConstructor
* @codeCoverageIgnore
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class JsonSchema
Expand Down
6 changes: 2 additions & 4 deletions src/Linker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use BEAR\Resource\Exception\LinkRelException;
use BEAR\Resource\Exception\MethodException;
use BEAR\Resource\Exception\UriException;
use Doctrine\Common\Annotations\Reader;
use ReflectionMethod;
use Ray\Aop\ReflectionMethod;

use function array_filter;
use function array_key_exists;
Expand All @@ -33,7 +32,6 @@ final class Linker implements LinkerInterface
private array $cache = [];

public function __construct(
private Reader $reader,
private InvokerInterface $invoker,
private FactoryInterface $factory,
) {
Expand Down Expand Up @@ -110,7 +108,7 @@ private function annotationLink(LinkType $link, ResourceObject $current, Abstrac

$classMethod = 'on' . ucfirst($request->method);
/** @var list<Link> $annotations */
$annotations = $this->reader->getMethodAnnotations(new ReflectionMethod($current::class, $classMethod));
$annotations = (new ReflectionMethod($current::class, $classMethod))->getAnnotations();
if ($link->type === LinkType::CRAWL_LINK) {
return $this->annotationCrawl($annotations, $link, $current);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Module/ResourceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public function __construct(
protected function configure(): void
{
$this->install(new ResourceClientModule());
$this->install(new AnnotationModule());
$this->install(new EmbedResourceModule());
$this->install(new HttpClientModule());
$this->bind()->annotatedWith(AppName::class)->toInstance($this->appName);

// Backward compatibility
/** @psalm-suppress DeprecatedClass */
$this->install(new AnnotationModule());
}
}
21 changes: 9 additions & 12 deletions src/NamedParamMetas.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@

use BEAR\Resource\Annotation\RequestParamInterface;
use BEAR\Resource\Annotation\ResourceParam;
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Assisted;
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
use ReflectionAttribute;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;

final class NamedParamMetas implements NamedParamMetasInterface
{
public function __construct(
private Reader $reader,
) {
}

/**
* {@inheritDoc}
*/
Expand All @@ -41,7 +35,7 @@ public function __invoke(callable $callable): array
private function getAnnotationParamMetas(ReflectionMethod $method): array
{
$parameters = $method->getParameters();
$annotations = $this->reader->getMethodAnnotations($method);
$annotations = $method->getAnnotations();
$assistedNames = $this->getAssistedNames($annotations);
$webContext = $this->getWebContext($annotations);

Expand Down Expand Up @@ -71,9 +65,8 @@ private function getAttributeParamMetas(ReflectionMethod $method): array
$refWebContext = $parameter->getAttributes(AbstractWebContextParam::class, ReflectionAttribute::IS_INSTANCEOF);
if ($refWebContext) {
$webParam = $refWebContext[0]->newInstance();
/** @psalm-suppress MixedAssignment */
$defaultValue = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null;
$param = new AssistedWebContextParam($webParam, new DefaultParam($defaultValue));
$default = $this->getDefault($parameter);
$param = new AssistedWebContextParam($webParam, $default);
$names[$parameter->name] = $param;
continue;
}
Expand Down Expand Up @@ -103,7 +96,9 @@ private function getAssistedNames(array $annotations): array
continue;
}

$names = $this->setAssistedAnnotation($names, $annotation);
// @codeCoverageIgnoreStart
$names = $this->setAssistedAnnotation($names, $annotation); // BC for annotation
// @codeCoverageIgnoreEnd
}

return $names;
Expand Down Expand Up @@ -132,6 +127,8 @@ private function getWebContext(array $annotations): array
* @param array<string, ParamInterface> $names
*
* @return array<string, ParamInterface>
*
* @codeCoverageIgnore BC for annotation
*/
private function setAssistedAnnotation(array $names, Assisted $assisted): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/NamedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getParameters(callable $callable, array $query): array
$parameters = [];
foreach ($metas as $varName => $param) {
/** @psalm-suppress all */
$parameters[] = $param($varName, $query, $this->injector);
$parameters[$varName] = $param($varName, $query, $this->injector);
}

return $parameters;
Expand Down
2 changes: 1 addition & 1 deletion src/NamedParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface NamedParameterInterface
*
* @param array<string, mixed> $query
*
* @return list<mixed>
* @return array<string, mixed>
*/
public function getParameters(callable $callable, array $query): array;
}
18 changes: 11 additions & 7 deletions src/OptionsMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use BEAR\Resource\Annotation\Embed;
use BEAR\Resource\Annotation\JsonSchema;
use BEAR\Resource\Annotation\Link;
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Di\Named;
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
use Ray\WebContextParam\Annotation\CookieParam;
Expand All @@ -16,7 +16,6 @@
use Ray\WebContextParam\Annotation\FormParam;
use Ray\WebContextParam\Annotation\QueryParam;
use Ray\WebContextParam\Annotation\ServerParam;
use ReflectionMethod;

use function assert;
use function class_exists;
Expand All @@ -41,7 +40,6 @@ final class OptionsMethods
];

public function __construct(
private Reader $reader,
#[Named('json_schema_dir')]
private string $schemaDir = '',
) {
Expand Down Expand Up @@ -82,7 +80,7 @@ public function __invoke(ResourceObject $ro, string $requestMethod): array
private function getMethodExtras(ReflectionMethod $method): array
{
$extras = [];
$annotations = $this->reader->getMethodAnnotations($method);
$annotations = $method->getAnnotations();
foreach ($annotations as $annotation) {
if ($annotation instanceof Link) {
$extras['links'][] = $annotation;
Expand All @@ -102,11 +100,15 @@ private function getMethodExtras(ReflectionMethod $method): array
private function getInMap(ReflectionMethod $method): array
{
$ins = [];
$annotations = $this->reader->getMethodAnnotations($method);
$ins = $this->getInsFromMethodAnnotations($annotations, $ins);
bc_for_annotation: {
// @codeCoverageIgnoreStart
$annotations = $method->getAnnotations();
$ins = $this->getInsFromMethodAnnotations($annotations, $ins);
if ($ins) {
return $ins;
}
// @codeCoverageIgnoreEnd
}

/** @var array<string, string> $insParam */
$insParam = $this->getInsFromParameterAttributes($method, $ins);
Expand All @@ -117,7 +119,7 @@ private function getInMap(ReflectionMethod $method): array
/** @return array<string, mixed> */
private function getJsonSchema(ReflectionMethod $method): array
{
$schema = $this->reader->getMethodAnnotation($method, JsonSchema::class);
$schema = $method->getAnnotation(JsonSchema::class);
if (! $schema instanceof JsonSchema) {
return [];
}
Expand All @@ -135,6 +137,8 @@ private function getJsonSchema(ReflectionMethod $method): array
* @param array<string, string> $ins
*
* @return array<string, string>
*
* @codeCoverageIgnore BC for annotation
*/
public function getInsFromMethodAnnotations(array $annotations, array $ins): array
{
Expand Down
Loading