GithubHelp home page GithubHelp logo

staabm / no-floaters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roave/no-floaters

0.0 2.0 0.0 465 KB

:mag_right: static analysis rules to prevent IEEE-754 floating point errors

License: MIT License

PHP 100.00%

no-floaters's Introduction

roave/no-floaters

roave/no-floaters

Build Status Latest Stable Version

This library is a PHPStan plugin that disallows:

  • declaration of float properties
  • float method parameters
  • float 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);

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

Professional Support

If you need help with setting up this library in your project, you can contact us at [email protected] for consulting/support.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.