Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.2', '8.1' ]
orchestra-versions: [ '8.0', '9.0' ]
php-versions: [ '8.4', '8.3', '8.2', '8.1' ]
orchestra-versions: [ '8.0', '9.0', '10.0' ]
exclude:
- php-versions: 8.1
orchestra-versions: 9.0
- php-versions: 8.1
orchestra-versions: 10.0

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -24,10 +26,10 @@ jobs:

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

- name: Cache composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
Expand All @@ -36,8 +38,8 @@ jobs:
- name: Remove composer.lock
run: rm -f composer.lock

- name: Remove Pint
run: composer remove "laravel/pint" --dev --no-update
- name: Remove Qa / bench libs
run: composer remove "laravel/pint" "brick/math" --dev --no-update

- name: Install Orchestra ${{ matrix.orchestra-versions }}
run: composer require "orchestra/testbench:^${{ matrix.orchestra-versions }}" --dev --no-update
Expand All @@ -46,4 +48,4 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Test with phpunit
run: vendor/bin/phpunit
run: composer test
24 changes: 13 additions & 11 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ jobs:

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
php-version: 8.4
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)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
Expand All @@ -39,16 +39,18 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

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

- name: PHPStan src
run: composer stan

- name: PHPStan tests
run: composer stan-tests

- 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 }}
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml
flags: unittests
name: codecov-math
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor
.*.cache
.phpstan
composer.lock
/cov
75 changes: 75 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/),
and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

## [3.0.0] - 2026-02-08

### Changed

- **BREAKING:** `Math` is now immutable by default — every operation returns a new instance, leaving the original unchanged
- `MathImmutable` replaced by `MathMutable` — mutability is now the explicit opt-in for performance-sensitive hot loops
- `__toString()` bypasses redundant regex validation for better performance
- PHPStan level 9 compliance for `src/`
- GitHub Actions workflows updated to latest action versions

### Added

- `MathMutableCast` — Laravel Eloquent cast that returns `MathMutable` instances. Use `MathMutableCast::class` (with optional `:nullable`) instead of `MathCast::class` for mutable cast attributes. Separate cast classes enable proper static type resolution. **Upgrading from v2:** since `Math` is now immutable, existing Laravel models that rely on in-place mutation of cast attributes should switch from `MathCast::class` to `MathMutableCast::class` to restore the previous behavior.
- `MathMutable` — mutable variant of `Math` where operations modify the instance in place. Extends `Math` and is accepted anywhere `Math` is type-hinted.
- `negate()` — flip sign, zero stays zero
- `clamp($min, $max)` — clip value between bounds
- `quotientAndRemainder($divisor)` — returns `[$quotient, $remainder]` in one call
- `isZero()` — precision-aware zero check
- `isNegative()` — complement to `isPositive()`
- `isEven()` / `isOdd()` — integer parity checks, return `false` for non-integers
- `getScale()` — number of meaningful decimal places (normalized)
- `getIntegralPart()` — part before the decimal point (normalized, `-0` becomes `0`)
- `getFractionalPart()` — part after the decimal point (normalized, trailing zeros stripped)
- `declare(strict_types=1)` in all source files
- `phpstan-tests.neon` — dedicated PHPStan configuration for tests at level 5

### Fixed

- `toBase()` / `fromBase()` now preserve the sign of negative numbers instead of silently stripping it — `Math::number('-42')->toBase(16)` returns `'-2a'` and `Math::fromBase('-2a', 16)` returns `'-42'`
- `fromBase()` now normalizes input case for bases <= 36 — `Math::fromBase('FF', 16)` works consistently with and without GMP
- `format()` now works correctly with immutable default (captures `abs()` return value)
- `bcDec2Base()` properly initializes result as `'0'`

### Removed

- `MathImmutable` — no longer needed, `Math` itself is now immutable

## [2.0.0] - 2024-04-24

### Added

- `MathCast` — Laravel Eloquent cast for `Math` instances with nullable support

### Changed

- Minimum PHP version raised to 8.1

### Removed

- PHP < 8.1 support

## [1.0.1] - 2021-06-16

### Added

- PHP 8.0 support

## [1.0.0] - 2019-07-31

Initial release.

[Unreleased]: https://github.com/fab2s/Math/compare/3.0.0...HEAD
[3.0.0]: https://github.com/fab2s/Math/compare/2.0.0...3.0.0
[2.0.0]: https://github.com/fab2s/Math/compare/1.0.1...2.0.0
[1.0.1]: https://github.com/fab2s/Math/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/fab2s/Math/releases/tag/1.0.0
Loading