Skip to content

Level 3 error "Offset -1 does not exist on 'mystring'" when using negative indices on explicitly defined string. #12122

@bshaffer

Description

@bshaffer

Bug report

Negative indices on strings should be allowed. When setting a string explicitly that has length greater than 0, PHPstan wrongfully concludes that "Offset -1 does not exist on 'mystring'". This fails when wrapped in a strlen check as well.

This issue only exists when setting the string (e.g. $mystring = 'mystring';), and does not happen when using a variable string from a function (e.g. function foo(string $mystring) {...} ).

Code snippet that reproduces the problem

https://phpstan.org/r/f482db20-093c-442e-874b-13237efd1e4d

Expected output

PHPStan should not throw an error for negative indices on static strings.

Did PHPStan help you today? Did it make you happy in any way?

PHPstan is fantastic, thank you for all that you do!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions