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
PossiblyInvalidArrayOffset - Psalm wrongly expects string array keys when only int is possible #4479
Comments
I found these snippets: https://psalm.dev/r/62dbf6620f<?php
class OwnershipStatus
{
public const NOT_OWNED = 0;
public const OWNED = 1;
public const WISHED = 2;
}
class BookActivity {
/**
* @psalm-var OwnershipStatus::*
*/
private int $ownershipStatus = OwnershipStatus::NOT_OWNED;
/**
* @psalm-return OwnershipStatus::*
*/
public function getOwnershipStatus(): int
{
return $this->ownershipStatus;
}
}
class Translator
{
private const OWNERSHIP_STATUS_TRANSLATION_KEYS = [
OwnershipStatus::OWNED => 'Activity:OwnBook:OWNED',
OwnershipStatus::WISHED => 'Activity:OwnBook:WISHED'
];
public function getTranslationKey(BookActivity $activity): string
{
return self::OWNERSHIP_STATUS_TRANSLATION_KEYS[$activity->getOwnershipStatus()];
}
}
|
The message is obviously misleading, but the code is not safe either. You're not handling The message should instead read: Cannot access value on variable Translator::OWNERSHIP_STATUS_TRANSLATION_KEYS using offset value of 0, expecting 1 or 2 |
I found these snippets: https://psalm.dev/r/ff55667bb8<?php
class OwnershipStatus
{
public const NOT_OWNED = 0;
public const OWNED = 1;
public const WISHED = 2;
}
class BookActivity {
/**
* @psalm-var OwnershipStatus::*
*/
private int $ownershipStatus = OwnershipStatus::NOT_OWNED;
/**
* @psalm-return OwnershipStatus::*
*/
public function getOwnershipStatus(): int
{
return $this->ownershipStatus;
}
}
class Translator
{
private const OWNERSHIP_STATUS_TRANSLATION_KEYS = [
OwnershipStatus::OWNED => 'Activity:OwnBook:OWNED',
OwnershipStatus::WISHED => 'Activity:OwnBook:WISHED'
];
public function getTranslationKey(BookActivity $activity): string
{
/** @psalm-trace $status */
$status = $activity->getOwnershipStatus();
assert($status !== 0);
/** @psalm-trace $status */;
return self::OWNERSHIP_STATUS_TRANSLATION_KEYS[$status];
}
}
|
@weirdan Indeed, good point! This is definitely a cosmetic issue then. The exception message is very misleading. It should indeed be "using offset value of 0, expecting 1 or 2". Leaving this open as a request to fix the exception message then. By the way, is there a way to declare that a method can accept/return any of these constants, but |
Thanks a lot, @muglug ! |
A snippet is worth a thousand words, so here we go:
https://psalm.dev/r/62dbf6620f
Bug 🐛
As you can see, the
OWNERSHIP_STATUS_TRANSLATION_KEYS
constant can only haveOwnershipStatus::*
keys, soint
only.But Psalm complains:
Not sure where the string type
'1'
or'2'
comes from?Feature request ✨
I'll happily create another issue for this if you wish, but as you can see, Psalm cannot infer the return type of
getTranslationKey()
, even though it looks pretty obvious.The text was updated successfully, but these errors were encountered: