Skip to content

array shape is not honored when combined with string literals #11006

Closed

Description

@Harrald

Bug report

I find it difficult to describe/explain. I think/hope the example explains the problem a lot better.

On one hand i have an array shape:

/**
 * @param array{
 *     ean?: array<StringOrNullAttributeDto>,
 *     osa_sizes?: array<StringAttributeDto>,
 *     size_uk?: array<StringOrNullAttributeDto>,
 *     size_us?: array<StringOrNullAttributeDto>,
 * } $values
 */

But when filling this array phpstan starts complaining when one of the keys is dynamic.

Code snippet that reproduces the problem

https://phpstan.org/r/39df9dc8-299e-4fc3-9254-3656797dbc0e

Expected output

PHPStan should not detect any issue with the code. But is does.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. ondrejmirtes commented on May 14, 2024

    @ondrejmirtes
    Member

    Yes, using $productParentPayloadDto->SizeAttributeCode as an array key downgrades the array shape to a regular array. Otherwise we'd have to split and maintain multiple array shapes from that point on. It's possible but that's not what PHPStan currently does.

  2. phpstan-bot commented on Nov 1, 2025

    @phpstan-bot
    Contributor

    @Harrald After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.3 (1 error)
    +PHP 8.5 (2 errors)
    +==========
    +
    +53: Possibly invalid array key type string|null.
    +63: Parameter $values of class AkeneoUpdateProductDto constructor expects array{ean?: array<StringOrNullAttributeDto>, osa_sizes?: array<StringAttributeDto>, size_uk?: array<StringOrNullAttributeDto>, size_us?: array<StringOrNullAttributeDto>}, non-empty-array<''|'ean'|'osa_sizes'|'size_uk'|'size_us', array{StringAttributeDto}|array{StringOrNullAttributeDto}> given.
    +
    +PHP 8.0 – 8.4 (1 error)
     ==========
     
     63: Parameter $values of class AkeneoUpdateProductDto constructor expects array{ean?: array<StringOrNullAttributeDto>, osa_sizes?: array<StringAttributeDto>, size_uk?: array<StringOrNullAttributeDto>, size_us?: array<StringOrNullAttributeDto>}, non-empty-array<''|'ean'|'osa_sizes'|'size_uk'|'size_us', array{StringAttributeDto}|array{StringOrNullAttributeDto}> given.
    Full report

    PHP 8.5 (2 errors)

    Line Error
    53 `Possibly invalid array key type string
    63 `Parameter $values of class AkeneoUpdateProductDto constructor expects array{ean?: array, osa_sizes?: array, size_uk?: array, size_us?: array}, non-empty-array<''

    PHP 8.0 – 8.4 (1 error)

    Line Error
    63 `Parameter $values of class AkeneoUpdateProductDto constructor expects array{ean?: array, osa_sizes?: array, size_uk?: array, size_us?: array}, non-empty-array<''

    PHP 7.2 – 7.4 (6 errors)

    Line Error
    6 Promoted properties are supported only on PHP 8.0 and later.
    22 Promoted properties are supported only on PHP 8.0 and later.
    30 Promoted properties are supported only on PHP 8.0 and later.
    38 Promoted properties are supported only on PHP 8.0 and later.
    62 Named arguments are supported only on PHP 8.0 and later.
    63 `Parameter $values of class AkeneoUpdateProductDto constructor expects array{ean?: array, osa_sizes?: array, size_uk?: array, size_us?: array}, non-empty-array<''
  3. phpstan-bot commented on Mar 14, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5193

  4. github-actions commented on Apr 15, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  5. locked as resolved and limited conversation to collaborators on Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions