Skip to content

Cannot access .. on mixed on array #14999

Closed
phpstan/phpstan-src
#5857

Description

@kamil-tekiela

Bug report

After upgrading to the latest version, I noticed that there is a new error which doesn't make sense to me as the variable is an array and is not mixed. I assume it might be because it's global which could have been changed, but in the example linked the global does not change nor could it change. The line before the problem line validates that it is an array.

Code snippet that reproduces the problem

https://phpstan.org/r/78f9c1a9-45f0-4c33-9d62-acc33a6e4617

Expected output

No errors.

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

No response

Activity

  1. added this to the Easy fixes milestone on Jul 27, 2026
  2. VincentLanglet commented on Jul 27, 2026

    @VincentLanglet
    Contributor

    but in the example linked the global does not change nor could it change.

    PHPStan does not goes down into getDisplay implémentation.

    You have multiple fixes

  3. kamil-tekiela commented on Jul 27, 2026

    @kamil-tekiela
    Author

    PHPStan does not goes down into getDisplay implémentation.

    But it doesn't need to go down into getDisplay. The variable cannot be mixed either way. For it to change from array to mixed it would need to change between $_SESSION['Import_message']['message'] and $_SESSION['Import_message']['go_back_url'], i.e. after the call to getDisplay.

  4. removed this from the Easy fixes milestone on Jul 27, 2026
  5. staabm commented on Jul 27, 2026

    @staabm
    Contributor

    ohh I missed that the super-global variable type is invalidated because of the getDisplay-method purity not beeing defined.

    Vincent is correct. PHPStan is pessimistic about the method beeing called could fiddle with the super-global.
    you can use @phpstan-pure as Vincent suggested to tell PHPStan that the method will not change global state.

    another fix would be using a local array and write into the super-global only once:
    https://phpstan.org/r/d9565e4a-dbce-45fa-96f7-a0d3f631eef9

  6. reopened this on Jul 27, 2026
  7. kamil-tekiela commented on Jul 27, 2026

    @kamil-tekiela
    Author

    Vincent is correct. PHPStan is pessimistic about the method beeing called could fiddle with the super-global.
    you can use @phpstan-pure as Vincent suggested to tell PHPStan that the method will not change global state.

    Isn't it a bit too pessimistic though? Or to put it differently, why does it complain about the next line instead of the one that assigns the result of the method call? If the global variable was fiddled with then the assignment $_SESSION['Import_message']['message'] will fail. If this assignment cannot fail, then the next line for sure cannot fail.

    Failing: https://3v4l.org/skmuD#v
    Ok: https://3v4l.org/YTm4I#v

    More accurate examples:
    Failing: https://3v4l.org/MFaSp#v & https://phpstan.org/r/60873521-9ff4-45e3-ba18-42b792592787
    Ok: https://3v4l.org/MjsCr#v & https://phpstan.org/r/0a80a4e5-b356-442b-8340-75a8de044ae5

  8. VincentLanglet commented on Jul 27, 2026

    @VincentLanglet
    Contributor

    Vincent is correct. PHPStan is pessimistic about the method beeing called could fiddle with the super-global.
    you can use @phpstan-pure as Vincent suggested to tell PHPStan that the method will not change global state.

    Isn't it a bit too pessimistic though? Or to put it differently, why does it complain about the next line instead of the one that assigns the result of the method call? If the global variable was fiddled with then the assignment $_SESSION['Import_message']['message'] will fail. If this assignment cannot fail, then the next line for sure cannot fail.

    I agree with this, that's why I didn't immediatly close the issue you might consider reopening it @staabm ?

    Anyway it will be faster/easier for you to rewrite your code @kamil-tekiela for now.

  9. staabm commented on Jul 27, 2026

    @staabm
    Contributor

    global variables can have all types of ugly side-effects we cannot forsee.

    of course we can re-open this issue. @VincentLanglet how would you image a possible improvement/solution for the given scenario?

  10. VincentLanglet commented on Jul 27, 2026

    @VincentLanglet
    Contributor

    of course we can re-open this issue. @VincentLanglet how would you image a possible improvement/solution for the given scenario?

    Technically we should report the error on the line

    $_SESSION['Import_message']['message'] = $message->getDisplay();
    

    and not the following one. (see https://3v4l.org/MFaSp#v)

    $message->getDisplay() seems to invalidate $_SESSION too late actually

  11. reopened this on Jul 27, 2026
  12. staabm commented on Jul 27, 2026

    @staabm
    Contributor

    strictly speaking you are right. I don't think we should spent time on this edge-case though.

  13. github-actions commented on Oct 4, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  14. locked as resolved and limited conversation to collaborators on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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