Skip to content

Conversation

eliot488995568
Copy link

@eliot488995568 eliot488995568 commented Oct 9, 2025

In src/State/SerializerContextBuilderInterface.php

Phpstan annotation return type need to define some special key or phpstan throw error because he cannot access to offset like 'groups'.
image

https://api-platform.com/docs/core/serialization/#changing-the-serialization-context-dynamically

//  File AbstractAdminContextBuilder.php who implements SerializerContextBuilderInterface
#[\Override]
public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array
{
    $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
    $resourceClass = $context['resource_class'] ?? null;
    $operation = $context['operation'] ?? null;
    $isItemOperation = $operation instanceof Get
        || $operation instanceof Patch
        || $operation instanceof Put;

    if ($resourceClass === $this->getApplicableResourceClass()) {
        if (!isset($context['groups'])) {
            $context['groups'] = [];
        }
        if (is_string($context['groups'])) {
            $context['groups'] = [$context['groups']];
        }
        if ($this->authorizationChecker->isGranted('ROLE_INVITED_USER')) {
            if ($normalization) {
                $context['groups'][] = 'timestamp:read';
                $context['groups'][] = 'blamable:read';
                $context['groups'][] = 'thing:read:user';
                $context['groups'][] = $this->getBaseSerializationGroup().':read:user';
                if ($isItemOperation) {
                    $context['groups'][] = 'thing:single:read:user';
                    $context['groups'][] = $this->getBaseSerializationGroup().':single:read:user';
                }
...

@eliot488995568 eliot488995568 marked this pull request as draft October 10, 2025 07:25
@eliot488995568 eliot488995568 marked this pull request as ready for review October 10, 2025 08:13
@eliot488995568 eliot488995568 changed the title chore(metadata): Expand phpstan-return type annotations chore(metadata): expand phpstan-return type annotations Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant