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

Assertion issue with arrays #11248

Open
loevgaard opened this issue Feb 5, 2025 · 6 comments
Open

Assertion issue with arrays #11248

loevgaard opened this issue Feb 5, 2025 · 6 comments

Comments

@loevgaard
Copy link

Here's the code:

https://psalm.dev/r/bc9418bc3e

I would not expect any errors.

Copy link

I found these snippets:

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

class Obj
{
   public function __toString(): string
   {
       return 'obj';
   }
}

function f1(array $context): void
{
    f2($context);
    
    echo (string) $context['obj'];
}

/**
 * @psalm-assert Obj $context['obj']
 */
function f2(array $context): void
{
    if(!isset($context['obj'])) {
        throw new \InvalidArgumentException('Object is not set');
    }
}
Psalm output (using commit d8cbc21):

INFO: PossiblyUndefinedStringArrayOffset - 15:19 - Possibly undefined array offset ''obj'' is risky given expected type 'array-key'. Consider using isset beforehand.

@danog
Copy link
Collaborator

danog commented Feb 5, 2025

There is nothing wrong here, use array shapes to specify an exact shape for parameters.

@danog danog closed this as completed Feb 5, 2025
@loevgaard
Copy link
Author

But I don't want that. I want to assert it. And why isn't there something wrong with my snippet? It doesn't do what I believe everyone would expect

@danog
Copy link
Collaborator

danog commented Feb 5, 2025

Nvm, misread

@danog danog reopened this Feb 5, 2025
@loevgaard
Copy link
Author

Ah, okay :) Thank you

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