Skip to content

allow_unknown_fields is not taken into account #12

@JeroenBakker

Description

@JeroenBakker

Hi there,

I often parse things such as API responses or other JSON decoded payloads with the allow_unknown_fields parameter set to true.
This allows me to be forwards compatible with any fields that may be added in the future, or lets me assert the rest of a shape conditionally based on other data.

But PHPStan thinks it knows the whole shape of an array with this code:

Type\shape(['foo' => Type\string()], allow_unknown_fields: true)->assert($body);

\PHPStan\dumpType($body); // Dumped type: array{foo: string}

// Call to method Psl\Type\TypeInterface<array<string, string>>::matches() with array{foo: string} will always evaluate to false. 
if (Type\shape(['bar' => Type\string()], allow_unknown_fields: true)->matches($body)) {
    \PHPStan\dumpType($body); // Dumped type: *NEVER* 
}

I would expect a type like array&hasOffsetValue('foo', string) instead.

I tried digging into the code to see if I could offer a PR to add/fix this, but I have no clue how to achieve this 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions