Skip to content

Commit

Permalink
Merge pull request #1 from fab2s/v2.x
Browse files Browse the repository at this point in the history
Drop php<8.0
  • Loading branch information
fab2s authored Apr 23, 2024
2 parents 0aedae3 + 0ae4280 commit 1b0ccc9
Show file tree
Hide file tree
Showing 17 changed files with 872 additions and 773 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI
on: [pull_request]
jobs:
tests:
name: Math (PHP ${{ matrix.php-versions }} / Orchestra ${{ matrix.orchestra-versions }})
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.2', '8.1' ]
orchestra-versions: [ '8.0', '9.0' ]
exclude:
- php-versions: 8.1
orchestra-versions: 9.0

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, dom, fileinfo, gmp, bcmath

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Remove composer.lock
run: rm -f composer.lock

- name: Remove Pint
run: composer remove "laravel/pint" --dev --no-update

- name: Install Orchestra ${{ matrix.orchestra-versions }}
run: composer require "orchestra/testbench:^${{ matrix.orchestra-versions }}" --dev --no-update

- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Test with phpunit
run: vendor/bin/phpunit
54 changes: 54 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: QA
on:
push:
branches:
- master
pull_request:
types: [ opened, synchronize ]
jobs:
tests:
name: Math QA
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: mbstring, dom, fileinfo, gmp, bcmath
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Remove composer.lock
run: rm -f composer.lock

- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Check code style
run: vendor/bin/pint --config pint.json --test

- name: Compute Coverage
run: vendor/bin/phpunit --coverage-clover ./coverage.xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./coverage.xml
flags: unittests
name: codecov-math
141 changes: 0 additions & 141 deletions .php_cs

This file was deleted.

15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Math

[![Build Status](https://travis-ci.com/fab2s/Math.svg?branch=master)](https://travis-ci.com/fab2s/Math) [![Total Downloads](https://poser.pugx.org/fab2s/math/downloads)](//packagist.org/packages/fab2s/math) [![Monthly Downloads](https://poser.pugx.org/fab2s/math/d/monthly)](//packagist.org/packages/fab2s/math) [![Latest Stable Version](https://poser.pugx.org/fab2s/math/v/stable)](https://packagist.org/packages/fab2s/math) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fab2s/Math/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fab2s/Math/?branch=master) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) [![License](https://poser.pugx.org/fab2s/math/license)](https://packagist.org/packages/fab2s/math)
[![CI](https://github.com/fab2s/Math/actions/workflows/ci.yml/badge.svg)](https://github.com/fab2s/Math/actions/workflows/ci.yml) [![QA](https://github.com/fab2s/Math/actions/workflows/qa.yml/badge.svg)](https://github.com/fab2s/Math/actions/workflows/qa.yml) [![codecov](https://codecov.io/gh/fab2s/Math/graph/badge.svg?token=6JD33CQLE3)](https://codecov.io/gh/fab2s/Math) [![Total Downloads](https://poser.pugx.org/fab2s/math/downloads)](//packagist.org/packages/fab2s/math) [![Monthly Downloads](https://poser.pugx.org/fab2s/math/d/monthly)](//packagist.org/packages/fab2s/math) [![Latest Stable Version](https://poser.pugx.org/fab2s/math/v/stable)](https://packagist.org/packages/fab2s/math) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) [![License](https://poser.pugx.org/fab2s/math/license)](https://packagist.org/packages/fab2s/math)

A fluent [bcmath](https://php.net/bcmath) based _Helper_ to handle high precision calculus in base 10 with a rather strict approach (want precision for something right?).
It does not try to be smart and just fails without `bcmath`, but it does auto detect [GMP](https://php.net/GMP) for faster base conversions.
Expand All @@ -19,7 +19,7 @@ composer require "fab2s/math"

## Prerequisites

`Math` requires [bcmath](https://php.net/bcmath), [GMP](https://php.net/GMP) is auto detected and used when available for faster base conversions (up to 62).
`Math` requires [bcmath](https://php.net/bcmath), [GMP](https://php.net/GMP) is auto-detected and used when available for faster base conversions (up to 62).

## In practice

Expand Down Expand Up @@ -109,8 +109,8 @@ Doing so is actually faster than casting a pre-existing instance to string becau

Arguments should be string or `Math`, but it is _ok_ to use integers up to `INT_(32|64)`.

**DO NOT** use `floats` as casting them to `string` may result in local dependent format, such as using a coma instead of a dot for decimals or just turn them exponential notation which is not supported by bcmath.
The way floats are handled in general and by PHP in particular is the very the reason why `bcmath` exists, so even if you trust your locale settings, using floats still kinda defeats the purpose of using such lib.
**YOU SHOULD NOT** use `floats` as casting them to `string` may result in local dependent format, such as using a coma instead of a dot for decimals or just turn them exponential notation which is not supported by bcmath.
The way floats are handled in general and by PHP in particular is the very reason why `bcmath` exists, so even if you trust your locale settings, using floats still kinda defeats the purpose of using such lib.

## Internal precision

Expand All @@ -126,9 +126,35 @@ $number = (new Math('100'))->div('3'); // uses precision 18
$number->setPrecision(14); // will use precision 14 for any further calculations
```

## Laravel

For those using [Laravel](https://laravel.com/), `Math` comes with a Laravel caster: [MathCaster](./src/Laravel/MathCast.php) which you can use to directly cast your model properties.

````php
use fab2s\Math\Laravel\MathCast;

class MyModel extends Model
{
protected $casts = [
'not_nullable' => MathCast::class,
'nullable' => MathCast::class . ':nullable',
];
}

$model = new MyModel;

$model->not_nullable = 41;
$model->not_nullable->add(1)->eq(42); // true

$model->not_nullable = null; // throw a NotNullableException

$model->nullabe = null; // is ok

````

## Requirements

`Math` is tested against php 7.1, 7.2, 7.3, 7.4 and 8.0
`Math` is tested against php 8.1 and 8.2. Additionally, MathCast is tested against Laravel 10 and 11.

## Contributing

Expand Down
30 changes: 22 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,47 @@
"name" : "Fabrice de Stefanis"
}],
"keywords" : [
"math",
"PHP",
"Simple",
"Base10",
"decimal",
"BcMath",
"HighPrecision"
"HighPrecision",
"laravel"
],
"license" : [
"MIT"
],
"require" : {
"php": ">=7.1",
"ext-bcmath": "*"
"php": "^8.1",
"ext-bcmath": "*",
"fab2s/context-exception": "^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "~7.0|~8.0"
"phpunit/phpunit": "^10.0",
"laravel/pint": "^1.11",
"orchestra/testbench": "^8.0|^9.0"
},
"autoload": {
"classmap": [
"src"
]
"psr-4": {
"fab2s\\Math\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"fab2s\\Math\\Tests\\": "tests/"
"fab2s\\Math\\Tests\\": "tests"
}
},
"scripts": {
"post-update-cmd": [
"rm -rf .*.cache"
],
"post-install-cmd": [
"rm -rf .*.cache"
],
"fix": "@php vendor/bin/pint --config pint.json"
},
"suggest": {
"ext-gmp": "For faster Math::baseConvert up to base62"
}
Expand Down
Loading

0 comments on commit 1b0ccc9

Please sign in to comment.