phpstan / phpstan

PHP Static Analysis Tool - discover bugs in your code without running it!
https://phpstan.org/
MIT License
12.82k stars 866 forks source link

Trimming a numeric-string might also return a numeric-string #10860

Open Dean151 opened 5 months ago

Dean151 commented 5 months ago

Feature request

There are string functions that we can send numeric string into, that will often return numeric string. Examples:

Given the proper guidance, and assuming this would be accepted, I'd be happy to contribute and open a pull request on phpstan-src.

Thanks for maintaining this awesome software!

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

PHPStan helps me everyday on my personal codebases. And it's also an incredible tool to help me improve the overall quality on the business code of my employer. Thank you so much!

staabm commented 5 months ago

A good start would be to formulate expectations, e.g. in the playground

Write code as you would in your codebase and add assertType assertions to express which types you expect, similar to e.g. https://github.com/phpstan/phpstan-src/blob/1.11.x/tests/PHPStan/Analyser/data/is-numeric.php

That way you can easily see in which situations PHPStan already behaves like you suggest and in which things can/should be improved

Dean151 commented 5 months ago

This is what I came up with in the playground: https://phpstan.org/r/d7b05fe3-f7ef-410b-956d-bee2f2398c22

Not sure to follow yet on the assertType assertions, but I'll take a deeper look on that when I'll be less exhausted 🙃

ondrejmirtes commented 5 months ago

This is a really rare edge case. You can instead convert the string to a number and manipulate it like a number instead of manipulating string like a number.

Dean151 commented 5 months ago

This is a really rare edge case. You can instead convert the string to a number and manipulate it like a number instead of manipulating string like a number.

In fact, this is not possible. Converting a numeric-string to a float will make a floating point approximation in the conversion. And for our use case (matrix multiplication and inversion), we need to keep the calculation precise, even when calculating the abs value. Without this very rare edge case handled, we have to ignore an error, which I don't really like ^^

ondrejmirtes commented 5 months ago

You could use something designed to work with large numbers like GMP https://www.php.net/manual/en/book.gmp.php or BCMath https://www.php.net/manual/en/book.bc.php.

Dean151 commented 5 months ago

You could use something designed to work with large numbers like GMP https://www.php.net/manual/en/book.gmp.php or BCMath https://www.php.net/manual/en/book.bc.php.

That's the whole point. We do use bcmath, a lot. And we do use the numeric-string annotation, a lot. But bcmath does not provide a "babs". This leads to either: use bccomp, then bcmul with -1 to get the absolute. Or quicker: ltrim the minus symbol.

Therefore, I haven't heard about gmp before, I'll look into it, assuming the performances are not worst than bcmath :)