Skip to content

Conversation

eliot488995568
Copy link

@eliot488995568 eliot488995568 commented Oct 9, 2025

Q A
Branch? main
Tickets Closes #7471
License MIT

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
Copy link
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was made on purpose.
While psalm support ...<string, mixed>, phpstan doesn't.

The createFromRequest can return anything in the array because the context is initialised with

$context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);

With your changes, doing something like

$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
$context['someCustomKey'];

will report an unsolvable phpstan error "Offset someCustomKey NEVER EXISTS in $context".

While your error "Cannot access offset on mixed" is easily solvable.
Just check the type of $context['group']

if (!isset($context['groups']) || !is_array($context['groups'])) {
     $context['groups'] = [];
}

and so on

@eliot488995568
Copy link
Author

eliot488995568 commented Oct 22, 2025

Thank you @VincentLanglet for your reply, I understand the problem better now. I will do the check in my code.
This PR is therefore more useful now, unless one day phpstan authorises ...<string, mixed>.

@VincentLanglet
Copy link
Contributor

Thank you @VincentLanglet for your reply, I understand the problem better now. I will do the check in my code. This PR is therefore more useful now, unless one day phpstan authorises ...<string, mixed>.

I think that as soon as phpstan support ...<string, mixed> the right PR will be to

  • remove @return array<string, mixed>
  • change the @psalm-return into a simple @return.

Also, I'll add a "phpstan-example" of what error this PR would currently introduce.
https://phpstan.org/r/524380ee-08ef-498c-9ed8-7a7bbc05cca8

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.

2 participants