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

InvalidArrayOffset reported for array_keys() keys #10382

Closed
ostrolucky opened this issue Nov 16, 2023 · 3 comments
Closed

InvalidArrayOffset reported for array_keys() keys #10382

ostrolucky opened this issue Nov 16, 2023 · 3 comments

Comments

@ostrolucky
Copy link
Contributor

Copy link

I found these snippets:

https://psalm.dev/r/6c6ea1b229
<?php

/** @param array<mixed> $config */
function foo (array $config): array {  
    $bar = [];
    foreach (array_keys($config['connections']) as $name) {
        $bar[$name] = 'hmm';
    }
    
    return $bar;
}
Psalm output (using commit 5095f4e):

INFO: PossiblyUndefinedStringArrayOffset - 6:25 - Possibly undefined array offset ''connections'' is risky given expected type 'array-key'. Consider using isset beforehand.

INFO: MixedArgument - 6:25 - Argument 1 of array_keys cannot be mixed, expecting array<array-key, mixed>

ERROR: InvalidArrayOffset - 7:9 - Cannot access value on variable $bar[$name] using a key-of<array<array-key, mixed>> offset, expecting array-key

@weirdan
Copy link
Collaborator

weirdan commented Jan 28, 2024

You need to validate that you pass an array to the array_keys(): https://psalm.dev/r/409ee9c8ec

Copy link

I found these snippets:

https://psalm.dev/r/409ee9c8ec
<?php

/** @param array<mixed> $config */
function foo (array $config): array {  
    $bar = [];
    if (!isset($config['connections']) || !is_array($config['connections'])) {
        throw new RuntimeException;
    }
    foreach (array_keys($config['connections']) as $name) {
        $bar[$name] = 'hmm';
    }
    
    return $bar;
}
Psalm output (using commit 3c90054):

No issues!

ostrolucky added a commit to doctrine/DoctrineBundle that referenced this issue Jan 28, 2024
@ostrolucky ostrolucky closed this as not planned Won't fix, can't repro, duplicate, stale Jan 28, 2024
ostrolucky added a commit to doctrine/DoctrineBundle that referenced this issue Jan 28, 2024
ostrolucky added a commit to doctrine/DoctrineBundle that referenced this issue Jan 28, 2024
ostrolucky added a commit to doctrine/DoctrineBundle that referenced this issue Jan 28, 2024
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

No branches or pull requests

2 participants