Skip to content

FN: impure behaviour in explicitly pure functions not reported as an error #4426

@dktapps

Description

@dktapps

Bug report

A method could be mistakenly marked as pure (or intentionally, with the expectation that it would constify it, a la C++).

In such cases, impure behaviour such as:

  • calling an impure function (or method)
  • assigning a field
  • assigning a global variable
  • (maybe) assigning to a reference?

should be considered an error when performed inside a pure function.

Code snippet that reproduces the problem

https://phpstan.org/r/f87df79e-9bd6-45bf-a9be-ef4e1cd8a84d
https://3v4l.org/clKaY

Expected output

An error should be reported on line 13, because a pure method is calling an impure method.

Side notes

I guess there will have to be a differentiation between assumed purity vs explicit purity for this issue to be solved without being super annoying. It would be nice to have an optional rule to check for violation of assumed purity and emit an error to tell the user to mark such methods explicitly with @phpstan-impure for more correct analysis.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions