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

false PossiblyUndefinedStringArrayOffset for shape. #6463

Closed
azjezz opened this issue Sep 9, 2021 · 4 comments
Closed

false PossiblyUndefinedStringArrayOffset for shape. #6463

azjezz opened this issue Sep 9, 2021 · 4 comments

Comments

@azjezz
Copy link
Contributor

azjezz commented Sep 9, 2021

example: https://psalm.dev/r/d3ce80c00a

In this scenario, T ( and therefor $datum ) is known to contain array{foo: string}

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d3ce80c00a
<?php

/**
 * @template T
 *
 * @param array<array-key, T> $iterable
 * @param (callable(T): bool) $predicate
 *
 * @return list<T>
 */
function filter(array $iterable, callable $predicate): array
{
    return array_values(array_filter(
        $iterable,
        /**
         * @param T $t
         */
        static fn($t): bool => $predicate($t)
    ));
}

$foo = 'CA';

/** @var list<array{foo: string}> $data */
$data = [
    [
        'foo' => 'CA',
    ],
    [
        'foo' => 'CA',
    ],
];

/** @psalm-trace $data */

filter($data, fn (array $datum): bool => /** @psalm-trace $datum */ $datum['foo'] === $foo);
Psalm output (using commit e6e5869):

INFO: PossiblyUndefinedStringArrayOffset - 36:69 - Possibly undefined array offset '"foo"' is risky given expected type 'array-key'. Consider using isset beforehand.

INFO: Trace - 36:69 - $datum: array<array-key, mixed>

INFO: Trace - 36:1 - $data: list<array{foo: string}>

@azjezz
Copy link
Contributor Author

azjezz commented Sep 9, 2021

first reported at: azjezz/psl#226

@weirdan
Copy link
Collaborator

weirdan commented Sep 9, 2021

It's probably possible to constrain the closure signature if it's defined inside a function call and [edit: this is important] there's no references to it in the enclosing scope, but it's not something that we have at the moment, not for calls to user-defined functions anyway.

@orklah
Copy link
Collaborator

orklah commented Jan 30, 2022

This was fixed

@orklah orklah closed this as completed Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants