diff --git a/src/App/src/Attribute/MethodDeprecation.php b/src/App/src/Attribute/MethodDeprecation.php deleted file mode 100644 index 0bb0253..0000000 --- a/src/App/src/Attribute/MethodDeprecation.php +++ /dev/null @@ -1,12 +0,0 @@ -inputFilter->setData((array) $request->getParsedBody()); diff --git a/src/App/src/Middleware/DeprecationMiddleware.php b/src/App/src/Middleware/DeprecationMiddleware.php index 4331211..48710a6 100644 --- a/src/App/src/Middleware/DeprecationMiddleware.php +++ b/src/App/src/Middleware/DeprecationMiddleware.php @@ -4,12 +4,10 @@ namespace Api\App\Middleware; -use Api\App\Attribute\MethodDeprecation; use Api\App\Attribute\ResourceDeprecation; use Api\App\Exception\ConflictException; use Api\App\Exception\RuntimeException; use Api\App\Service\HandlerService; -use Core\App\Message; use Dot\DependencyInjection\Attribute\Inject; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -17,14 +15,10 @@ use Psr\Http\Server\RequestHandlerInterface; use ReflectionClass; use ReflectionException; -use ReflectionMethod; -use function array_column; use function array_filter; -use function array_intersect; use function array_merge; use function array_values; -use function count; use function implode; use function is_string; use function sprintf; @@ -32,12 +26,6 @@ class DeprecationMiddleware implements MiddlewareInterface { public const RESOURCE_DEPRECATION_ATTRIBUTE = ResourceDeprecation::class; - public const METHOD_DEPRECATION_ATTRIBUTE = MethodDeprecation::class; - - public const DEPRECATION_ATTRIBUTES = [ - self::RESOURCE_DEPRECATION_ATTRIBUTE, - self::METHOD_DEPRECATION_ATTRIBUTE, - ]; /** * @param array $config @@ -71,7 +59,6 @@ public function process( return $response; } - $this->validateAttributes($attributes); $attribute = $this->getAttribute($attributes); if (null === $attribute) { return $response; @@ -96,23 +83,12 @@ public function process( */ private function getAttribute(array $attributes): ?array { - $attribute = array_values( + return array_values( array_filter( $attributes, fn (array $attribute): bool => $attribute['deprecationType'] === self::RESOURCE_DEPRECATION_ATTRIBUTE ) )[0] ?? null; - - if (null === $attribute) { - $attribute = array_values( - array_filter( - $attributes, - fn (array $attribute): bool => $attribute['deprecationType'] === self::METHOD_DEPRECATION_ATTRIBUTE - ) - )[0] ?? null; - } - - return $attribute; } /** @@ -122,6 +98,7 @@ private function getAttribute(array $attributes): ?array private function getReflectionAttributes(ReflectionClass $reflectionObject): array { $attributes = []; + foreach ($reflectionObject->getAttributes(self::RESOURCE_DEPRECATION_ATTRIBUTE) as $attribute) { $attributes[] = array_merge( ($attribute->newInstance())->toArray(), @@ -129,33 +106,9 @@ private function getReflectionAttributes(ReflectionClass $reflectionObject): arr ); } - foreach ($reflectionObject->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) { - foreach ($refMethod->getAttributes(self::METHOD_DEPRECATION_ATTRIBUTE) as $attribute) { - $attributes[] = array_merge(($attribute->newInstance())->toArray(), ['identifier' => $refMethod->name]); - } - } - return $attributes; } - /** - * @param array $attributes - * @throws ConflictException - */ - private function validateAttributes(array $attributes): void - { - $intersect = array_intersect(self::DEPRECATION_ATTRIBUTES, array_column($attributes, 'deprecationType')); - if (count($intersect) === count(self::DEPRECATION_ATTRIBUTES)) { - throw ConflictException::create( - sprintf( - Message::RESTRICTION_DEPRECATION, - self::RESOURCE_DEPRECATION_ATTRIBUTE, - self::METHOD_DEPRECATION_ATTRIBUTE - ) - ); - } - } - /** * @param non-empty-string $baseLink * @param array $attribute diff --git a/src/Core/src/App/src/Message.php b/src/Core/src/App/src/Message.php index 1ef9f49..2a46c21 100644 --- a/src/Core/src/App/src/Message.php +++ b/src/Core/src/App/src/Message.php @@ -44,7 +44,6 @@ class Message public const RESOURCE_ALREADY_REGISTERED = 'Resource "%s" is already registered.'; public const RESOURCE_NOT_ALLOWED = 'You are not allowed to access this resource.'; public const RESOURCE_NOT_FOUND = '%s not found.'; - public const RESTRICTION_DEPRECATION = 'Cannot use both "%s" and "%s" attributes on the same object.'; public const RESTRICTION_IMAGE = 'File must be an image> Accepted mim type(s): %s'; public const RESTRICTION_ROLES = 'At least one role is required.'; public const ROLE_NOT_FOUND = 'Role not found.'; @@ -143,14 +142,6 @@ public static function resourceNotFound(string $resource = 'Resource'): string return sprintf(self::RESOURCE_NOT_FOUND, $resource); } - /** - * @return non-empty-string - */ - public static function restrictionDeprecation(string $first, string $second): string - { - return sprintf(self::RESTRICTION_DEPRECATION, $first, $second); - } - /** * @param string[] $mimeTypes * @return non-empty-string diff --git a/src/Core/src/Security/src/Entity/OAuthRefreshToken.php b/src/Core/src/Security/src/Entity/OAuthRefreshToken.php index db0ba61..2345ab9 100644 --- a/src/Core/src/Security/src/Entity/OAuthRefreshToken.php +++ b/src/Core/src/Security/src/Entity/OAuthRefreshToken.php @@ -48,8 +48,6 @@ public function getIdentifier(): string public function setIdentifier(mixed $identifier): self { - $this->setId((int) $identifier); - return $this; } diff --git a/test/Unit/App/Attribute/MethodDeprecationTest.php b/test/Unit/App/Attribute/MethodDeprecationTest.php deleted file mode 100644 index 911b19e..0000000 --- a/test/Unit/App/Attribute/MethodDeprecationTest.php +++ /dev/null @@ -1,128 +0,0 @@ -getAttributes($reflectionClass); - - $this->expectException(SunsetException::class); - - $attributes[0]->newInstance(); - } - - public function testValidDatePassesValidation(): void - { - $class = new class implements RequestHandlerInterface{ - #[MethodDeprecation( - sunset: '2038-01-01', - link: 'test-link', - deprecationReason: 'test-deprecation-reason', - )] - public function test(): void - { - } - - public function handle(ServerRequestInterface $request): ResponseInterface - { - return new JsonResponse('test'); - } - }; - - $reflectionClass = new ReflectionClass($class); - $attributes = $this->getAttributes($reflectionClass); - - $attribute = $attributes[0]->newInstance(); - - $this->assertSame('2038-01-01', $attribute->sunset); - $this->assertSame('test-link', $attribute->link); - $this->assertSame('test-deprecation-reason', $attribute->deprecationReason); - } - - public function testToArray(): void - { - $class = new class implements RequestHandlerInterface{ - #[MethodDeprecation( - sunset: '2038-01-01', - link: 'test-link', - deprecationReason: 'test-deprecation-reason', - rel: 'test-rel', - type: 'test-type', - )] - public function test(): void - { - } - - public function handle(ServerRequestInterface $request): ResponseInterface - { - return new JsonResponse('test'); - } - }; - - $reflectionClass = new ReflectionClass($class); - $attributes = $this->getAttributes($reflectionClass); - - $this->assertNotEmpty($attributes); - $attribute = $attributes[0]->newInstance(); - - $array = $attribute->toArray(); - - $this->assertIsArray($array); - $this->assertNotEmpty($array); - $this->assertArrayHasKey('sunset', $array); - $this->assertArrayHasKey('link', $array); - $this->assertArrayHasKey('rel', $array); - $this->assertArrayHasKey('type', $array); - $this->assertArrayHasKey('deprecationReason', $array); - $this->assertArrayHasKey('deprecationType', $array); - - $this->assertSame('2038-01-01', $array['sunset']); - $this->assertSame('test-rel', $array['rel']); - $this->assertSame('test-type', $array['type']); - $this->assertSame('test-link', $array['link']); - $this->assertSame('test-deprecation-reason', $array['deprecationReason']); - $this->assertSame(DeprecationMiddleware::METHOD_DEPRECATION_ATTRIBUTE, $array['deprecationType']); - } - - /** - * @template T of RequestHandlerInterface - * @param ReflectionClass $reflectionClass - * @return array - */ - private function getAttributes(ReflectionClass $reflectionClass): array - { - $methods = $reflectionClass->getMethods(); - return $methods[0]->getAttributes(MethodDeprecation::class); - } -} diff --git a/test/Unit/App/Middleware/DeprecationMiddlewareTest.php b/test/Unit/App/Middleware/DeprecationMiddlewareTest.php index baa9456..859949e 100644 --- a/test/Unit/App/Middleware/DeprecationMiddlewareTest.php +++ b/test/Unit/App/Middleware/DeprecationMiddlewareTest.php @@ -4,7 +4,6 @@ namespace ApiTest\Unit\App\Middleware; -use Api\App\Attribute\MethodDeprecation; use Api\App\Attribute\ResourceDeprecation; use Api\App\Exception\ConflictException; use Api\App\Middleware\DeprecationMiddleware; @@ -50,46 +49,6 @@ protected function setUp(): void $this->deprecationMiddleware = new DeprecationMiddleware(self::VERSIONING_CONFIG); } - /** - * @throws ReflectionException - * @throws Exception - */ - public function testThrowsDeprecationConflictException(): void - { - $handler = new #[ResourceDeprecation( - sunset: '2038-01-01', - link: 'test-link', - deprecationReason: 'test-deprecation-reason', - )] class implements RequestHandlerInterface { - #[MethodDeprecation( - sunset: '2038-01-01', - link: 'test-link', - deprecationReason: 'test-deprecation-reason', - )] - public function handle(ServerRequestInterface $request): ResponseInterface - { - return new EmptyResponse(); - } - }; - - $routeResult = $this->createMock(RouteResult::class); - $route = $this->createMock(Route::class); - $lazyLoadingMiddleware = new LazyLoadingMiddleware( - $this->createMock(MiddlewareContainer::class), - $handler::class, - ); - - $route->method('getMiddleware')->willReturn($lazyLoadingMiddleware); - $routeResult->method('isFailure')->willReturn(false); - $routeResult->method('getMatchedRoute')->willReturn($route); - $this->request->method('getAttribute')->with(RouteResult::class)->willReturn($routeResult); - $this->handler->method('handle')->with($this->request)->willReturn($this->response); - - $this->expectException(ConflictException::class); - - $this->deprecationMiddleware->process($this->request, $this->handler); - } - /** * @throws Exception * @throws ConflictException