Skip to content

"Cannot assign offset" error due to variable changing type to union #2232

@ste93cry

Description

@ste93cry

Bug report

I get the error Cannot assign offset 'os' to array|string. and I shouldn't. Removing some preceding if statements makes the error disappear. The variable the assignment is happening on cannot be a string, but for some reason at some point during the parsing its type is changed to an union of string and array types

Code snippet that reproduces the problem

https://phpstan.org/r/2da96b3e-8592-43c3-8b04-c7dc4d45e331

This is the minimal example that I was able to achieve, but it doesn't reproduce the problem so it's pretty useless as is. I'm not able to get it working. However, analysing the Event.php file in getsentry/sentry-php you can see the error. Removing the if statements between lines 591 and 629 fixes the problem

Expected output

I don't expect any error on that line

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions