Skip to content

Commit 1c1023a

Browse files
authored
fix: better generics support for State\ProcessorInterface (#6103)
* fix: better generics support for State\ProcessorInterface
1 parent 227c858 commit 1c1023a

11 files changed

+71
-16
lines changed

src/HttpCache/State/AddHeadersProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818
use ApiPlatform\State\ProcessorInterface;
1919
use Symfony\Component\HttpFoundation\Response;
2020

21+
/**
22+
* @template T1
23+
* @template T2
24+
*
25+
* @implements ProcessorInterface<T1, T2>
26+
*/
2127
final class AddHeadersProcessor implements ProcessorInterface
2228
{
2329
/**
24-
* @param ProcessorInterface<Response>|ProcessorInterface<mixed> $decorated
30+
* @param ProcessorInterface<T1, T2> $decorated
2531
*/
2632
public function __construct(private readonly ProcessorInterface $decorated, private readonly bool $etag = false, private readonly ?int $maxAge = null, private readonly ?int $sharedMaxAge = null, private readonly ?array $vary = null, private readonly ?bool $public = null, private readonly ?int $staleWhileRevalidate = null, private readonly ?int $staleIfError = null)
2733
{

src/Hydra/State/HydraLinkProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
use Symfony\Component\WebLink\GenericLinkProvider;
2323
use Symfony\Component\WebLink\Link;
2424

25+
/**
26+
* @template T1
27+
* @template T2
28+
*
29+
* @implements ProcessorInterface<T1, T2>
30+
*/
2531
final class HydraLinkProcessor implements ProcessorInterface
2632
{
2733
use CorsTrait;
2834

2935
/**
30-
* @param ProcessorInterface<mixed> $decorated
36+
* @param ProcessorInterface<T1, T2> $decorated
3137
*/
3238
public function __construct(private readonly ProcessorInterface $decorated, private readonly UrlGeneratorInterface $urlGenerator)
3339
{

src/State/CallableProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
use ApiPlatform\Metadata\Operation;
1818
use Psr\Container\ContainerInterface;
1919

20+
/**
21+
* @template T1
22+
* @template T2
23+
*
24+
* @implements ProcessorInterface<T1, T2>
25+
*/
2026
final class CallableProcessor implements ProcessorInterface
2127
{
2228
public function __construct(private readonly ?ContainerInterface $locator = null)
@@ -40,7 +46,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4046
throw new RuntimeException(sprintf('Processor "%s" not found on operation "%s"', $processor, $operation->getName()));
4147
}
4248

43-
/** @var ProcessorInterface $processorInstance */
49+
/** @var ProcessorInterface<T1, T2> $processorInstance */
4450
$processorInstance = $this->locator->get($processor);
4551

4652
return $processorInstance->process($data, $operation, $uriVariables, $context);

src/State/Processor/AddLinkHeaderProcessor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@
1818
use Symfony\Component\HttpFoundation\Response;
1919
use Symfony\Component\WebLink\HttpHeaderSerializer;
2020

21+
/**
22+
* @template T1
23+
* @template T2
24+
*
25+
* @implements ProcessorInterface<T1, T2>
26+
*/
2127
final class AddLinkHeaderProcessor implements ProcessorInterface
2228
{
29+
/**
30+
* @param ProcessorInterface<T1, T2> $decorated
31+
*/
2332
public function __construct(private readonly ProcessorInterface $decorated, private readonly ?HttpHeaderSerializer $serializer = new HttpHeaderSerializer())
2433
{
2534
}

src/State/Processor/RespondProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
7474
$status = $operation->getStatus();
7575

7676
if ($sunset = $operation->getSunset()) {
77-
$headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
77+
$headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
7878
}
7979

8080
if ($acceptPatch = $operation->getAcceptPatch()) {

src/State/Processor/SerializeProcessor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@
2626
/**
2727
* Serializes data.
2828
*
29+
* @template T1
30+
* @template T2
31+
*
32+
* @implements ProcessorInterface<T1, T2>
33+
*
2934
* @author Kévin Dunglas <[email protected]>
3035
*/
3136
final class SerializeProcessor implements ProcessorInterface
3237
{
38+
/**
39+
* @param ProcessorInterface<T1, T2> $processor
40+
*/
3341
public function __construct(private readonly ProcessorInterface $processor, private readonly SerializerInterface $serializer, private readonly SerializerContextBuilderInterface $serializerContextBuilder)
3442
{
3543
}

src/State/Processor/WriteProcessor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@
2121
/**
2222
* Bridges persistence and the API system.
2323
*
24+
* @template T1
25+
* @template T2
26+
*
27+
* @implements ProcessorInterface<T1, T2>
28+
*
2429
* @author Kévin Dunglas <[email protected]>
2530
* @author Baptiste Meyer <[email protected]>
2631
*/
2732
final class WriteProcessor implements ProcessorInterface
2833
{
2934
use ClassInfoTrait;
3035

36+
/**
37+
* @param ProcessorInterface<T1, T2> $processor
38+
* @param ProcessorInterface<T1, T2> $callableProcessor
39+
*/
3140
public function __construct(private readonly ProcessorInterface $processor, private readonly ProcessorInterface $callableProcessor)
3241
{
3342
}

src/State/ProcessorInterface.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@
1414
namespace ApiPlatform\State;
1515

1616
use ApiPlatform\Metadata\Operation;
17+
use Symfony\Component\HttpFoundation\Request;
1718

1819
/**
19-
* Process data: send an email, persist to storage, add to queue etc.
20+
* Processes data: sends an email, persists to storage, adds to queue etc.
2021
*
21-
* @template T
22+
* @template T1
23+
* @template T2
2224
*
2325
* @author Antoine Bluchet <[email protected]>
2426
*/
2527
interface ProcessorInterface
2628
{
2729
/**
28-
* Handle the state.
30+
* Handles the state.
2931
*
30-
* @param array<string, mixed> $uriVariables
31-
* @param array<string, mixed>&array{request?: \Symfony\Component\HttpFoundation\Request, previous_data?: mixed, resource_class?: string, original_data?: mixed} $context
32+
* @param T1 $data
33+
* @param array<string, mixed> $uriVariables
34+
* @param array<string, mixed>&array{request?: Request, previous_data?: mixed, resource_class?: string, original_data?: mixed} $context
3235
*
33-
* @return T
36+
* @return T2
3437
*/
3538
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []);
3639
}

src/State/ProviderInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace ApiPlatform\State;
1515

1616
use ApiPlatform\Metadata\Operation;
17+
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
18+
use Symfony\Component\HttpFoundation\Request;
1719

1820
/**
1921
* Retrieves data from a persistence layer.
@@ -27,10 +29,10 @@ interface ProviderInterface
2729
/**
2830
* Provides data.
2931
*
30-
* @param array<string, mixed> $uriVariables
31-
* @param array<string, mixed>|array{request?: \Symfony\Component\HttpFoundation\Request, resource_class?: string} $context
32+
* @param array<string, mixed> $uriVariables
33+
* @param array<string, mixed>|array{request?: Request, resource_class?: string} $context
3234
*
33-
* @return T|Pagination\PartialPaginatorInterface<T>|iterable<T>|null
35+
* @return T|PartialPaginatorInterface<T>|iterable<T>|null
3436
*/
3537
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null;
3638
}

src/Symfony/State/MercureLinkProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
use ApiPlatform\State\ProcessorInterface;
1818
use Symfony\Component\Mercure\Discovery;
1919

20+
/**
21+
* @template T1
22+
* @template T2
23+
*
24+
* @implements ProcessorInterface<T1, T2>
25+
*/
2026
final class MercureLinkProcessor implements ProcessorInterface
2127
{
2228
/**
23-
* @param ProcessorInterface<mixed> $decorated
29+
* @param ProcessorInterface<T1, T2> $decorated
2430
*/
2531
public function __construct(private readonly ProcessorInterface $decorated, private readonly Discovery $discovery)
2632
{

tests/State/RespondProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testRedirectToOperation(): void
7171
return ($args[2] ?? null)?->getUriTemplate() ?? '/default';
7272
});
7373

74-
/** @var ProcessorInterface<Response> $respondProcessor */
74+
/** @var ProcessorInterface<string, Response> $respondProcessor */
7575
$respondProcessor = new RespondProcessor($iriConverter->reveal(), $resourceClassResolver->reveal(), $operationMetadataFactory->reveal());
7676

7777
$response = $respondProcessor->process('content', $canonicalUriTemplateRedirectingOperation, context: [
@@ -103,7 +103,7 @@ public function testAddsExceptionHeaders(): void
103103
{
104104
$operation = new Get();
105105

106-
/** @var ProcessorInterface<Response> $respondProcessor */
106+
/** @var ProcessorInterface<string, Response> $respondProcessor */
107107
$respondProcessor = new RespondProcessor();
108108
$req = new Request();
109109
$req->attributes->set('exception', new TooManyRequestsHttpException(32));

0 commit comments

Comments
 (0)