Skip to content

str_replace considered impure #11884

Closed
phpstan/phpstan-src
#6018

Description

@staabm

Feature request

str_replace could be considered pure, when the by-ref parameter is not passed.

https://phpstan.org/r/6d607859-e782-47bf-8da3-a6ba95bae922
I would expect no error in this snippet.

should also be fixed in str_ireplace, preg_replace while at it - but I guess we will find a solution which is independent of concrete function signatures.

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

finds bugs every day - you know that ;)

Activity

  1. staabm commented on Oct 21, 2024

    @staabm
    ContributorAuthor

    I think to implement that, I would need to add ParameterAcceptor->hasSideEffects(CallLike $callLike): TrinaryLogic

    or adjust ParametersAcceptorSelector::selectFromArgs to return a variant without side-effects for such calls

  2. ondrejmirtes commented on Oct 21, 2024

    @ondrejmirtes
    Member

    I think we'd a new annotation like @phpstan-pure-unless-parameter-passed.

    But again, this is not a pressing issue 😊

  3. xPaw commented on Nov 15, 2024

    @xPaw

    Not gonna make a separate issue, will just leave here: there's some other methods that aren't considered pure but probably can be?

    preg_match, similar_text, sha256

    I also noticed it's reporting hash_hmac call as impure, but says "call to function sha256()" (the first argumented to hash)
    https://phpstan.org/r/ff33c5fa-38b1-4a30-87ef-6653fda81660

  4. ondrejmirtes commented on Nov 15, 2024

    @ondrejmirtes
    Member

    Not gonna make a separate issue

    Make a separate issue please. It doesn't cost anything.

  5. xPaw commented on Nov 15, 2024

    @xPaw

    Do you want a separate issue for each of the methods or what? Created #12048 for hashes.

  6. phpstan-bot commented on Sep 12, 2025

    @phpstan-bot
    Contributor

    @xPaw After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -8: Possibly impure call to function sha256() in pure method HelloWorld::sayHello().
    +No errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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