Skip to content

Commit 361efd7

Browse files
authored
Merge pull request #470 from dotkernel/issue-468
Issues #468: Remove `MethodDeprecation` implementation
2 parents e480055 + ea25a46 commit 361efd7

File tree

6 files changed

+2
-245
lines changed

6 files changed

+2
-245
lines changed

src/App/src/Attribute/MethodDeprecation.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/App/src/Handler/PostErrorReportResourceHandler.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Api\App\Handler;
66

7-
use Api\App\Attribute\MethodDeprecation;
87
use Api\App\Exception\BadRequestException;
98
use Api\App\InputFilter\ErrorReportInputFilter;
109
use Api\App\Service\ErrorReportServiceInterface;
@@ -31,11 +30,6 @@ public function __construct(
3130
* @throws BadRequestException
3231
* @throws RuntimeException
3332
*/
34-
#[MethodDeprecation(
35-
sunset: '2038-01-01',
36-
link: 'https://docs.dotkernel.org/api-documentation/v5/core-features/versioning',
37-
deprecationReason: 'Method deprecation example.',
38-
)]
3933
public function handle(ServerRequestInterface $request): ResponseInterface
4034
{
4135
$this->inputFilter->setData((array) $request->getParsedBody());

src/App/src/Middleware/DeprecationMiddleware.php

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,28 @@
44

55
namespace Api\App\Middleware;
66

7-
use Api\App\Attribute\MethodDeprecation;
87
use Api\App\Attribute\ResourceDeprecation;
98
use Api\App\Exception\ConflictException;
109
use Api\App\Exception\RuntimeException;
1110
use Api\App\Service\HandlerService;
12-
use Core\App\Message;
1311
use Dot\DependencyInjection\Attribute\Inject;
1412
use Psr\Http\Message\ResponseInterface;
1513
use Psr\Http\Message\ServerRequestInterface;
1614
use Psr\Http\Server\MiddlewareInterface;
1715
use Psr\Http\Server\RequestHandlerInterface;
1816
use ReflectionClass;
1917
use ReflectionException;
20-
use ReflectionMethod;
2118

22-
use function array_column;
2319
use function array_filter;
24-
use function array_intersect;
2520
use function array_merge;
2621
use function array_values;
27-
use function count;
2822
use function implode;
2923
use function is_string;
3024
use function sprintf;
3125

3226
class DeprecationMiddleware implements MiddlewareInterface
3327
{
3428
public const RESOURCE_DEPRECATION_ATTRIBUTE = ResourceDeprecation::class;
35-
public const METHOD_DEPRECATION_ATTRIBUTE = MethodDeprecation::class;
36-
37-
public const DEPRECATION_ATTRIBUTES = [
38-
self::RESOURCE_DEPRECATION_ATTRIBUTE,
39-
self::METHOD_DEPRECATION_ATTRIBUTE,
40-
];
4129

4230
/**
4331
* @param array<non-empty-string, mixed> $config
@@ -71,7 +59,6 @@ public function process(
7159
return $response;
7260
}
7361

74-
$this->validateAttributes($attributes);
7562
$attribute = $this->getAttribute($attributes);
7663
if (null === $attribute) {
7764
return $response;
@@ -96,23 +83,12 @@ public function process(
9683
*/
9784
private function getAttribute(array $attributes): ?array
9885
{
99-
$attribute = array_values(
86+
return array_values(
10087
array_filter(
10188
$attributes,
10289
fn (array $attribute): bool => $attribute['deprecationType'] === self::RESOURCE_DEPRECATION_ATTRIBUTE
10390
)
10491
)[0] ?? null;
105-
106-
if (null === $attribute) {
107-
$attribute = array_values(
108-
array_filter(
109-
$attributes,
110-
fn (array $attribute): bool => $attribute['deprecationType'] === self::METHOD_DEPRECATION_ATTRIBUTE
111-
)
112-
)[0] ?? null;
113-
}
114-
115-
return $attribute;
11692
}
11793

11894
/**
@@ -122,40 +98,17 @@ private function getAttribute(array $attributes): ?array
12298
private function getReflectionAttributes(ReflectionClass $reflectionObject): array
12399
{
124100
$attributes = [];
101+
125102
foreach ($reflectionObject->getAttributes(self::RESOURCE_DEPRECATION_ATTRIBUTE) as $attribute) {
126103
$attributes[] = array_merge(
127104
($attribute->newInstance())->toArray(),
128105
['identifier' => $reflectionObject->name]
129106
);
130107
}
131108

132-
foreach ($reflectionObject->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) {
133-
foreach ($refMethod->getAttributes(self::METHOD_DEPRECATION_ATTRIBUTE) as $attribute) {
134-
$attributes[] = array_merge(($attribute->newInstance())->toArray(), ['identifier' => $refMethod->name]);
135-
}
136-
}
137-
138109
return $attributes;
139110
}
140111

141-
/**
142-
* @param array<int, mixed> $attributes
143-
* @throws ConflictException
144-
*/
145-
private function validateAttributes(array $attributes): void
146-
{
147-
$intersect = array_intersect(self::DEPRECATION_ATTRIBUTES, array_column($attributes, 'deprecationType'));
148-
if (count($intersect) === count(self::DEPRECATION_ATTRIBUTES)) {
149-
throw ConflictException::create(
150-
sprintf(
151-
Message::RESTRICTION_DEPRECATION,
152-
self::RESOURCE_DEPRECATION_ATTRIBUTE,
153-
self::METHOD_DEPRECATION_ATTRIBUTE
154-
)
155-
);
156-
}
157-
}
158-
159112
/**
160113
* @param non-empty-string $baseLink
161114
* @param array<non-empty-string, mixed> $attribute

src/Core/src/App/src/Message.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class Message
4444
public const RESOURCE_ALREADY_REGISTERED = 'Resource "%s" is already registered.';
4545
public const RESOURCE_NOT_ALLOWED = 'You are not allowed to access this resource.';
4646
public const RESOURCE_NOT_FOUND = '%s not found.';
47-
public const RESTRICTION_DEPRECATION = 'Cannot use both "%s" and "%s" attributes on the same object.';
4847
public const RESTRICTION_IMAGE = 'File must be an image> Accepted mim type(s): %s';
4948
public const RESTRICTION_ROLES = 'At least one role is required.';
5049
public const ROLE_NOT_FOUND = 'Role not found.';
@@ -143,14 +142,6 @@ public static function resourceNotFound(string $resource = 'Resource'): string
143142
return sprintf(self::RESOURCE_NOT_FOUND, $resource);
144143
}
145144

146-
/**
147-
* @return non-empty-string
148-
*/
149-
public static function restrictionDeprecation(string $first, string $second): string
150-
{
151-
return sprintf(self::RESTRICTION_DEPRECATION, $first, $second);
152-
}
153-
154145
/**
155146
* @param string[] $mimeTypes
156147
* @return non-empty-string

test/Unit/App/Attribute/MethodDeprecationTest.php

Lines changed: 0 additions & 128 deletions
This file was deleted.

test/Unit/App/Middleware/DeprecationMiddlewareTest.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace ApiTest\Unit\App\Middleware;
66

7-
use Api\App\Attribute\MethodDeprecation;
87
use Api\App\Attribute\ResourceDeprecation;
98
use Api\App\Exception\ConflictException;
109
use Api\App\Middleware\DeprecationMiddleware;
@@ -50,46 +49,6 @@ protected function setUp(): void
5049
$this->deprecationMiddleware = new DeprecationMiddleware(self::VERSIONING_CONFIG);
5150
}
5251

53-
/**
54-
* @throws ReflectionException
55-
* @throws Exception
56-
*/
57-
public function testThrowsDeprecationConflictException(): void
58-
{
59-
$handler = new #[ResourceDeprecation(
60-
sunset: '2038-01-01',
61-
link: 'test-link',
62-
deprecationReason: 'test-deprecation-reason',
63-
)] class implements RequestHandlerInterface {
64-
#[MethodDeprecation(
65-
sunset: '2038-01-01',
66-
link: 'test-link',
67-
deprecationReason: 'test-deprecation-reason',
68-
)]
69-
public function handle(ServerRequestInterface $request): ResponseInterface
70-
{
71-
return new EmptyResponse();
72-
}
73-
};
74-
75-
$routeResult = $this->createMock(RouteResult::class);
76-
$route = $this->createMock(Route::class);
77-
$lazyLoadingMiddleware = new LazyLoadingMiddleware(
78-
$this->createMock(MiddlewareContainer::class),
79-
$handler::class,
80-
);
81-
82-
$route->method('getMiddleware')->willReturn($lazyLoadingMiddleware);
83-
$routeResult->method('isFailure')->willReturn(false);
84-
$routeResult->method('getMatchedRoute')->willReturn($route);
85-
$this->request->method('getAttribute')->with(RouteResult::class)->willReturn($routeResult);
86-
$this->handler->method('handle')->with($this->request)->willReturn($this->response);
87-
88-
$this->expectException(ConflictException::class);
89-
90-
$this->deprecationMiddleware->process($this->request, $this->handler);
91-
}
92-
9352
/**
9453
* @throws Exception
9554
* @throws ConflictException

0 commit comments

Comments
 (0)