Skip to content

GMP operations with operator overloads give false-positive errors #12123

@Firehed

Description

@Firehed

Bug report

PHPStan is incorrectly yielding errors like the following:

  155    Binary operation "**" between GMP and 3 results in an error.
  156    Binary operation "*" between GMP and GMP results in an error.
  157    Binary operation "+" between (array|float|int) and GMP results in an error.
  159    Binary operation "**" between GMP and 2 results in an error.

This has been supported since PHP 5.6, though admittedly the documentation isn't amazing .

I'll check if this can be handled through the existing signature stub file, but I suspect that's not possible due to the engine's operator overloading for the GMP class. I don't have a comprehensive list, but at the very least, the following are supported through operator overloads:

  • gmp_add
  • gmp_sub
  • gmp_mul
  • gmp_div/gmp_div_q
  • gmp_mod

Based on the RFC, I think and, or, xor are also supported, and maybe some of the other bit twiddling functions. gmp_cmp is as well, but there are some other subtleties to its use (=== remains reference equality, which almost certainly is not what you want for how its used)

Code snippet that reproduces the problem

https://phpstan.org/r/803a8194-e77f-47dd-bc68-7efb89014f7d

Expected output

No errors:

https://3v4l.org/WGWLk

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

It remains awesome and I'm looking forward to my elephant arriving!

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