Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array keys not retained when using splat operator to define class constant #10460

Closed
trowski opened this issue Dec 7, 2023 · 1 comment · Fixed by #10464
Closed

Array keys not retained when using splat operator to define class constant #10460

trowski opened this issue Dec 7, 2023 · 1 comment · Fixed by #10464

Comments

@trowski
Copy link
Contributor

trowski commented Dec 7, 2023

When using the splat operator to define a class constant, Psalm seems to resolve the resulting array as having integer keys instead of retaining the key type of the source array.

https://psalm.dev/r/32157859fc

Copy link

I found these snippets:

https://psalm.dev/r/32157859fc
<?php

class BaseClass {
    public const KEYS = [
        'hostname' => 'host',
        'pass' => 'password',
        'dbname' => 'db',
    ];
}

class ChildClass extends BaseClass {
    public const KEYS = [
        ...parent::KEYS,
        'ssl' => 'sslmode',
   	];
}

/**
 * @param array<non-empty-string, non-empty-string> $keyMap
 */
function example(array $keyMap): void
{
}

example(ChildClass::KEYS);
Psalm output (using commit 0e43c44):

INFO: UnusedParam - 21:24 - Param keyMap is never referenced in this method

ERROR: InvalidArgument - 25:9 - Argument 1 of example expects array<non-empty-string, non-empty-string>, but array{0: 'host', 1: 'password', 2: 'db', ssl: 'sslmode'} provided

ERROR: InvalidConstantAssignmentValue - 12:18 - ChildClass::KEYS with declared type array{0: 'host', 1: 'password', 2: 'db', ssl: 'sslmode'} cannot be assigned type array{dbname: 'db', hostname: 'host', pass: 'password', ssl: 'sslmode'}

INFO: InvalidClassConstantType - 12:18 - The type "array{0: 'host', 1: 'password', 2: 'db', ssl: 'sslmode'}" for ChildClass::KEYS does not satisfy the type "array{dbname: 'db', hostname: 'host', pass: 'password'}" inherited from BaseClass::KEYS

danog added a commit to danog/psalm that referenced this issue Dec 7, 2023
@danog danog mentioned this issue Dec 7, 2023
orklah added a commit that referenced this issue Dec 7, 2023
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 a pull request may close this issue.

1 participant