roave / no-floaters
PHPStan Rules to Disallow Float proliferation in contexts where IEEE-754 rounding errors are not acceptable
Fund package maintenance!
Tidelift
Installs: 176 676
Dependents: 15
Suggesters: 0
Security: 0
Stars: 209
Watchers: 8
Forks: 11
Open Issues: 8
Type:phpstan-extension
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- nikic/php-parser: ^4.17.1
- phpstan/phpstan: ^1.10.47
Requires (Dev)
- doctrine/coding-standard: ^12.0.0
- infection/infection: ^0.27.11
- maglnet/composer-require-checker: ^4.7.1
- phpstan/phpstan-phpunit: ^1.4.1
- phpstan/phpstan-strict-rules: ^1.6.1
- phpunit/phpunit: ^10.5.38
- squizlabs/php_codesniffer: ^3.11.0
- dev-master
- 1.13.x-dev
- 1.12.x-dev
- 1.12.0
- 1.11.x-dev
- 1.11.0
- 1.10.x-dev
- 1.10.0
- 1.9.x-dev
- 1.9.0
- 1.8.x-dev
- 1.8.0
- 1.7.x-dev
- 1.7.0
- 1.6.x-dev
- 1.6.0
- 1.5.x-dev
- 1.5.0
- 1.4.x-dev
- 1.4.0
- 1.3.x-dev
- 1.3.0
- 1.2.x-dev
- 1.2.0
- 1.1.0
- 1.0.0
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/phpstan-phpstan-strict-rules-2.x
- dev-renovate/phpstan-phpstan-phpunit-2.x
- dev-renovate/phpstan-phpstan-2.x
- dev-renovate/nikic-php-parser-5.x
- dev-renovate/all-minor-patch
- dev-renovate/lock-file-maintenance
This package is auto-updated.
Last update: 2024-12-22 03:22:14 UTC
README
This library is a PHPStan plugin that disallows:
- declaration of
float
properties float
method parametersfloat
method return types- assignment of
float
values to variables or properties
The reason for this restriction is that rounding errors coming from floating point arithmetic operations are not acceptable in certain business logic scenario, such as dealing with money, evaluating exam results, rocket science, etc.
An example of such problems can be seen with the following typical example:
var_dump((0.7 + 0.1) === 0.8); // output: bool(false)
This can mean no trouble at all, or a lot of trouble, depending
on how many numbers you are running through your system, so it
is advisable to avoid float
for domains where rounding can
potentially lead to trouble.
float
is still perfectly acceptable in many programming contexts,
and this ruleset should only be applied where it is critical not
to introduce rounding errors.
Installation
composer require --dev roave/no-floaters
Configuration
In your phpstan.neon
configuration, add following section:
includes: - vendor/roave/no-floaters/rules.neon
Optionally, you can configure the library to disallow any
float
-producing expression at all, by adding following to your
phpstan.neon
:
parameters: disallowFloatsEverywhere: true
If the above is enabled, given the following example-file.php
contents:
<?php $a = 1 / 3;
You should get something like following:
vendor/bin/phpstan analyse example-file.php -l 7
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
------ -----------------------------------------------------
Line example-file.php
------ -----------------------------------------------------
3 Cannot assign float to $a - floats are not allowed.
------ -----------------------------------------------------
[ERROR] Found 1 error
roave/no-floaters for enterprise
Available as part of the Tidelift Subscription.
The maintainers of roave/no-floaters and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more..
If you need help with setting up this library in your project, you can contact us at team@roave.com for consulting/support.