Skip to content

PHPDoc array shape is enforced as closed: an extra key is a compile error #538

Closed

Description

@samdark

A PHPDoc array shape is treated as closed: passing an array with an extra key is a compile error. PHP ignores the docblock, and PHPStan accepts extra keys in an array{...} shape unless it is sealed.

<?php
/** @param list<array{name: string}> $items */
function names(array $items): string { return implode(',', array_column($items, 'name')); }
$items = [['name' => 'a', 'extra' => 'x']];
echo names($items), "\n";

Expected (php): a

Actual (manticore compile shape.php -o shape):

line 5: error: __main(): argument 1 to names() — element array{name:string,extra:string} given, array{name:string} expected: key extra is not in the shape
compile: front-end (parse/typeck/IR) returned null (rc=65)

Real code often passes a richer array to a function documented with fewer keys, so this stops otherwise valid libraries from compiling. Could extra keys be accepted here: dropped, kept in a bag, or reported as a warning? Related, but not the same: #157.

Manticore 0.17.0 (ghcr.io/manticorephp/compiler:latest, image built 2026-10-24, Linux x86_64). PHP reference: 8.5.4.

Activity

  1. added
    bug
    crash
    and removed
    crash
    on Oct 30, 2026
  2. added theissue type on Oct 30, 2026
  3. added
    area:types
    on Oct 30, 2026
  4. blacktrs commented on Oct 30, 2026

    @blacktrs
    Contributor

    I consider this a bug for now. However, it was intentional. Annotations suggest to the compiler that the array shape has a stable structure.
    But I believe the type check could be softened in such particular cases, and a warning could be produced at compile time. I feel we should have a flag to enforce strict shapes during compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:typesbug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions