From 0eeb4fe561958a724dd2470d5407c8e0ee1b3a4f Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 15:50:07 +0100 Subject: [PATCH 01/20] use blackbox 6 --- composer.json | 6 +++--- fixtures/Colour.php | 5 +---- tests/ColourTest.php | 2 +- tests/Fixtures/ColourTest.php | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 14521dd..77e90aa 100644 --- a/composer.json +++ b/composer.json @@ -32,16 +32,16 @@ "require-dev": { "phpunit/phpunit": "~10.2", "vimeo/psalm": "~5.0", - "innmind/black-box": "~5.0", + "innmind/black-box": "~6.5", "innmind/coding-standard": "~2.0" }, "conflict": { - "innmind/black-box": "<5.0|~6.0" + "innmind/black-box": "<6.0|~7.0" }, "suggest": { "innmind/black-box": "For property based testing" }, "provide": { - "innmind/black-box-sets": "5.0" + "innmind/black-box-sets": "6.0" } } diff --git a/fixtures/Colour.php b/fixtures/Colour.php index 3318c9b..dc38d38 100644 --- a/fixtures/Colour.php +++ b/fixtures/Colour.php @@ -16,9 +16,6 @@ final class Colour */ public static function any(): Set { - return Set\Decorate::immutable( - static fn($case) => $case->toRGBA(), - Set\Elements::of(...Colours::cases()), - ); + return Set::of(...Colours::cases())->map(static fn($case) => $case->toRGBA()); } } diff --git a/tests/ColourTest.php b/tests/ColourTest.php index 834047f..6466d38 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -39,7 +39,7 @@ public function testOf() public function testReturnNothingForRandomStrings() { $this - ->forAll(Set\Unicode::strings()) + ->forAll(Set::strings()->unicode()) ->then(function($string) { $this->assertNull(Colour::maybe($string)->match( static fn($colour) => $colour, diff --git a/tests/Fixtures/ColourTest.php b/tests/Fixtures/ColourTest.php index fed7de1..438e354 100644 --- a/tests/Fixtures/ColourTest.php +++ b/tests/Fixtures/ColourTest.php @@ -21,7 +21,7 @@ public function testInterface() foreach ($set->values(Random::default) as $value) { $this->assertInstanceOf(Set\Value::class, $value); - $this->assertTrue($value->isImmutable()); + $this->assertTrue($value->immutable()); $this->assertInstanceOf(RGBA::class, $value->unwrap()); } } From 69b8332be300de52f81125e0ca46ca631819fad7 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 15:52:04 +0100 Subject: [PATCH 02/20] fix php 8.4 deprecations --- CHANGELOG.md | 6 ++++++ src/CMYKA.php | 2 +- src/HSLA.php | 2 +- src/RGBA.php | 2 +- tests/CMYKATest.php | 2 +- tests/HSLATest.php | 2 +- tests/RGBATest.php | 2 +- 7 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec6f9b3..0bb3555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed + +- PHP `8.4` deprecations + ## 4.2.0 - 2023-09-16 ### Added diff --git a/src/CMYKA.php b/src/CMYKA.php index 831e513..d18896d 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -28,7 +28,7 @@ public function __construct( Magenta $magenta, Yellow $yellow, Black $black, - Alpha $alpha = null, + ?Alpha $alpha = null, ) { $this->cyan = $cyan; $this->magenta = $magenta; diff --git a/src/HSLA.php b/src/HSLA.php index b7895e7..39b794f 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -26,7 +26,7 @@ public function __construct( Hue $hue, Saturation $saturation, Lightness $lightness, - Alpha $alpha = null, + ?Alpha $alpha = null, ) { $this->hue = $hue; $this->saturation = $saturation; diff --git a/src/RGBA.php b/src/RGBA.php index fbdfd0f..8fc80e1 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -30,7 +30,7 @@ public function __construct( Red $red, Green $green, Blue $blue, - Alpha $alpha = null, + ?Alpha $alpha = null, ) { $this->red = $red; $this->blue = $blue; diff --git a/tests/CMYKATest.php b/tests/CMYKATest.php index 80f8a5e..0f8e54a 100644 --- a/tests/CMYKATest.php +++ b/tests/CMYKATest.php @@ -366,7 +366,7 @@ public function testOf( int $magenta, int $yellow, int $black, - float $alpha = null, + ?float $alpha = null, ) { $cmyka = CMYKA::of($string); diff --git a/tests/HSLATest.php b/tests/HSLATest.php index 290798f..d1a6f8e 100644 --- a/tests/HSLATest.php +++ b/tests/HSLATest.php @@ -262,7 +262,7 @@ public function testOf( int $hue, int $saturation, int $lightness, - float $alpha = null, + ?float $alpha = null, ) { $hsla = HSLA::of($string); diff --git a/tests/RGBATest.php b/tests/RGBATest.php index 3fa5f89..dbe0011 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -277,7 +277,7 @@ public function testFromHexadecimal( int $red, int $green, int $blue, - float $alpha = null, + ?float $alpha = null, ) { $rgba = RGBA::of($string); From 5104e3caeb51e5524ea67dae56cac4236e47fe83 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 15:52:27 +0100 Subject: [PATCH 03/20] CS --- src/Colour.php | 6 +----- tests/CMYKATest.php | 1 - tests/HSLATest.php | 1 - 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Colour.php b/src/Colour.php index 8aa23d0..b2ed022 100644 --- a/src/Colour.php +++ b/src/Colour.php @@ -4,11 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\DomainException; -use Innmind\Immutable\{ - Map, - Str, - Maybe, -}; +use Innmind\Immutable\Maybe; /** * @psalm-immutable diff --git a/tests/CMYKATest.php b/tests/CMYKATest.php index 0f8e54a..2ffd8f9 100644 --- a/tests/CMYKATest.php +++ b/tests/CMYKATest.php @@ -11,7 +11,6 @@ Black, Alpha, RGBA, - Exception\DomainException, }; use PHPUnit\Framework\TestCase; diff --git a/tests/HSLATest.php b/tests/HSLATest.php index d1a6f8e..b2b12b4 100644 --- a/tests/HSLATest.php +++ b/tests/HSLATest.php @@ -11,7 +11,6 @@ Alpha, RGBA, CMYKA, - Exception\DomainException, }; use PHPUnit\Framework\TestCase; From 2cfa05b2f61803cf726c8ecc7c88e95d06d47314 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 15:56:21 +0100 Subject: [PATCH 04/20] use innmind/static-analysis --- composer.json | 2 +- psalm.xml | 3 +++ src/Exception/InvalidValueRangeException.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 77e90aa..9dad0b7 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { "phpunit/phpunit": "~10.2", - "vimeo/psalm": "~5.0", + "innmind/static-analysis": "^1.2.1", "innmind/black-box": "~6.5", "innmind/coding-standard": "~2.0" }, diff --git a/psalm.xml b/psalm.xml index 5d768ff..a4a4d73 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,4 +14,7 @@ + + + diff --git a/src/Exception/InvalidValueRangeException.php b/src/Exception/InvalidValueRangeException.php index 488c150..3e2a383 100644 --- a/src/Exception/InvalidValueRangeException.php +++ b/src/Exception/InvalidValueRangeException.php @@ -3,6 +3,6 @@ namespace Innmind\Colour\Exception; -class InvalidValueRangeException extends DomainException +final class InvalidValueRangeException extends DomainException { } From 6ec3070241f24de28c359f7edc8cec93134e8b7f Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 15:57:26 +0100 Subject: [PATCH 05/20] require php 8.4 --- .github/workflows/ci.yml | 70 ++++------------------------------------ CHANGELOG.md | 4 +++ composer.json | 2 +- 3 files changed, 12 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index feba0f6..779f162 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,68 +3,12 @@ name: CI on: [push, pull_request] jobs: - phpunit: - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-latest, macOS-latest] - php-version: ['8.2', '8.3'] - dependencies: ['lowest', 'highest'] - name: 'PHPUnit' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - coverage: xdebug - - name: Composer - uses: "ramsey/composer-install@v2" - with: - dependency-versions: ${{ matrix.dependencies }} - - name: PHPUnit - run: vendor/bin/phpunit --coverage-clover=coverage.clover - - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} + blackbox: + uses: innmind/github-workflows/.github/workflows/black-box-matrix.yml@next + coverage: + uses: innmind/github-workflows/.github/workflows/coverage-matrix.yml@next + secrets: inherit psalm: - runs-on: ubuntu-latest - strategy: - matrix: - php-version: ['8.2', '8.3'] - dependencies: ['lowest', 'highest'] - name: 'Psalm' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - - name: Composer - uses: "ramsey/composer-install@v2" - with: - dependency-versions: ${{ matrix.dependencies }} - - name: Psalm - run: vendor/bin/psalm --shepherd + uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@next cs: - runs-on: ubuntu-latest - strategy: - matrix: - php-version: ['8.2'] - name: 'CS' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - - name: Composer - uses: "ramsey/composer-install@v2" - - name: CS - run: vendor/bin/php-cs-fixer fix --diff --dry-run + uses: innmind/github-workflows/.github/workflows/cs.yml@next diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb3555..53645b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Changed + +- Requires PHP `8.4` + ### Fixed - PHP `8.4` deprecations diff --git a/composer.json b/composer.json index 9dad0b7..852ffc4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "issues": "http://github.com/Innmind/Colour/issues" }, "require": { - "php": "~8.2", + "php": "~8.4", "innmind/immutable": "~4.0|~5.0" }, "autoload": { From c6e7aee731ae5f8a2e5556c3c15b34175a1c3b1a Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:07:51 +0100 Subject: [PATCH 06/20] replace phpunit by blackbox --- .gitattributes | 1 - .gitignore | 1 - blackbox.php | 28 ++++++++++++++++++++++++++++ composer.json | 1 - phpunit.xml.dist | 22 ---------------------- tests/AlphaTest.php | 10 +++++----- tests/BlackTest.php | 2 +- tests/BlueTest.php | 2 +- tests/CMYKATest.php | 18 +++++++----------- tests/ColourTest.php | 7 +++---- tests/CyanTest.php | 2 +- tests/Fixtures/ColourTest.php | 2 +- tests/GreenTest.php | 2 +- tests/HSLATest.php | 15 +++++---------- tests/HueTest.php | 7 +++---- tests/IntensityTest.php | 2 +- tests/LightnessTest.php | 2 +- tests/MagentaTest.php | 2 +- tests/RGBATest.php | 19 ++++++------------- tests/RedTest.php | 2 +- tests/SaturationTest.php | 2 +- tests/YellowTest.php | 2 +- 22 files changed, 68 insertions(+), 83 deletions(-) create mode 100644 blackbox.php delete mode 100644 phpunit.xml.dist diff --git a/.gitattributes b/.gitattributes index 4160131..05f17e3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,3 @@ /.gitattributes export-ignore /.gitignore export-ignore -/phpunit.xml.dist export-ignore /tests export-ignore diff --git a/.gitignore b/.gitignore index e96516b..987e2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ composer.lock vendor -.phpunit.result.cache diff --git a/blackbox.php b/blackbox.php new file mode 100644 index 0000000..31365e1 --- /dev/null +++ b/blackbox.php @@ -0,0 +1,28 @@ +when( + \getenv('ENABLE_COVERAGE') !== false, + static fn(Application $app) => $app + ->codeCoverage( + CodeCoverage::of( + __DIR__.'/src/', + __DIR__.'/tests/', + __DIR__.'/fixtures/', + ) + ->dumpTo('coverage.clover') + ->enableWhen(true), + ) + ->scenariiPerProof(50), + ) + ->tryToProve(Load::directory(__DIR__.'/tests/')) + ->exit(); diff --git a/composer.json b/composer.json index 852ffc4..1b0c472 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ } }, "require-dev": { - "phpunit/phpunit": "~10.2", "innmind/static-analysis": "^1.2.1", "innmind/black-box": "~6.5", "innmind/coding-standard": "~2.0" diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 01eb7ae..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - ./tests - - - - - . - - - ./tests - ./vendor - - - diff --git a/tests/AlphaTest.php b/tests/AlphaTest.php index f4fd246..3bacc3a 100644 --- a/tests/AlphaTest.php +++ b/tests/AlphaTest.php @@ -7,7 +7,8 @@ Alpha, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class AlphaTest extends TestCase { @@ -37,7 +38,8 @@ public function testSub() $alpha = (new Alpha(0.54))->subtract(new Alpha(0.12)); $this->assertInstanceOf(Alpha::class, $alpha); - $this->assertEqualsWithDelta(0.42, $alpha->toFloat(), 0.001); + $this->assertGreaterThanOrEqual(0.4199, $alpha->toFloat()); + $this->assertLessThanOrEqual(0.421, $alpha->toFloat()); $this->assertSame( 0.0, @@ -75,9 +77,7 @@ public function testAtMinimum() $this->assertFalse((new Alpha(0.5))->atMinimum()); } - /** - * @dataProvider hexadecimals - */ + #[DataProvider('hexadecimals')] public function testHexadecimal($hex, $percent) { $this->assertSame( diff --git a/tests/BlackTest.php b/tests/BlackTest.php index c78d5cc..2780d13 100644 --- a/tests/BlackTest.php +++ b/tests/BlackTest.php @@ -7,7 +7,7 @@ Black, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BlackTest extends TestCase { diff --git a/tests/BlueTest.php b/tests/BlueTest.php index a31322b..212673a 100644 --- a/tests/BlueTest.php +++ b/tests/BlueTest.php @@ -8,7 +8,7 @@ Intensity, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BlueTest extends TestCase { diff --git a/tests/CMYKATest.php b/tests/CMYKATest.php index 2ffd8f9..4e00d79 100644 --- a/tests/CMYKATest.php +++ b/tests/CMYKATest.php @@ -12,7 +12,8 @@ Alpha, RGBA, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class CMYKATest extends TestCase { @@ -292,12 +293,11 @@ public function testSubtractAlpha() $this->assertSame(20, $cmyk2->magenta()->toInt()); $this->assertSame(30, $cmyk2->yellow()->toInt()); $this->assertSame(40, $cmyk2->black()->toInt()); - $this->assertEqualsWithDelta(0.1, $cmyk2->alpha()->toFloat(), 0.01); + $this->assertGreaterThanOrEqual(0.09, $cmyk2->alpha()->toFloat()); + $this->assertLessThanOrEqual(0.11, $cmyk2->alpha()->toFloat()); } - /** - * @dataProvider withAlpha - */ + #[DataProvider('withAlpha')] public function testWithAlpha( string $string, int $cyan, @@ -328,9 +328,7 @@ public static function withAlpha() ]; } - /** - * @dataProvider withoutAlpha - */ + #[DataProvider('withoutAlpha')] public function testWithoutAlpha( string $string, int $cyan, @@ -356,9 +354,7 @@ public static function withoutAlpha() ]; } - /** - * @dataProvider colours - */ + #[DataProvider('colours')] public function testOf( string $string, int $cyan, diff --git a/tests/ColourTest.php b/tests/ColourTest.php index 6466d38..9df14d7 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -10,11 +10,12 @@ CMYKA, Exception\DomainException, }; -use PHPUnit\Framework\TestCase; use Innmind\BlackBox\{ + PHPUnit\Framework\TestCase, PHPUnit\BlackBox, Set, }; +use PHPUnit\Framework\Attributes\DataProvider; class ColourTest extends TestCase { @@ -61,9 +62,7 @@ public function testLiterals() $this->assertCount(98, Colour::cases()); } - /** - * @dataProvider literals - */ + #[DataProvider('literals')] public function testFromLiteral(Colour|RGBA $colour, string $hex) { $rgba = $colour->toRGBA(); diff --git a/tests/CyanTest.php b/tests/CyanTest.php index 2539c35..0ffa7df 100644 --- a/tests/CyanTest.php +++ b/tests/CyanTest.php @@ -7,7 +7,7 @@ Cyan, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CyanTest extends TestCase { diff --git a/tests/Fixtures/ColourTest.php b/tests/Fixtures/ColourTest.php index 438e354..ff3d2ec 100644 --- a/tests/Fixtures/ColourTest.php +++ b/tests/Fixtures/ColourTest.php @@ -6,10 +6,10 @@ use Fixtures\Innmind\Colour\Colour; use Innmind\Colour\RGBA; use Innmind\BlackBox\{ + PHPUnit\Framework\TestCase, Set, Random, }; -use PHPUnit\Framework\TestCase; class ColourTest extends TestCase { diff --git a/tests/GreenTest.php b/tests/GreenTest.php index fc310ac..1cf6284 100644 --- a/tests/GreenTest.php +++ b/tests/GreenTest.php @@ -8,7 +8,7 @@ Intensity, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class GreenTest extends TestCase { diff --git a/tests/HSLATest.php b/tests/HSLATest.php index b2b12b4..db6c37d 100644 --- a/tests/HSLATest.php +++ b/tests/HSLATest.php @@ -12,7 +12,8 @@ RGBA, CMYKA, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class HSLATest extends TestCase { @@ -196,9 +197,7 @@ public function testSubtractAlpha() $this->assertSame(0.7, $hsl2->alpha()->toFloat()); } - /** - * @dataProvider withAlpha - */ + #[DataProvider('withAlpha')] public function testWithAlpha( string $string, int $hue, @@ -227,9 +226,7 @@ public static function withAlpha() ]; } - /** - * @dataProvider withoutAlpha - */ + #[DataProvider('withoutAlpha')] public function testWithoutAlpha( string $string, int $hue, @@ -253,9 +250,7 @@ public static function withoutAlpha() ]; } - /** - * @dataProvider colours - */ + #[DataProvider('colours')] public function testOf( string $string, int $hue, diff --git a/tests/HueTest.php b/tests/HueTest.php index 25bb368..7510fab 100644 --- a/tests/HueTest.php +++ b/tests/HueTest.php @@ -7,7 +7,8 @@ Hue, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class HueTest extends TestCase { @@ -35,9 +36,7 @@ public function testThrowWhenValueTooHigh() new Hue(360); } - /** - * @dataProvider rotations - */ + #[DataProvider('rotations')] public function testRotateBy($initial, $degrees, $expected) { $hue = (new Hue($initial))->rotateBy($degrees); diff --git a/tests/IntensityTest.php b/tests/IntensityTest.php index b1c641c..a65a889 100644 --- a/tests/IntensityTest.php +++ b/tests/IntensityTest.php @@ -7,7 +7,7 @@ Intensity, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntensityTest extends TestCase { diff --git a/tests/LightnessTest.php b/tests/LightnessTest.php index eba5e92..726edfd 100644 --- a/tests/LightnessTest.php +++ b/tests/LightnessTest.php @@ -7,7 +7,7 @@ Lightness, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LightnessTest extends TestCase { diff --git a/tests/MagentaTest.php b/tests/MagentaTest.php index 9f1c502..27540c9 100644 --- a/tests/MagentaTest.php +++ b/tests/MagentaTest.php @@ -7,7 +7,7 @@ Magenta, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MagentaTest extends TestCase { diff --git a/tests/RGBATest.php b/tests/RGBATest.php index dbe0011..692c781 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -13,7 +13,8 @@ CMYKA, Exception\DomainException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class RGBATest extends TestCase { @@ -209,9 +210,7 @@ public function testSubtractGreen() $this->assertSame(0.5, $rgba2->alpha()->toFloat()); } - /** - * @dataProvider hexadecimalWithAlpha - */ + #[DataProvider('hexadecimalWithAlpha')] public function testFromHexadecimalWithAlpha( string $string, int $red, @@ -238,9 +237,7 @@ public static function hexadecimalWithAlpha() ]; } - /** - * @dataProvider hexadecimalWithoutAlpha - */ + #[DataProvider('hexadecimalWithoutAlpha')] public function testFromHexadecimalWithoutAlpha( string $string, int $red, @@ -269,9 +266,7 @@ public static function hexadecimalWithoutAlpha() ]; } - /** - * @dataProvider hexadecimals - */ + #[DataProvider('hexadecimals')] public function testFromHexadecimal( string $string, int $red, @@ -416,9 +411,7 @@ public function testFromRGBAFunction() $this->assertTrue($rgb->alpha()->atMaximum()); } - /** - * @dataProvider allFormats - */ + #[DataProvider('allFormats')] public function testOf( string $colour, int $red, diff --git a/tests/RedTest.php b/tests/RedTest.php index 1fd0580..d5347ed 100644 --- a/tests/RedTest.php +++ b/tests/RedTest.php @@ -8,7 +8,7 @@ Intensity, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RedTest extends TestCase { diff --git a/tests/SaturationTest.php b/tests/SaturationTest.php index 97d5b58..0b1fba1 100644 --- a/tests/SaturationTest.php +++ b/tests/SaturationTest.php @@ -7,7 +7,7 @@ Saturation, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SaturationTest extends TestCase { diff --git a/tests/YellowTest.php b/tests/YellowTest.php index c47b479..70c828d 100644 --- a/tests/YellowTest.php +++ b/tests/YellowTest.php @@ -7,7 +7,7 @@ Yellow, Exception\InvalidValueRangeException, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class YellowTest extends TestCase { From 296219c69bfc826caa006ad27f1a7abc45321e2e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:08:35 +0100 Subject: [PATCH 07/20] update dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1b0c472..8d70bf5 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require": { "php": "~8.4", - "innmind/immutable": "~4.0|~5.0" + "innmind/immutable": "dev-next" }, "autoload": { "psr-4": { From 4f895a437f37568de8ef5497219664b0da3f56ec Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:31:36 +0100 Subject: [PATCH 08/20] use Attempts to better track errors --- CHANGELOG.md | 1 + src/Alpha.php | 17 ++-- src/Black.php | 13 +-- src/Blue.php | 21 ++-- src/CMYKA.php | 86 +++++++++------- src/Colour.php | 26 +++-- src/Cyan.php | 13 +-- src/Green.php | 21 ++-- src/HSLA.php | 78 ++++++++------- src/Hue.php | 13 +-- src/Intensity.php | 13 +-- src/Lightness.php | 13 +-- src/Magenta.php | 13 +-- src/RGBA.php | 238 ++++++++++++++++++++++++--------------------- src/Red.php | 21 ++-- src/Saturation.php | 13 +-- src/Yellow.php | 13 +-- 17 files changed, 304 insertions(+), 309 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53645b4..0344e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - Requires PHP `8.4` +- Named constructors `::of()`, `::fromHexadecimal()` and `::fromIntensity()` now return instances of `Innmind\Immutable\Attempt` ### Fixed diff --git a/src/Alpha.php b/src/Alpha.php index 51206ca..3097c91 100644 --- a/src/Alpha.php +++ b/src/Alpha.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,24 +28,19 @@ public function __construct(float $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(float $value): Maybe + public static function of(float $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromHexadecimal(string $hex): Maybe + public static function fromHexadecimal(string $hex): Attempt { if (\mb_strlen($hex) === 1) { $hex .= $hex; diff --git a/src/Black.php b/src/Black.php index 69fbafa..9ff6ca0 100644 --- a/src/Black.php +++ b/src/Black.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $black): self diff --git a/src/Blue.php b/src/Blue.php index 848db4e..01dd2f2 100644 --- a/src/Blue.php +++ b/src/Blue.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,24 +28,19 @@ public function __construct(int $integer) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromHexadecimal(string $hex): Maybe + public static function fromHexadecimal(string $hex): Attempt { if (\mb_strlen($hex) === 1) { $hex .= $hex; @@ -55,9 +50,9 @@ public static function fromHexadecimal(string $hex): Maybe } /** - * @return Maybe + * @return Attempt */ - public static function fromIntensity(Intensity $intensity): Maybe + public static function fromIntensity(Intensity $intensity): Attempt { return self::of( (int) \round((255 * $intensity->toInt()) / 100), diff --git a/src/CMYKA.php b/src/CMYKA.php index d18896d..d53fda4 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -7,6 +7,7 @@ use Innmind\Immutable\{ Str, Maybe, + Attempt, }; /** @@ -44,10 +45,7 @@ public function __construct( */ public static function of(string $colour): self { - return self::maybe($colour)->match( - static fn($self) => $self, - static fn() => throw new DomainException($colour), - ); + return self::attempt($colour)->unwrap(); } /** @@ -56,10 +54,20 @@ public static function of(string $colour): self * @return Maybe */ public static function maybe(string $colour): Maybe + { + return self::attempt($colour)->maybe(); + } + + /** + * @psalm-pure + * + * @return Attempt + */ + public static function attempt(string $colour): Attempt { $colour = Str::of($colour)->trim(); - return self::withAlpha($colour)->otherwise( + return self::withAlpha($colour)->recover( static fn() => self::withoutAlpha($colour), ); } @@ -262,9 +270,9 @@ public function toString(): string /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function withAlpha(Str $colour): Maybe + private static function withAlpha(Str $colour): Attempt { $matches = $colour ->capture(self::PATTERN_WITH_ALPHA) @@ -273,45 +281,47 @@ private static function withAlpha(Str $colour): Maybe ->get('cyan') ->filter(static fn($cyan) => \is_numeric($cyan)) ->map(static fn($cyan) => (int) $cyan) - ->flatMap(static fn($cyan) => Cyan::of($cyan)); + ->flatMap(static fn($cyan) => Cyan::of($cyan)->maybe()); $magenta = $matches ->get('magenta') ->filter(static fn($magenta) => \is_numeric($magenta)) ->map(static fn($magenta) => (int) $magenta) - ->flatMap(static fn($magenta) => Magenta::of($magenta)); + ->flatMap(static fn($magenta) => Magenta::of($magenta)->maybe()); $yellow = $matches ->get('yellow') ->filter(static fn($yellow) => \is_numeric($yellow)) ->map(static fn($yellow) => (int) $yellow) - ->flatMap(static fn($yellow) => Yellow::of($yellow)); + ->flatMap(static fn($yellow) => Yellow::of($yellow)->maybe()); $black = $matches ->get('black') ->filter(static fn($black) => \is_numeric($black)) ->map(static fn($black) => (int) $black) - ->flatMap(static fn($black) => Black::of($black)); + ->flatMap(static fn($black) => Black::of($black)->maybe()); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); - - return Maybe::all($cyan, $magenta, $yellow, $black, $alpha)->map( - static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black, Alpha $alpha) => new self( - $cyan, - $magenta, - $yellow, - $black, - $alpha, - ), - ); + ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); + + return Maybe::all($cyan, $magenta, $yellow, $black, $alpha) + ->map( + static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black, Alpha $alpha) => new self( + $cyan, + $magenta, + $yellow, + $black, + $alpha, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function withoutAlpha(Str $colour): Maybe + private static function withoutAlpha(Str $colour): Attempt { $matches = $colour ->capture(self::PATTERN_WITHOUT_ALPHA) @@ -320,30 +330,32 @@ private static function withoutAlpha(Str $colour): Maybe ->get('cyan') ->filter(static fn($cyan) => \is_numeric($cyan)) ->map(static fn($cyan) => (int) $cyan) - ->flatMap(static fn($cyan) => Cyan::of($cyan)); + ->flatMap(static fn($cyan) => Cyan::of($cyan)->maybe()); $magenta = $matches ->get('magenta') ->filter(static fn($magenta) => \is_numeric($magenta)) ->map(static fn($magenta) => (int) $magenta) - ->flatMap(static fn($magenta) => Magenta::of($magenta)); + ->flatMap(static fn($magenta) => Magenta::of($magenta)->maybe()); $yellow = $matches ->get('yellow') ->filter(static fn($yellow) => \is_numeric($yellow)) ->map(static fn($yellow) => (int) $yellow) - ->flatMap(static fn($yellow) => Yellow::of($yellow)); + ->flatMap(static fn($yellow) => Yellow::of($yellow)->maybe()); $black = $matches ->get('black') ->filter(static fn($black) => \is_numeric($black)) ->map(static fn($black) => (int) $black) - ->flatMap(static fn($black) => Black::of($black)); - - return Maybe::all($cyan, $magenta, $yellow, $black)->map( - static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black) => new self( - $cyan, - $magenta, - $yellow, - $black, - ), - ); + ->flatMap(static fn($black) => Black::of($black)->maybe()); + + return Maybe::all($cyan, $magenta, $yellow, $black) + ->map( + static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black) => new self( + $cyan, + $magenta, + $yellow, + $black, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Colour.php b/src/Colour.php index b2ed022..5bdefdf 100644 --- a/src/Colour.php +++ b/src/Colour.php @@ -4,7 +4,10 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\DomainException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\{ + Maybe, + Attempt, +}; /** * @psalm-immutable @@ -119,10 +122,7 @@ enum Colour */ public static function of(string $colour): RGBA|HSLA|CMYKA { - return self::maybe($colour)->match( - static fn($colour) => $colour, - static fn() => throw new DomainException($colour), - ); + return self::attempt($colour)->unwrap(); } /** @@ -132,9 +132,19 @@ public static function of(string $colour): RGBA|HSLA|CMYKA */ public static function maybe(string $colour): Maybe { - return RGBA::maybe($colour) - ->otherwise(static fn() => HSLA::maybe($colour)) - ->otherwise(static fn() => CMYKA::maybe($colour)); + return self::attempt($colour)->maybe(); + } + + /** + * @psalm-pure + * + * @return Attempt + */ + public static function attempt(string $colour): Attempt + { + return RGBA::attempt($colour) + ->recover(static fn() => HSLA::attempt($colour)) + ->recover(static fn() => CMYKA::attempt($colour)); } /** diff --git a/src/Cyan.php b/src/Cyan.php index a9654b9..b6c6e46 100644 --- a/src/Cyan.php +++ b/src/Cyan.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $cyan): self diff --git a/src/Green.php b/src/Green.php index e25519e..7ba7e2d 100644 --- a/src/Green.php +++ b/src/Green.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,24 +28,19 @@ public function __construct(int $integer) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromHexadecimal(string $hex): Maybe + public static function fromHexadecimal(string $hex): Attempt { if (\mb_strlen($hex) === 1) { $hex .= $hex; @@ -57,9 +52,9 @@ public static function fromHexadecimal(string $hex): Maybe /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromIntensity(Intensity $intensity): Maybe + public static function fromIntensity(Intensity $intensity): Attempt { return self::of( (int) \round((255 * $intensity->toInt()) / 100), diff --git a/src/HSLA.php b/src/HSLA.php index 39b794f..b732c52 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -7,6 +7,7 @@ use Innmind\Immutable\{ Str, Maybe, + Attempt, }; /** @@ -41,10 +42,7 @@ public function __construct( */ public static function of(string $colour): self { - return self::maybe($colour)->match( - static fn($self) => $self, - static fn() => throw new DomainException($colour), - ); + return self::attempt($colour)->unwrap(); } /** @@ -53,10 +51,20 @@ public static function of(string $colour): self * @return Maybe */ public static function maybe(string $colour): Maybe + { + return self::attempt($colour)->maybe(); + } + + /** + * @psalm-pure + * + * @return Attempt + */ + public static function attempt(string $colour): Attempt { $colour = Str::of($colour)->trim(); - return self::withAlpha($colour)->otherwise( + return self::withAlpha($colour)->recover( static fn() => self::withoutAlpha($colour), ); } @@ -251,9 +259,9 @@ private function hueToPoint(float $p, float $q, float $t): float /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function withAlpha(Str $colour): Maybe + private static function withAlpha(Str $colour): Attempt { $matches = $colour ->capture(self::PATTERN_WITH_ALPHA) @@ -262,39 +270,41 @@ private static function withAlpha(Str $colour): Maybe ->get('hue') ->filter(static fn($hue) => \is_numeric($hue)) ->map(static fn($hue) => (int) $hue) - ->flatMap(static fn($hue) => Hue::of($hue)); + ->flatMap(static fn($hue) => Hue::of($hue)->maybe()); $saturation = $matches ->get('saturation') ->filter(static fn($saturation) => \is_numeric($saturation)) ->map(static fn($saturation) => (int) $saturation) - ->flatMap(static fn($saturation) => Saturation::of($saturation)); + ->flatMap(static fn($saturation) => Saturation::of($saturation)->maybe()); $lightness = $matches ->get('lightness') ->filter(static fn($lightness) => \is_numeric($lightness)) ->map(static fn($lightness) => (int) $lightness) - ->flatMap(static fn($lightness) => Lightness::of($lightness)); + ->flatMap(static fn($lightness) => Lightness::of($lightness)->maybe()); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); - - return Maybe::all($hue, $saturation, $lightness, $alpha)->map( - static fn(Hue $hue, Saturation $saturation, Lightness $lightness, Alpha $alpha) => new self( - $hue, - $saturation, - $lightness, - $alpha, - ), - ); + ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); + + return Maybe::all($hue, $saturation, $lightness, $alpha) + ->map( + static fn(Hue $hue, Saturation $saturation, Lightness $lightness, Alpha $alpha) => new self( + $hue, + $saturation, + $lightness, + $alpha, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function withoutAlpha(Str $colour): Maybe + private static function withoutAlpha(Str $colour): Attempt { $matches = $colour ->capture(self::PATTERN_WITHOUT_ALPHA) @@ -303,24 +313,26 @@ private static function withoutAlpha(Str $colour): Maybe ->get('hue') ->filter(static fn($hue) => \is_numeric($hue)) ->map(static fn($hue) => (int) $hue) - ->flatMap(static fn($hue) => Hue::of($hue)); + ->flatMap(static fn($hue) => Hue::of($hue)->maybe()); $saturation = $matches ->get('saturation') ->filter(static fn($saturation) => \is_numeric($saturation)) ->map(static fn($saturation) => (int) $saturation) - ->flatMap(static fn($saturation) => Saturation::of($saturation)); + ->flatMap(static fn($saturation) => Saturation::of($saturation)->maybe()); $lightness = $matches ->get('lightness') ->filter(static fn($lightness) => \is_numeric($lightness)) ->map(static fn($lightness) => (int) $lightness) - ->flatMap(static fn($lightness) => Lightness::of($lightness)); - - return Maybe::all($hue, $saturation, $lightness)->map( - static fn(Hue $hue, Saturation $saturation, Lightness $lightness) => new self( - $hue, - $saturation, - $lightness, - ), - ); + ->flatMap(static fn($lightness) => Lightness::of($lightness)->maybe()); + + return Maybe::all($hue, $saturation, $lightness) + ->map( + static fn(Hue $hue, Saturation $saturation, Lightness $lightness) => new self( + $hue, + $saturation, + $lightness, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Hue.php b/src/Hue.php index d253dc7..9102509 100644 --- a/src/Hue.php +++ b/src/Hue.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function rotateBy(int $degrees): self diff --git a/src/Intensity.php b/src/Intensity.php index 5235d22..8b85f96 100644 --- a/src/Intensity.php +++ b/src/Intensity.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function toInt(): int diff --git a/src/Lightness.php b/src/Lightness.php index 2a1d781..37a1adf 100644 --- a/src/Lightness.php +++ b/src/Lightness.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $lightness): self diff --git a/src/Magenta.php b/src/Magenta.php index 63fbfc3..d3814df 100644 --- a/src/Magenta.php +++ b/src/Magenta.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $magenta): self diff --git a/src/RGBA.php b/src/RGBA.php index 8fc80e1..13f5633 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -7,6 +7,7 @@ use Innmind\Immutable\{ Str, Maybe, + Attempt, }; /** @@ -45,10 +46,7 @@ public function __construct( */ public static function of(string $colour): self { - return self::maybe($colour)->match( - static fn($self) => $self, - static fn() => throw new DomainException($colour), - ); + return self::attempt($colour)->unwrap(); } /** @@ -57,12 +55,22 @@ public static function of(string $colour): self * @return Maybe */ public static function maybe(string $colour): Maybe + { + return self::attempt($colour)->maybe(); + } + + /** + * @psalm-pure + * + * @return Attempt + */ + public static function attempt(string $colour): Attempt { $colour = Str::of($colour)->trim(); return self::fromHexadecimal($colour) - ->otherwise(static fn() => self::fromRGBFunction($colour)) - ->otherwise(static fn() => self::fromRGBAFunction($colour)); + ->recover(static fn() => self::fromRGBFunction($colour)) + ->recover(static fn() => self::fromRGBAFunction($colour)); } public function red(): Red @@ -286,11 +294,11 @@ public function toString(): string /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromHexadecimal(Str $colour): Maybe + private static function fromHexadecimal(Str $colour): Attempt { - return self::fromHexadecimalWithAlpha($colour)->otherwise( + return self::fromHexadecimalWithAlpha($colour)->recover( static fn() => self::fromHexadecimalWithoutAlpha($colour), ); } @@ -298,9 +306,9 @@ private static function fromHexadecimal(Str $colour): Maybe /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromHexadecimalWithAlpha(Str $colour): Maybe + private static function fromHexadecimalWithAlpha(Str $colour): Attempt { if ($colour->startsWith('#')) { $colour = $colour->drop(1); @@ -310,8 +318,8 @@ private static function fromHexadecimalWithAlpha(Str $colour): Maybe $colour->length() !== 4 && $colour->length() !== 8 ) { - /** @var Maybe */ - return Maybe::nothing(); + /** @var Attempt */ + return Attempt::error(new DomainException('Invalid length')); } $matches = $colour @@ -319,33 +327,35 @@ private static function fromHexadecimalWithAlpha(Str $colour): Maybe ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->flatMap(static fn($red) => Red::fromHexadecimal($red)); + ->flatMap(static fn($red) => Red::fromHexadecimal($red)->maybe()); $green = $matches ->get('green') - ->flatMap(static fn($green) => Green::fromHexadecimal($green)); + ->flatMap(static fn($green) => Green::fromHexadecimal($green)->maybe()); $blue = $matches ->get('blue') - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); + ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)->maybe()); $alpha = $matches ->get('alpha') - ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)); - - return Maybe::all($red, $green, $blue, $alpha)->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ); + ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)->maybe()); + + return Maybe::all($red, $green, $blue, $alpha) + ->map( + static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( + $red, + $green, + $blue, + $alpha, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromHexadecimalWithoutAlpha(Str $colour): Maybe + private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt { if ($colour->startsWith('#')) { $colour = $colour->drop(1); @@ -355,8 +365,8 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Maybe $colour->length() !== 3 && $colour->length() !== 6 ) { - /** @var Maybe */ - return Maybe::nothing(); + /** @var Attempt */ + return Attempt::error(new DomainException('Invalid length')); } $matches = $colour @@ -364,31 +374,33 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Maybe ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->flatMap(static fn($red) => Red::fromHexadecimal($red)); + ->flatMap(static fn($red) => Red::fromHexadecimal($red)->maybe()); $green = $matches ->get('green') - ->flatMap(static fn($green) => Green::fromHexadecimal($green)); + ->flatMap(static fn($green) => Green::fromHexadecimal($green)->maybe()); $blue = $matches ->get('blue') - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); - - return Maybe::all($red, $green, $blue)->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ); + ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)->maybe()); + + return Maybe::all($red, $green, $blue) + ->map( + static fn(Red $red, Green $green, Blue $blue) => new self( + $red, + $green, + $blue, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBFunction(Str $colour): Maybe + private static function fromRGBFunction(Str $colour): Attempt { - return self::fromRGBFunctionWithPoints($colour)->otherwise( + return self::fromRGBFunctionWithPoints($colour)->recover( static fn() => self::fromRGBFunctionWithPercents($colour), ); } @@ -396,9 +408,9 @@ private static function fromRGBFunction(Str $colour): Maybe /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBFunctionWithPoints(Str $colour): Maybe + private static function fromRGBFunctionWithPoints(Str $colour): Attempt { $matches = $colour ->capture(self::RGB_FUNCTION_PATTERN) @@ -407,33 +419,35 @@ private static function fromRGBFunctionWithPoints(Str $colour): Maybe ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Red::of($red)); + ->flatMap(static fn($red) => Red::of($red)->maybe()); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Green::of($green)); + ->flatMap(static fn($green) => Green::of($green)->maybe()); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Blue::of($blue)); - - return Maybe::all($red, $green, $blue)->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ); + ->flatMap(static fn($blue) => Blue::of($blue)->maybe()); + + return Maybe::all($red, $green, $blue) + ->map( + static fn(Red $red, Green $green, Blue $blue) => new self( + $red, + $green, + $blue, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBFunctionWithPercents(Str $colour): Maybe + private static function fromRGBFunctionWithPercents(Str $colour): Attempt { $matches = $colour ->capture(self::PERCENTED_RGB_FUNCTION_PATTERN) @@ -442,38 +456,40 @@ private static function fromRGBFunctionWithPercents(Str $colour): Maybe ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Intensity::of($red)) - ->flatMap(static fn($red) => Red::fromIntensity($red)); + ->flatMap(static fn($red) => Intensity::of($red)->maybe()) + ->flatMap(static fn($red) => Red::fromIntensity($red)->maybe()); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Intensity::of($green)) - ->flatMap(static fn($green) => Green::fromIntensity($green)); + ->flatMap(static fn($green) => Intensity::of($green)->maybe()) + ->flatMap(static fn($green) => Green::fromIntensity($green)->maybe()); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Intensity::of($blue)) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); - - return Maybe::all($red, $green, $blue)->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ); + ->flatMap(static fn($blue) => Intensity::of($blue)->maybe()) + ->flatMap(static fn($blue) => Blue::fromIntensity($blue)->maybe()); + + return Maybe::all($red, $green, $blue) + ->map( + static fn(Red $red, Green $green, Blue $blue) => new self( + $red, + $green, + $blue, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBAFunction(Str $colour): Maybe + private static function fromRGBAFunction(Str $colour): Attempt { - return self::fromRGBAFunctionWithPoints($colour)->otherwise( + return self::fromRGBAFunctionWithPoints($colour)->recover( static fn() => self::fromRGBAFunctionWithPercents($colour), ); } @@ -481,9 +497,9 @@ private static function fromRGBAFunction(Str $colour): Maybe /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBAFunctionWithPoints(Str $colour): Maybe + private static function fromRGBAFunctionWithPoints(Str $colour): Attempt { $matches = $colour ->capture(self::RGBA_FUNCTION_PATTERN) @@ -492,39 +508,41 @@ private static function fromRGBAFunctionWithPoints(Str $colour): Maybe ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Red::of($red)); + ->flatMap(static fn($red) => Red::of($red)->maybe()); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Green::of($green)); + ->flatMap(static fn($green) => Green::of($green)->maybe()); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Blue::of($blue)); + ->flatMap(static fn($blue) => Blue::of($blue)->maybe()); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); - - return Maybe::all($red, $green, $blue, $alpha)->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ); + ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); + + return Maybe::all($red, $green, $blue, $alpha) + ->map( + static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( + $red, + $green, + $blue, + $alpha, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - private static function fromRGBAFunctionWithPercents(Str $colour): Maybe + private static function fromRGBAFunctionWithPercents(Str $colour): Attempt { $matches = $colour ->capture(self::PERCENTED_RGBA_FUNCTION_PATTERN) @@ -533,33 +551,35 @@ private static function fromRGBAFunctionWithPercents(Str $colour): Maybe ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Intensity::of($red)) - ->flatMap(static fn($red) => Red::fromIntensity($red)); + ->flatMap(static fn($red) => Intensity::of($red)->maybe()) + ->flatMap(static fn($red) => Red::fromIntensity($red)->maybe()); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Intensity::of($green)) - ->flatMap(static fn($green) => Green::fromIntensity($green)); + ->flatMap(static fn($green) => Intensity::of($green)->maybe()) + ->flatMap(static fn($green) => Green::fromIntensity($green)->maybe()); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Intensity::of($blue)) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); + ->flatMap(static fn($blue) => Intensity::of($blue)->maybe()) + ->flatMap(static fn($blue) => Blue::fromIntensity($blue)->maybe()); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); - - return Maybe::all($red, $green, $blue, $alpha)->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ); + ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); + + return Maybe::all($red, $green, $blue, $alpha) + ->map( + static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( + $red, + $green, + $blue, + $alpha, + ), + ) + ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Red.php b/src/Red.php index 410393b..d13fb5e 100644 --- a/src/Red.php +++ b/src/Red.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,24 +28,19 @@ public function __construct(int $integer) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromHexadecimal(string $hex): Maybe + public static function fromHexadecimal(string $hex): Attempt { if (\mb_strlen($hex) === 1) { $hex .= $hex; @@ -57,9 +52,9 @@ public static function fromHexadecimal(string $hex): Maybe /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function fromIntensity(Intensity $intensity): Maybe + public static function fromIntensity(Intensity $intensity): Attempt { return self::of( (int) \round((255 * $intensity->toInt()) / 100), diff --git a/src/Saturation.php b/src/Saturation.php index 0240522..b6e76ea 100644 --- a/src/Saturation.php +++ b/src/Saturation.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $saturation): self diff --git a/src/Yellow.php b/src/Yellow.php index de646c0..3604d7a 100644 --- a/src/Yellow.php +++ b/src/Yellow.php @@ -4,7 +4,7 @@ namespace Innmind\Colour; use Innmind\Colour\Exception\InvalidValueRangeException; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; /** * @psalm-immutable @@ -28,16 +28,11 @@ public function __construct(int $value) /** * @psalm-pure * - * @return Maybe + * @return Attempt */ - public static function of(int $value): Maybe + public static function of(int $value): Attempt { - try { - return Maybe::just(new self($value)); - } catch (InvalidValueRangeException $e) { - /** @var Maybe */ - return Maybe::nothing(); - } + return Attempt::of(static fn() => new self($value)); } public function add(self $yellow): self From 6299f22da497aa1fc7ae52b22a1ae3aeccac62b5 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:34:00 +0100 Subject: [PATCH 09/20] fix the kind of exceptions that can be thrown --- src/CMYKA.php | 2 +- src/Colour.php | 3 +-- src/HSLA.php | 2 +- src/RGBA.php | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/CMYKA.php b/src/CMYKA.php index d53fda4..377e723 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -41,7 +41,7 @@ public function __construct( /** * @psalm-pure * - * @throws DomainException + * @throws \Exception */ public static function of(string $colour): self { diff --git a/src/Colour.php b/src/Colour.php index 5bdefdf..1e79f79 100644 --- a/src/Colour.php +++ b/src/Colour.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\DomainException; use Innmind\Immutable\{ Maybe, Attempt, @@ -118,7 +117,7 @@ enum Colour /** * @psalm-pure * - * @throws DomainException + * @throws \Exception */ public static function of(string $colour): RGBA|HSLA|CMYKA { diff --git a/src/HSLA.php b/src/HSLA.php index b732c52..aec48de 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -38,7 +38,7 @@ public function __construct( /** * @psalm-pure * - * @throws DomainException + * @throws \Exception */ public static function of(string $colour): self { diff --git a/src/RGBA.php b/src/RGBA.php index 13f5633..eacf502 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -42,7 +42,7 @@ public function __construct( /** * @psalm-pure * - * @throws DomainException + * @throws \Exception */ public static function of(string $colour): self { From 8109da2ae41739f949373c9716370c2fb08a75a9 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:34:05 +0100 Subject: [PATCH 10/20] add workflow to create releases --- .github/workflows/release.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b25ad8a --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,11 @@ +name: Create release + +on: + push: + tags: + - '*' + +jobs: + release: + uses: innmind/github-workflows/.github/workflows/release.yml@main + secrets: inherit From ddbdfd0b39feb484c211ef213cc652c6e3bc2346 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Mon, 3 Nov 2025 16:40:21 +0100 Subject: [PATCH 11/20] make constructors private --- CHANGELOG.md | 15 ++++ src/Alpha.php | 10 ++- src/Black.php | 12 +++- src/Blue.php | 12 +++- src/CMYKA.php | 46 ++++++------- src/Cyan.php | 12 +++- src/Green.php | 12 +++- src/HSLA.php | 51 +++++++------- src/Hue.php | 12 +++- src/Intensity.php | 12 +++- src/Lightness.php | 12 +++- src/Magenta.php | 12 +++- src/RGBA.php | 105 +++++++++++----------------- src/Red.php | 12 +++- src/Saturation.php | 12 +++- src/Yellow.php | 12 +++- tests/AlphaTest.php | 32 ++++----- tests/BlackTest.php | 30 ++++---- tests/BlueTest.php | 40 +++++------ tests/CMYKATest.php | 144 +++++++++++++++++++-------------------- tests/CyanTest.php | 30 ++++---- tests/GreenTest.php | 40 +++++------ tests/HSLATest.php | 88 ++++++++++++------------ tests/HueTest.php | 26 +++---- tests/IntensityTest.php | 6 +- tests/LightnessTest.php | 30 ++++---- tests/MagentaTest.php | 30 ++++---- tests/RGBATest.php | 110 +++++++++++++++--------------- tests/RedTest.php | 40 +++++------ tests/SaturationTest.php | 30 ++++---- tests/YellowTest.php | 30 ++++---- 31 files changed, 584 insertions(+), 481 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0344e49..697bbff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,21 @@ - Requires PHP `8.4` - Named constructors `::of()`, `::fromHexadecimal()` and `::fromIntensity()` now return instances of `Innmind\Immutable\Attempt` +- `Innmind\Colour\Alpha` constructor is now private, use `::of()` instead +- `Innmind\Colour\Back` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Blue` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Cyan` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Green` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Hue` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Intensity` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Lightness` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Magenta` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Red` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Saturation` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\Yellow` constructor is now private, use `::of()` or `::at()` instead +- `Innmind\Colour\CMYKA` constructor is now private, use `::from()` instead +- `Innmind\Colour\HSLA` constructor is now private, use `::from()` instead +- `Innmind\Colour\RGBA` constructor is now private, use `::from()` instead ### Fixed diff --git a/src/Alpha.php b/src/Alpha.php index 3097c91..926194e 100644 --- a/src/Alpha.php +++ b/src/Alpha.php @@ -16,7 +16,7 @@ final class Alpha /** * @throws InvalidValueRangeException */ - public function __construct(float $value) + private function __construct(float $value) { if ($value < 0 || $value > 1) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,14 @@ public function __construct(float $value) $this->value = $value; } + /** + * @psalm-pure + */ + public static function max(): self + { + return new self(1); + } + /** * @psalm-pure * diff --git a/src/Black.php b/src/Black.php index 9ff6ca0..fbc2363 100644 --- a/src/Black.php +++ b/src/Black.php @@ -16,7 +16,7 @@ final class Black /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Blue.php b/src/Blue.php index 01dd2f2..55e4f31 100644 --- a/src/Blue.php +++ b/src/Blue.php @@ -16,7 +16,7 @@ final class Blue /** * @throws InvalidValueRangeException */ - public function __construct(int $integer) + private function __construct(int $integer) { if ($integer < 0 || $integer > 255) { throw new InvalidValueRangeException((string) $integer); @@ -25,6 +25,16 @@ public function __construct(int $integer) $this->integer = $integer; } + /** + * @psalm-pure + * + * @param int<0, 255> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/CMYKA.php b/src/CMYKA.php index 377e723..df1a150 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -24,18 +24,31 @@ final class CMYKA private Black $black; private Alpha $alpha; - public function __construct( + private function __construct( Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black, - ?Alpha $alpha = null, + Alpha $alpha, ) { $this->cyan = $cyan; $this->magenta = $magenta; $this->yellow = $yellow; $this->black = $black; - $this->alpha = $alpha ?? new Alpha(1); + $this->alpha = $alpha; + } + + /** + * @psalm-pure + */ + public static function from( + Cyan $cyan, + Magenta $magenta, + Yellow $yellow, + Black $black, + ?Alpha $alpha = null, + ): self { + return new self($cyan, $magenta, $yellow, $black, $alpha ?? Alpha::max()); } /** @@ -227,10 +240,10 @@ public function toRGBA(): RGBA $green = 1 - \min(1, $magenta * (1 - $black) + $black); $blue = 1 - \min(1, $yellow * (1 - $black) + $black); - return new RGBA( - new Red((int) \round($red * 255)), - new Green((int) \round($green * 255)), - new Blue((int) \round($blue * 255)), + return RGBA::from( + Red::of((int) \round($red * 255))->unwrap(), + Green::of((int) \round($green * 255))->unwrap(), + Blue::of((int) \round($blue * 255))->unwrap(), $this->alpha, ); } @@ -304,15 +317,7 @@ private static function withAlpha(Str $colour): Attempt ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); return Maybe::all($cyan, $magenta, $yellow, $black, $alpha) - ->map( - static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black, Alpha $alpha) => new self( - $cyan, - $magenta, - $yellow, - $black, - $alpha, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -348,14 +353,7 @@ private static function withoutAlpha(Str $colour): Attempt ->flatMap(static fn($black) => Black::of($black)->maybe()); return Maybe::all($cyan, $magenta, $yellow, $black) - ->map( - static fn(Cyan $cyan, Magenta $magenta, Yellow $yellow, Black $black) => new self( - $cyan, - $magenta, - $yellow, - $black, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Cyan.php b/src/Cyan.php index b6c6e46..e7ebd23 100644 --- a/src/Cyan.php +++ b/src/Cyan.php @@ -16,7 +16,7 @@ final class Cyan /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Green.php b/src/Green.php index 7ba7e2d..0a849d0 100644 --- a/src/Green.php +++ b/src/Green.php @@ -16,7 +16,7 @@ final class Green /** * @throws InvalidValueRangeException */ - public function __construct(int $integer) + private function __construct(int $integer) { if ($integer < 0 || $integer > 255) { throw new InvalidValueRangeException((string) $integer); @@ -25,6 +25,16 @@ public function __construct(int $integer) $this->integer = $integer; } + /** + * @psalm-pure + * + * @param int<0, 255> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/HSLA.php b/src/HSLA.php index aec48de..bfecd92 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -23,16 +23,28 @@ final class HSLA private Lightness $lightness; private Alpha $alpha; - public function __construct( + private function __construct( Hue $hue, Saturation $saturation, Lightness $lightness, - ?Alpha $alpha = null, + Alpha $alpha, ) { $this->hue = $hue; $this->saturation = $saturation; $this->lightness = $lightness; - $this->alpha = $alpha ?? new Alpha(1); + $this->alpha = $alpha; + } + + /** + * @psalm-pure + */ + public static function from( + Hue $hue, + Saturation $saturation, + Lightness $lightness, + ?Alpha $alpha = null, + ): self { + return new self($hue, $saturation, $lightness, $alpha ?? Alpha::max()); } /** @@ -172,10 +184,10 @@ public function toRGBA(): RGBA $lightness = $this->lightness->toInt() / 100; if ($this->saturation->atMinimum()) { - return new RGBA( - new Red((int) \round($lightness * 255)), - new Green((int) \round($lightness * 255)), - new Blue((int) \round($lightness * 255)), + return RGBA::from( + Red::of((int) \round($lightness * 255))->unwrap(), + Green::of((int) \round($lightness * 255))->unwrap(), + Blue::of((int) \round($lightness * 255))->unwrap(), $this->alpha, ); } @@ -187,10 +199,10 @@ public function toRGBA(): RGBA $q = $lightness < 0.5 ? $lightness * (1 + $saturation) : $lightness + $saturation - $lightness * $saturation; $p = 2 * $lightness - $q; - return new RGBA( - new Red((int) \round($this->hueToPoint($p, $q, $hue + 1 / 3) * 255)), - new Green((int) \round($this->hueToPoint($p, $q, $hue) * 255)), - new Blue((int) \round($this->hueToPoint($p, $q, $hue - 1 / 3) * 255)), + return RGBA::from( + Red::of((int) \round($this->hueToPoint($p, $q, $hue + 1 / 3) * 255))->unwrap(), + Green::of((int) \round($this->hueToPoint($p, $q, $hue) * 255))->unwrap(), + Blue::of((int) \round($this->hueToPoint($p, $q, $hue - 1 / 3) * 255))->unwrap(), $this->alpha, ); } @@ -288,14 +300,7 @@ private static function withAlpha(Str $colour): Attempt ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); return Maybe::all($hue, $saturation, $lightness, $alpha) - ->map( - static fn(Hue $hue, Saturation $saturation, Lightness $lightness, Alpha $alpha) => new self( - $hue, - $saturation, - $lightness, - $alpha, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -326,13 +331,7 @@ private static function withoutAlpha(Str $colour): Attempt ->flatMap(static fn($lightness) => Lightness::of($lightness)->maybe()); return Maybe::all($hue, $saturation, $lightness) - ->map( - static fn(Hue $hue, Saturation $saturation, Lightness $lightness) => new self( - $hue, - $saturation, - $lightness, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Hue.php b/src/Hue.php index 9102509..1f8d096 100644 --- a/src/Hue.php +++ b/src/Hue.php @@ -16,7 +16,7 @@ final class Hue /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 359) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 359> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Intensity.php b/src/Intensity.php index 8b85f96..bae4b39 100644 --- a/src/Intensity.php +++ b/src/Intensity.php @@ -16,7 +16,7 @@ final class Intensity /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Lightness.php b/src/Lightness.php index 37a1adf..646a239 100644 --- a/src/Lightness.php +++ b/src/Lightness.php @@ -16,7 +16,7 @@ final class Lightness /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Magenta.php b/src/Magenta.php index d3814df..9258084 100644 --- a/src/Magenta.php +++ b/src/Magenta.php @@ -16,7 +16,7 @@ final class Magenta /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/RGBA.php b/src/RGBA.php index eacf502..f434160 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -27,16 +27,28 @@ final class RGBA private Green $green; private Alpha $alpha; - public function __construct( + private function __construct( Red $red, Green $green, Blue $blue, - ?Alpha $alpha = null, + Alpha $alpha, ) { $this->red = $red; $this->blue = $blue; $this->green = $green; - $this->alpha = $alpha ?? new Alpha(1); + $this->alpha = $alpha; + } + + /** + * @psalm-pure + */ + public static function from( + Red $red, + Green $green, + Blue $blue, + ?Alpha $alpha = null, + ): self { + return new self($red, $green, $blue, $alpha ?? Alpha::max()); } /** @@ -203,10 +215,10 @@ public function toHSLA(): HSLA $lightness = ($max + $min) / 2; if ($max === $min) { - return new HSLA( - new Hue(0), - new Saturation(0), - new Lightness((int) \round($lightness * 100)), + return HSLA::from( + Hue::at(0), + Saturation::at(0), + Lightness::of((int) \round($lightness * 100))->unwrap(), $this->alpha, ); } @@ -229,10 +241,10 @@ public function toHSLA(): HSLA $hue *= 60; - return new HSLA( - new Hue((int) \round($hue)), - new Saturation((int) \round($saturation * 100)), - new Lightness((int) \round($lightness * 100)), + return HSLA::from( + Hue::of((int) \round($hue))->unwrap(), + Saturation::of((int) \round($saturation * 100))->unwrap(), + Lightness::of((int) \round($lightness * 100))->unwrap(), $this->alpha, ); } @@ -248,11 +260,11 @@ public function toCMYKA(): CMYKA $this->green->atMinimum() && $this->blue->atMinimum() ) { - return new CMYKA( - new Cyan(0), - new Magenta(0), - new Yellow(0), - new Black(100), + return CMYKA::from( + Cyan::at(0), + Magenta::at(0), + Yellow::at(0), + Black::at(100), $this->alpha, ); } @@ -262,11 +274,11 @@ public function toCMYKA(): CMYKA $magenta = (1 - $green - $black) / (1 - $black); $yellow = (1 - $blue - $black) / (1 - $black); - return new CMYKA( - new Cyan((int) \round($cyan * 100)), - new Magenta((int) \round($magenta * 100)), - new Yellow((int) \round($yellow * 100)), - new Black((int) \round($black * 100)), + return CMYKA::from( + Cyan::of((int) \round($cyan * 100))->unwrap(), + Magenta::of((int) \round($magenta * 100))->unwrap(), + Yellow::of((int) \round($yellow * 100))->unwrap(), + Black::of((int) \round($black * 100))->unwrap(), $this->alpha, ); } @@ -339,14 +351,7 @@ private static function fromHexadecimalWithAlpha(Str $colour): Attempt ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)->maybe()); return Maybe::all($red, $green, $blue, $alpha) - ->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -383,13 +388,7 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)->maybe()); return Maybe::all($red, $green, $blue) - ->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -432,13 +431,7 @@ private static function fromRGBFunctionWithPoints(Str $colour): Attempt ->flatMap(static fn($blue) => Blue::of($blue)->maybe()); return Maybe::all($red, $green, $blue) - ->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -472,13 +465,7 @@ private static function fromRGBFunctionWithPercents(Str $colour): Attempt ->flatMap(static fn($blue) => Blue::fromIntensity($blue)->maybe()); return Maybe::all($red, $green, $blue) - ->map( - static fn(Red $red, Green $green, Blue $blue) => new self( - $red, - $green, - $blue, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -526,14 +513,7 @@ private static function fromRGBAFunctionWithPoints(Str $colour): Attempt ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); return Maybe::all($red, $green, $blue, $alpha) - ->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } @@ -572,14 +552,7 @@ private static function fromRGBAFunctionWithPercents(Str $colour): Attempt ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); return Maybe::all($red, $green, $blue, $alpha) - ->map( - static fn(Red $red, Green $green, Blue $blue, Alpha $alpha) => new self( - $red, - $green, - $blue, - $alpha, - ), - ) + ->map(self::from(...)) ->attempt(static fn() => new DomainException($colour->toString())); } } diff --git a/src/Red.php b/src/Red.php index d13fb5e..1c81bb4 100644 --- a/src/Red.php +++ b/src/Red.php @@ -16,7 +16,7 @@ final class Red /** * @throws InvalidValueRangeException */ - public function __construct(int $integer) + private function __construct(int $integer) { if ($integer < 0 || $integer > 255) { throw new InvalidValueRangeException((string) $integer); @@ -25,6 +25,16 @@ public function __construct(int $integer) $this->integer = $integer; } + /** + * @psalm-pure + * + * @param int<0, 255> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Saturation.php b/src/Saturation.php index b6e76ea..7cece0b 100644 --- a/src/Saturation.php +++ b/src/Saturation.php @@ -16,7 +16,7 @@ final class Saturation /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/src/Yellow.php b/src/Yellow.php index 3604d7a..c90810d 100644 --- a/src/Yellow.php +++ b/src/Yellow.php @@ -16,7 +16,7 @@ final class Yellow /** * @throws InvalidValueRangeException */ - public function __construct(int $value) + private function __construct(int $value) { if ($value < 0 || $value > 100) { throw new InvalidValueRangeException((string) $value); @@ -25,6 +25,16 @@ public function __construct(int $value) $this->value = $value; } + /** + * @psalm-pure + * + * @param int<0, 100> $value + */ + public static function at(int $value): self + { + return new self($value); + } + /** * @psalm-pure * diff --git a/tests/AlphaTest.php b/tests/AlphaTest.php index 3bacc3a..0d4b05a 100644 --- a/tests/AlphaTest.php +++ b/tests/AlphaTest.php @@ -14,7 +14,7 @@ class AlphaTest extends TestCase { public function testInterface() { - $alpha = new Alpha(0.5); + $alpha = Alpha::of(0.5)->unwrap(); $this->assertSame(0.5, $alpha->toFloat()); $this->assertSame('0.5', $alpha->toString()); @@ -22,20 +22,20 @@ public function testInterface() public function testAdd() { - $alpha = (new Alpha(0.12))->add(new Alpha(0.30)); + $alpha = Alpha::of(0.12)->unwrap()->add(Alpha::of(0.30)->unwrap()); $this->assertInstanceOf(Alpha::class, $alpha); $this->assertSame(0.42, $alpha->toFloat()); $this->assertSame( 1.0, - (new Alpha(0.6))->add(new Alpha(0.7))->toFloat(), + Alpha::of(0.6)->unwrap()->add(Alpha::of(0.7)->unwrap())->toFloat(), ); } public function testSub() { - $alpha = (new Alpha(0.54))->subtract(new Alpha(0.12)); + $alpha = Alpha::of(0.54)->unwrap()->subtract(Alpha::of(0.12)->unwrap()); $this->assertInstanceOf(Alpha::class, $alpha); $this->assertGreaterThanOrEqual(0.4199, $alpha->toFloat()); @@ -43,7 +43,7 @@ public function testSub() $this->assertSame( 0.0, - (new Alpha(0.5))->subtract(new Alpha(0.7))->toFloat(), + Alpha::of(0.5)->unwrap()->subtract(Alpha::of(0.7)->unwrap())->toFloat(), ); } @@ -52,7 +52,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-0.1'); - new Alpha(-0.1); + Alpha::of(-0.1)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -60,21 +60,21 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('1.1'); - new Alpha(1.1); + Alpha::of(1.1)->unwrap(); } public function testAtMaximum() { - $this->assertTrue((new Alpha(1))->atMaximum()); - $this->assertFalse((new Alpha(0))->atMaximum()); - $this->assertFalse((new Alpha(0.5))->atMaximum()); + $this->assertTrue(Alpha::of(1)->unwrap()->atMaximum()); + $this->assertFalse(Alpha::of(0)->unwrap()->atMaximum()); + $this->assertFalse(Alpha::of(0.5)->unwrap()->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Alpha(1))->atMinimum()); - $this->assertTrue((new Alpha(0))->atMinimum()); - $this->assertFalse((new Alpha(0.5))->atMinimum()); + $this->assertFalse(Alpha::of(1)->unwrap()->atMinimum()); + $this->assertTrue(Alpha::of(0)->unwrap()->atMinimum()); + $this->assertFalse(Alpha::of(0.5)->unwrap()->atMinimum()); } #[DataProvider('hexadecimals')] @@ -82,7 +82,7 @@ public function testHexadecimal($hex, $percent) { $this->assertSame( $hex, - (new Alpha($percent))->toHexadecimal(), + Alpha::of($percent)->unwrap()->toHexadecimal(), ); $alpha = Alpha::fromHexadecimal($hex)->match( @@ -122,7 +122,7 @@ public static function hexadecimals() public function testEquals() { - $this->assertTrue((new Alpha(0.5))->equals(new Alpha(0.5))); - $this->assertFalse((new Alpha(1.0))->equals(new Alpha(0.5))); + $this->assertTrue(Alpha::of(0.5)->unwrap()->equals(Alpha::of(0.5)->unwrap())); + $this->assertFalse(Alpha::of(1.0)->unwrap()->equals(Alpha::of(0.5)->unwrap())); } } diff --git a/tests/BlackTest.php b/tests/BlackTest.php index 2780d13..50c426c 100644 --- a/tests/BlackTest.php +++ b/tests/BlackTest.php @@ -13,7 +13,7 @@ class BlackTest extends TestCase { public function testInterface() { - $black = new Black(50); + $black = Black::at(50); $this->assertSame(50, $black->toInt()); $this->assertSame('50', $black->toString()); @@ -24,7 +24,7 @@ public function testThrowWhenValueTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-1'); - new Black(-1); + Black::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -32,14 +32,14 @@ public function testThrowWhenValueTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Black(101); + Black::of(101)->unwrap(); } public function testAdd() { - $black = new Black(50); + $black = Black::at(50); - $black2 = $black->add(new Black(25)); + $black2 = $black->add(Black::at(25)); $this->assertInstanceOf(Black::class, $black2); $this->assertNotSame($black, $black2); @@ -49,9 +49,9 @@ public function testAdd() public function testSubtract() { - $black = new Black(50); + $black = Black::at(50); - $black2 = $black->subtract(new Black(25)); + $black2 = $black->subtract(Black::at(25)); $this->assertInstanceOf(Black::class, $black2); $this->assertNotSame($black, $black2); @@ -61,21 +61,21 @@ public function testSubtract() public function testEquals() { - $this->assertTrue((new Black(50))->equals(new Black(50))); - $this->assertFalse((new Black(100))->equals(new Black(50))); + $this->assertTrue(Black::at(50)->equals(Black::at(50))); + $this->assertFalse(Black::at(100)->equals(Black::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Black(100))->atMaximum()); - $this->assertFalse((new Black(0))->atMaximum()); - $this->assertFalse((new Black(50))->atMaximum()); + $this->assertTrue(Black::at(100)->atMaximum()); + $this->assertFalse(Black::at(0)->atMaximum()); + $this->assertFalse(Black::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Black(100))->atMinimum()); - $this->assertTrue((new Black(0))->atMinimum()); - $this->assertFalse((new Black(50))->atMinimum()); + $this->assertFalse(Black::at(100)->atMinimum()); + $this->assertTrue(Black::at(0)->atMinimum()); + $this->assertFalse(Black::at(50)->atMinimum()); } } diff --git a/tests/BlueTest.php b/tests/BlueTest.php index 212673a..1c73507 100644 --- a/tests/BlueTest.php +++ b/tests/BlueTest.php @@ -14,7 +14,7 @@ class BlueTest extends TestCase { public function testInterface() { - $blue = new Blue(255); + $blue = Blue::at(255); $this->assertSame(255, $blue->toInt()); $this->assertSame('ff', $blue->toString()); @@ -34,27 +34,27 @@ public function testFromHexadecimal() public function testAdd() { - $blue = (new Blue(12))->add(new Blue(30)); + $blue = Blue::at(12)->add(Blue::at(30)); $this->assertInstanceOf(Blue::class, $blue); $this->assertSame(42, $blue->toInt()); $this->assertSame( 255, - (new Blue(150))->add(new Blue(150))->toInt(), + Blue::at(150)->add(Blue::at(150))->toInt(), ); } public function testSub() { - $blue = (new Blue(54))->subtract(new Blue(12)); + $blue = Blue::at(54)->subtract(Blue::at(12)); $this->assertInstanceOf(Blue::class, $blue); $this->assertSame(42, $blue->toInt()); $this->assertSame( 0, - (new Blue(150))->subtract(new Blue(255))->toInt(), + Blue::at(150)->subtract(Blue::at(255))->toInt(), ); } @@ -63,7 +63,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-42'); - new Blue(-42); + Blue::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -71,26 +71,26 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('512'); - new Blue(512); + Blue::of(512)->unwrap(); } public function testAtMaximum() { - $this->assertTrue((new Blue(255))->atMaximum()); - $this->assertFalse((new Blue(0))->atMaximum()); - $this->assertFalse((new Blue(122))->atMaximum()); + $this->assertTrue(Blue::at(255)->atMaximum()); + $this->assertFalse(Blue::at(0)->atMaximum()); + $this->assertFalse(Blue::at(122)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Blue(255))->atMinimum()); - $this->assertTrue((new Blue(0))->atMinimum()); - $this->assertFalse((new Blue(122))->atMinimum()); + $this->assertFalse(Blue::at(255)->atMinimum()); + $this->assertTrue(Blue::at(0)->atMinimum()); + $this->assertFalse(Blue::at(122)->atMinimum()); } public function testFromIntensity() { - $blue = Blue::fromIntensity(new Intensity(100))->match( + $blue = Blue::fromIntensity(Intensity::at(100))->match( static fn($blue) => $blue, static fn() => null, ); @@ -99,28 +99,28 @@ public function testFromIntensity() $this->assertSame(255, $blue->toInt()); $this->assertSame( 191, - Blue::fromIntensity(new Intensity(75))->match( + Blue::fromIntensity(Intensity::at(75))->match( static fn($blue) => $blue->toInt(), static fn() => null, ), ); $this->assertSame( 128, - Blue::fromIntensity(new Intensity(50))->match( + Blue::fromIntensity(Intensity::at(50))->match( static fn($blue) => $blue->toInt(), static fn() => null, ), ); $this->assertSame( 64, - Blue::fromIntensity(new Intensity(25))->match( + Blue::fromIntensity(Intensity::at(25))->match( static fn($blue) => $blue->toInt(), static fn() => null, ), ); $this->assertSame( 0, - Blue::fromIntensity(new Intensity(0))->match( + Blue::fromIntensity(Intensity::at(0))->match( static fn($blue) => $blue->toInt(), static fn() => null, ), @@ -129,7 +129,7 @@ public function testFromIntensity() public function testEquals() { - $this->assertTrue((new Blue(50))->equals(new Blue(50))); - $this->assertFalse((new Blue(100))->equals(new Blue(50))); + $this->assertTrue(Blue::at(50)->equals(Blue::at(50))); + $this->assertFalse(Blue::at(100)->equals(Blue::at(50))); } } diff --git a/tests/CMYKATest.php b/tests/CMYKATest.php index 4e00d79..f7fb1d1 100644 --- a/tests/CMYKATest.php +++ b/tests/CMYKATest.php @@ -19,11 +19,11 @@ class CMYKATest extends TestCase { public function testInterface() { - $cmyk = new CMYKA( - $cyan = new Cyan(10), - $magenta = new Magenta(20), - $yellow = new Yellow(30), - $black = new Black(40), + $cmyk = CMYKA::from( + $cyan = Cyan::at(10), + $magenta = Magenta::at(20), + $yellow = Yellow::at(30), + $black = Black::at(40), ); $this->assertSame($cyan, $cmyk->cyan()); @@ -33,12 +33,12 @@ public function testInterface() $this->assertSame(1.0, $cmyk->alpha()->toFloat()); $this->assertSame('device-cmyk(10%, 20%, 30%, 40%)', $cmyk->toString()); - $cmyka = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), - $alpha = new Alpha(0.5), + $cmyka = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), + $alpha = Alpha::of(0.5)->unwrap(), ); $this->assertSame($alpha, $cmyka->alpha()); @@ -47,14 +47,14 @@ public function testInterface() public function testAddCyan() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->addCyan(new Cyan(1)); + $cmyk2 = $cmyk->addCyan(Cyan::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -72,14 +72,14 @@ public function testAddCyan() public function testSubtractCyan() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->subtractCyan(new Cyan(1)); + $cmyk2 = $cmyk->subtractCyan(Cyan::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -97,14 +97,14 @@ public function testSubtractCyan() public function testAddMagenta() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->addMagenta(new Magenta(1)); + $cmyk2 = $cmyk->addMagenta(Magenta::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -122,14 +122,14 @@ public function testAddMagenta() public function testSubtractMagenta() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->subtractMagenta(new Magenta(1)); + $cmyk2 = $cmyk->subtractMagenta(Magenta::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -147,14 +147,14 @@ public function testSubtractMagenta() public function testAddYellow() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->addYellow(new Yellow(1)); + $cmyk2 = $cmyk->addYellow(Yellow::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -172,14 +172,14 @@ public function testAddYellow() public function testSubtractYellow() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->subtractYellow(new Yellow(1)); + $cmyk2 = $cmyk->subtractYellow(Yellow::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -197,14 +197,14 @@ public function testSubtractYellow() public function testAddBlack() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->addBlack(new Black(1)); + $cmyk2 = $cmyk->addBlack(Black::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -222,14 +222,14 @@ public function testAddBlack() public function testSubtractBlack() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->subtractBlack(new Black(1)); + $cmyk2 = $cmyk->subtractBlack(Black::at(1)); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -247,15 +247,15 @@ public function testSubtractBlack() public function testAddAlpha() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), - new Alpha(0.1), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), + Alpha::of(0.1)->unwrap(), ); - $cmyk2 = $cmyk->addAlpha(new Alpha(0.1)); + $cmyk2 = $cmyk->addAlpha(Alpha::of(0.1)->unwrap()); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); @@ -273,14 +273,14 @@ public function testAddAlpha() public function testSubtractAlpha() { - $cmyk = new CMYKA( - new Cyan(10), - new Magenta(20), - new Yellow(30), - new Black(40), + $cmyk = CMYKA::from( + Cyan::at(10), + Magenta::at(20), + Yellow::at(30), + Black::at(40), ); - $cmyk2 = $cmyk->subtractAlpha(new Alpha(0.9)); + $cmyk2 = $cmyk->subtractAlpha(Alpha::of(0.9)->unwrap()); $this->assertInstanceOf(CMYKA::class, $cmyk2); $this->assertNotSame($cmyk, $cmyk2); diff --git a/tests/CyanTest.php b/tests/CyanTest.php index 0ffa7df..2be0b35 100644 --- a/tests/CyanTest.php +++ b/tests/CyanTest.php @@ -13,7 +13,7 @@ class CyanTest extends TestCase { public function testInterface() { - $cyan = new Cyan(50); + $cyan = Cyan::at(50); $this->assertSame(50, $cyan->toInt()); $this->assertSame('50', $cyan->toString()); @@ -24,7 +24,7 @@ public function testThrowWhenValueTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-1'); - new Cyan(-1); + Cyan::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -32,14 +32,14 @@ public function testThrowWhenValueTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Cyan(101); + Cyan::of(101)->unwrap(); } public function testAdd() { - $cyan = new Cyan(50); + $cyan = Cyan::at(50); - $cyan2 = $cyan->add(new Cyan(25)); + $cyan2 = $cyan->add(Cyan::at(25)); $this->assertInstanceOf(Cyan::class, $cyan2); $this->assertNotSame($cyan, $cyan2); @@ -49,9 +49,9 @@ public function testAdd() public function testSubtract() { - $cyan = new Cyan(50); + $cyan = Cyan::at(50); - $cyan2 = $cyan->subtract(new Cyan(25)); + $cyan2 = $cyan->subtract(Cyan::at(25)); $this->assertInstanceOf(Cyan::class, $cyan2); $this->assertNotSame($cyan, $cyan2); @@ -61,21 +61,21 @@ public function testSubtract() public function testEquals() { - $this->assertTrue((new Cyan(50))->equals(new Cyan(50))); - $this->assertFalse((new Cyan(100))->equals(new Cyan(50))); + $this->assertTrue(Cyan::at(50)->equals(Cyan::at(50))); + $this->assertFalse(Cyan::at(100)->equals(Cyan::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Cyan(100))->atMaximum()); - $this->assertFalse((new Cyan(0))->atMaximum()); - $this->assertFalse((new Cyan(50))->atMaximum()); + $this->assertTrue(Cyan::at(100)->atMaximum()); + $this->assertFalse(Cyan::at(0)->atMaximum()); + $this->assertFalse(Cyan::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Cyan(100))->atMinimum()); - $this->assertTrue((new Cyan(0))->atMinimum()); - $this->assertFalse((new Cyan(50))->atMinimum()); + $this->assertFalse(Cyan::at(100)->atMinimum()); + $this->assertTrue(Cyan::at(0)->atMinimum()); + $this->assertFalse(Cyan::at(50)->atMinimum()); } } diff --git a/tests/GreenTest.php b/tests/GreenTest.php index 1cf6284..7be13c3 100644 --- a/tests/GreenTest.php +++ b/tests/GreenTest.php @@ -14,7 +14,7 @@ class GreenTest extends TestCase { public function testInterface() { - $green = new Green(255); + $green = Green::at(255); $this->assertSame(255, $green->toInt()); $this->assertSame('ff', $green->toString()); @@ -34,27 +34,27 @@ public function testFromHexadecimal() public function testAdd() { - $Green = (new Green(12))->add(new Green(30)); + $Green = Green::at(12)->add(Green::at(30)); $this->assertInstanceOf(Green::class, $Green); $this->assertSame(42, $Green->toInt()); $this->assertSame( 255, - (new Green(150))->add(new Green(150))->toInt(), + Green::at(150)->add(Green::at(150))->toInt(), ); } public function testSub() { - $green = (new Green(54))->subtract(new Green(12)); + $green = Green::at(54)->subtract(Green::at(12)); $this->assertInstanceOf(Green::class, $green); $this->assertSame(42, $green->toInt()); $this->assertSame( 0, - (new Green(150))->subtract(new Green(255))->toInt(), + Green::at(150)->subtract(Green::at(255))->toInt(), ); } @@ -63,7 +63,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-42'); - new Green(-42); + Green::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -71,26 +71,26 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('512'); - new Green(512); + Green::of(512)->unwrap(); } public function testAtMaximum() { - $this->assertTrue((new Green(255))->atMaximum()); - $this->assertFalse((new Green(0))->atMaximum()); - $this->assertFalse((new Green(122))->atMaximum()); + $this->assertTrue(Green::at(255)->atMaximum()); + $this->assertFalse(Green::at(0)->atMaximum()); + $this->assertFalse(Green::at(122)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Green(255))->atMinimum()); - $this->assertTrue((new Green(0))->atMinimum()); - $this->assertFalse((new Green(122))->atMinimum()); + $this->assertFalse(Green::at(255)->atMinimum()); + $this->assertTrue(Green::at(0)->atMinimum()); + $this->assertFalse(Green::at(122)->atMinimum()); } public function testFromIntensity() { - $green = Green::fromIntensity(new Intensity(100))->match( + $green = Green::fromIntensity(Intensity::at(100))->match( static fn($green) => $green, static fn() => null, ); @@ -99,28 +99,28 @@ public function testFromIntensity() $this->assertSame(255, $green->toInt()); $this->assertSame( 191, - Green::fromIntensity(new Intensity(75))->match( + Green::fromIntensity(Intensity::at(75))->match( static fn($green) => $green->toInt(), static fn() => null, ), ); $this->assertSame( 128, - Green::fromIntensity(new Intensity(50))->match( + Green::fromIntensity(Intensity::at(50))->match( static fn($green) => $green->toInt(), static fn() => null, ), ); $this->assertSame( 64, - Green::fromIntensity(new Intensity(25))->match( + Green::fromIntensity(Intensity::at(25))->match( static fn($green) => $green->toInt(), static fn() => null, ), ); $this->assertSame( 0, - Green::fromIntensity(new Intensity(0))->match( + Green::fromIntensity(Intensity::at(0))->match( static fn($green) => $green->toInt(), static fn() => null, ), @@ -129,7 +129,7 @@ public function testFromIntensity() public function testEquals() { - $this->assertTrue((new Green(50))->equals(new Green(50))); - $this->assertFalse((new Green(100))->equals(new Green(50))); + $this->assertTrue(Green::at(50)->equals(Green::at(50))); + $this->assertFalse(Green::at(100)->equals(Green::at(50))); } } diff --git a/tests/HSLATest.php b/tests/HSLATest.php index db6c37d..e3eaf21 100644 --- a/tests/HSLATest.php +++ b/tests/HSLATest.php @@ -19,10 +19,10 @@ class HSLATest extends TestCase { public function testInterface() { - $hsl = new HSLA( - $hue = new Hue(150), - $saturation = new Saturation(42), - $lightness = new Lightness(24), + $hsl = HSLA::from( + $hue = Hue::at(150), + $saturation = Saturation::at(42), + $lightness = Lightness::at(24), ); $this->assertSame($hue, $hsl->hue()); @@ -31,11 +31,11 @@ public function testInterface() $this->assertSame(1.0, $hsl->alpha()->toFloat()); $this->assertSame('hsl(150, 42%, 24%)', $hsl->toString()); - $hsla = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), - $alpha = new Alpha(0.5), + $hsla = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), + $alpha = Alpha::of(0.5)->unwrap(), ); $this->assertSame($alpha, $hsla->alpha()); @@ -44,10 +44,10 @@ public function testInterface() public function testRotateBy() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); $hsl2 = $hsl->rotateBy(50); @@ -66,13 +66,13 @@ public function testRotateBy() public function testAddSaturation() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); - $hsl2 = $hsl->addSaturation(new Saturation(58)); + $hsl2 = $hsl->addSaturation(Saturation::at(58)); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); @@ -88,13 +88,13 @@ public function testAddSaturation() public function testSubtractSaturation() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); - $hsl2 = $hsl->SubtractSaturation(new Saturation(22)); + $hsl2 = $hsl->SubtractSaturation(Saturation::at(22)); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); @@ -110,13 +110,13 @@ public function testSubtractSaturation() public function testAddLightness() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); - $hsl2 = $hsl->addLightness(new Lightness(6)); + $hsl2 = $hsl->addLightness(Lightness::at(6)); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); @@ -132,13 +132,13 @@ public function testAddLightness() public function testSubtractLightness() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); - $hsl2 = $hsl->subtractLightness(new Lightness(22)); + $hsl2 = $hsl->subtractLightness(Lightness::at(22)); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); @@ -154,14 +154,14 @@ public function testSubtractLightness() public function testAddAlpha() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), - new Alpha(0.1), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), + Alpha::of(0.1)->unwrap(), ); - $hsl2 = $hsl->addALpha(new Alpha(0.1)); + $hsl2 = $hsl->addALpha(Alpha::of(0.1)->unwrap()); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); @@ -177,13 +177,13 @@ public function testAddAlpha() public function testSubtractAlpha() { - $hsl = new HSLA( - new Hue(150), - new Saturation(42), - new Lightness(24), + $hsl = HSLA::from( + Hue::at(150), + Saturation::at(42), + Lightness::at(24), ); - $hsl2 = $hsl->subtractAlpha(new Alpha(0.3)); + $hsl2 = $hsl->subtractAlpha(Alpha::of(0.3)->unwrap()); $this->assertInstanceOf(HSLA::class, $hsl2); $this->assertNotSame($hsl, $hsl2); diff --git a/tests/HueTest.php b/tests/HueTest.php index 7510fab..6f9d1bb 100644 --- a/tests/HueTest.php +++ b/tests/HueTest.php @@ -14,7 +14,7 @@ class HueTest extends TestCase { public function testInterface() { - $hue = new Hue(260); + $hue = Hue::at(260); $this->assertSame(260, $hue->toInt()); $this->assertSame('260', $hue->toString()); @@ -25,7 +25,7 @@ public function testThrowWhenValueTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-20'); - new Hue(-20); + Hue::at(-20)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -33,13 +33,13 @@ public function testThrowWhenValueTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('360'); - new Hue(360); + Hue::at(360)->unwrap(); } #[DataProvider('rotations')] public function testRotateBy($initial, $degrees, $expected) { - $hue = (new Hue($initial))->rotateBy($degrees); + $hue = Hue::at($initial)->rotateBy($degrees); $this->assertInstanceOf(Hue::class, $hue); $this->assertSame($expected, $hue->toInt()); @@ -57,7 +57,7 @@ public static function rotations(): array public function testOpposite() { - $hue = (new Hue(150))->opposite(); + $hue = Hue::at(150)->opposite(); $this->assertInstanceOf(Hue::class, $hue); $this->assertSame(330, $hue->toInt()); @@ -65,21 +65,21 @@ public function testOpposite() public function testEquals() { - $this->assertTrue((new Hue(50))->equals(new Hue(50))); - $this->assertFalse((new Hue(100))->equals(new Hue(50))); + $this->assertTrue(Hue::at(50)->equals(Hue::at(50))); + $this->assertFalse(Hue::at(100)->equals(Hue::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Hue(359))->atMaximum()); - $this->assertFalse((new Hue(0))->atMaximum()); - $this->assertFalse((new Hue(50))->atMaximum()); + $this->assertTrue(Hue::at(359)->atMaximum()); + $this->assertFalse(Hue::at(0)->atMaximum()); + $this->assertFalse(Hue::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Hue(359))->atMinimum()); - $this->assertTrue((new Hue(0))->atMinimum()); - $this->assertFalse((new Hue(50))->atMinimum()); + $this->assertFalse(Hue::at(359)->atMinimum()); + $this->assertTrue(Hue::at(0)->atMinimum()); + $this->assertFalse(Hue::at(50)->atMinimum()); } } diff --git a/tests/IntensityTest.php b/tests/IntensityTest.php index a65a889..e9dc134 100644 --- a/tests/IntensityTest.php +++ b/tests/IntensityTest.php @@ -13,7 +13,7 @@ class IntensityTest extends TestCase { public function testInterface() { - $intensity = new Intensity(42); + $intensity = Intensity::at(42); $this->assertSame(42, $intensity->toInt()); } @@ -23,7 +23,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-1'); - new Intensity(-1); + Intensity::of(-1)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -31,6 +31,6 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Intensity(101); + Intensity::of(101)->unwrap(); } } diff --git a/tests/LightnessTest.php b/tests/LightnessTest.php index 726edfd..997aab3 100644 --- a/tests/LightnessTest.php +++ b/tests/LightnessTest.php @@ -13,7 +13,7 @@ class LightnessTest extends TestCase { public function testInterface() { - $lightness = new Lightness(100); + $lightness = Lightness::at(100); $this->assertSame(100, $lightness->toInt()); $this->assertSame('100', $lightness->toString()); @@ -21,27 +21,27 @@ public function testInterface() public function testAdd() { - $lightness = (new Lightness(12))->add(new Lightness(30)); + $lightness = Lightness::at(12)->add(Lightness::at(30)); $this->assertInstanceOf(Lightness::class, $lightness); $this->assertSame(42, $lightness->toInt()); $this->assertSame( 100, - (new Lightness(50))->add(new Lightness(75))->toInt(), + Lightness::at(50)->add(Lightness::at(75))->toInt(), ); } public function testSub() { - $lightness = (new Lightness(54))->subtract(new Lightness(12)); + $lightness = Lightness::at(54)->subtract(Lightness::at(12)); $this->assertInstanceOf(Lightness::class, $lightness); $this->assertSame(42, $lightness->toInt()); $this->assertSame( 0, - (new Lightness(50))->subtract(new Lightness(75))->toInt(), + Lightness::at(50)->subtract(Lightness::at(75))->toInt(), ); } @@ -50,7 +50,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-42'); - new Lightness(-42); + Lightness::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -58,26 +58,26 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Lightness(101); + Lightness::of(101)->unwrap(); } public function testEquals() { - $this->assertTrue((new Lightness(50))->equals(new Lightness(50))); - $this->assertFalse((new Lightness(100))->equals(new Lightness(50))); + $this->assertTrue(Lightness::at(50)->equals(Lightness::at(50))); + $this->assertFalse(Lightness::at(100)->equals(Lightness::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Lightness(100))->atMaximum()); - $this->assertFalse((new Lightness(0))->atMaximum()); - $this->assertFalse((new Lightness(50))->atMaximum()); + $this->assertTrue(Lightness::at(100)->atMaximum()); + $this->assertFalse(Lightness::at(0)->atMaximum()); + $this->assertFalse(Lightness::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Lightness(100))->atMinimum()); - $this->assertTrue((new Lightness(0))->atMinimum()); - $this->assertFalse((new Lightness(50))->atMinimum()); + $this->assertFalse(Lightness::at(100)->atMinimum()); + $this->assertTrue(Lightness::at(0)->atMinimum()); + $this->assertFalse(Lightness::at(50)->atMinimum()); } } diff --git a/tests/MagentaTest.php b/tests/MagentaTest.php index 27540c9..afb1c20 100644 --- a/tests/MagentaTest.php +++ b/tests/MagentaTest.php @@ -13,7 +13,7 @@ class MagentaTest extends TestCase { public function testInterface() { - $magenta = new Magenta(50); + $magenta = Magenta::at(50); $this->assertSame(50, $magenta->toInt()); $this->assertSame('50', $magenta->toString()); @@ -24,7 +24,7 @@ public function testThrowWhenValueTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-1'); - new Magenta(-1); + Magenta::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -32,14 +32,14 @@ public function testThrowWhenValueTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Magenta(101); + Magenta::of(101)->unwrap(); } public function testAdd() { - $magenta = new Magenta(50); + $magenta = Magenta::at(50); - $magenta2 = $magenta->add(new Magenta(25)); + $magenta2 = $magenta->add(Magenta::at(25)); $this->assertInstanceOf(Magenta::class, $magenta2); $this->assertNotSame($magenta, $magenta2); @@ -49,9 +49,9 @@ public function testAdd() public function testSubtract() { - $magenta = new Magenta(50); + $magenta = Magenta::at(50); - $magenta2 = $magenta->subtract(new Magenta(25)); + $magenta2 = $magenta->subtract(Magenta::at(25)); $this->assertInstanceOf(Magenta::class, $magenta2); $this->assertNotSame($magenta, $magenta2); @@ -61,21 +61,21 @@ public function testSubtract() public function testEquals() { - $this->assertTrue((new Magenta(50))->equals(new Magenta(50))); - $this->assertFalse((new Magenta(100))->equals(new Magenta(50))); + $this->assertTrue(Magenta::at(50)->equals(Magenta::at(50))); + $this->assertFalse(Magenta::at(100)->equals(Magenta::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Magenta(100))->atMaximum()); - $this->assertFalse((new Magenta(0))->atMaximum()); - $this->assertFalse((new Magenta(50))->atMaximum()); + $this->assertTrue(Magenta::at(100)->atMaximum()); + $this->assertFalse(Magenta::at(0)->atMaximum()); + $this->assertFalse(Magenta::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Magenta(100))->atMinimum()); - $this->assertTrue((new Magenta(0))->atMinimum()); - $this->assertFalse((new Magenta(50))->atMinimum()); + $this->assertFalse(Magenta::at(100)->atMinimum()); + $this->assertTrue(Magenta::at(0)->atMinimum()); + $this->assertFalse(Magenta::at(50)->atMinimum()); } } diff --git a/tests/RGBATest.php b/tests/RGBATest.php index 692c781..6458bff 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -20,11 +20,11 @@ class RGBATest extends TestCase { public function testInterface() { - $rgba = new RGBA( - $red = new Red(0), - $green = new Green(255), - $blue = new Blue(122), - $alpha = new Alpha(0.5), + $rgba = RGBA::from( + $red = Red::at(0), + $green = Green::at(255), + $blue = Blue::at(122), + $alpha = Alpha::of(0.5)->unwrap(), ); $this->assertSame($red, $rgba->red()); @@ -36,10 +36,10 @@ public function testInterface() public function testWithoutAlpha() { - $rgba = new RGBA( - new Red(0), - new Green(255), - new Blue(122), + $rgba = RGBA::from( + Red::at(0), + Green::at(255), + Blue::at(122), ); $this->assertSame(1.0, $rgba->alpha()->toFloat()); @@ -47,25 +47,25 @@ public function testWithoutAlpha() public function testStringCast() { - $rgba = new RGBA( - new Red(0), - new Green(255), - new Blue(122), + $rgba = RGBA::from( + Red::at(0), + Green::at(255), + Blue::at(122), ); $this->assertSame('#00ff7a', $rgba->toString()); $this->assertSame( 'rgba(0, 255, 122, 0.5)', - $rgba->subtractAlpha(new Alpha(0.5))->toString(), + $rgba->subtractAlpha(Alpha::of(0.5)->unwrap())->toString(), ); } public function testToHexadecimal() { - $rgba = new RGBA( - new Red(0), - new Green(255), - new Blue(122), + $rgba = RGBA::from( + Red::at(0), + Green::at(255), + Blue::at(122), ); $this->assertSame( @@ -74,20 +74,20 @@ public function testToHexadecimal() ); $this->assertSame( '00ff7a80', - $rgba->subtractAlpha(new Alpha(0.5))->toHexadecimal(), + $rgba->subtractAlpha(Alpha::of(0.5)->unwrap())->toHexadecimal(), ); } public function testAddRed() { - $rgba = new RGBA( - new Red(10), - new Green(255), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(10), + Green::at(255), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->addRed(new Red(150)); + $rgba2 = $rgba->addRed(Red::at(150)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(10, $rgba->red()->toInt()); @@ -102,14 +102,14 @@ public function testAddRed() public function testSubtractRed() { - $rgba = new RGBA( - new Red(150), - new Green(255), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(150), + Green::at(255), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->subtractRed(new Red(10)); + $rgba2 = $rgba->subtractRed(Red::at(10)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(150, $rgba->red()->toInt()); @@ -124,14 +124,14 @@ public function testSubtractRed() public function testAddBlue() { - $rgba = new RGBA( - new Red(10), - new Green(255), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(10), + Green::at(255), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->addBlue(new Blue(12)); + $rgba2 = $rgba->addBlue(Blue::at(12)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(10, $rgba->red()->toInt()); @@ -146,14 +146,14 @@ public function testAddBlue() public function testSubtractBlue() { - $rgba = new RGBA( - new Red(10), - new Green(255), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(10), + Green::at(255), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->subtractBlue(new Blue(10)); + $rgba2 = $rgba->subtractBlue(Blue::at(10)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(10, $rgba->red()->toInt()); @@ -168,14 +168,14 @@ public function testSubtractBlue() public function testAddGreen() { - $rgba = new RGBA( - new Red(10), - new Green(205), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(10), + Green::at(205), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->addGreen(new Green(15)); + $rgba2 = $rgba->addGreen(Green::at(15)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(10, $rgba->red()->toInt()); @@ -190,14 +190,14 @@ public function testAddGreen() public function testSubtractGreen() { - $rgba = new RGBA( - new Red(10), - new Green(255), - new Blue(122), - new Alpha(0.5), + $rgba = RGBA::from( + Red::at(10), + Green::at(255), + Blue::at(122), + Alpha::of(0.5)->unwrap(), ); - $rgba2 = $rgba->subtractGreen(new Green(10)); + $rgba2 = $rgba->subtractGreen(Green::at(10)); $this->assertInstanceOf(RGBA::class, $rgba2); $this->assertNotSame($rgba, $rgba2); $this->assertSame(10, $rgba->red()->toInt()); diff --git a/tests/RedTest.php b/tests/RedTest.php index d5347ed..4f3a80c 100644 --- a/tests/RedTest.php +++ b/tests/RedTest.php @@ -14,7 +14,7 @@ class RedTest extends TestCase { public function testInterface() { - $red = new Red(255); + $red = Red::at(255); $this->assertSame(255, $red->toInt()); $this->assertSame('ff', $red->toString()); @@ -34,27 +34,27 @@ public function testFromHexadecimal() public function testAdd() { - $red = (new Red(12))->add(new Red(30)); + $red = Red::at(12)->add(Red::at(30)); $this->assertInstanceOf(Red::class, $red); $this->assertSame(42, $red->toInt()); $this->assertSame( 255, - (new Red(150))->add(new Red(150))->toInt(), + Red::at(150)->add(Red::at(150))->toInt(), ); } public function testSub() { - $red = (new Red(54))->subtract(new Red(12)); + $red = Red::at(54)->subtract(Red::at(12)); $this->assertInstanceOf(Red::class, $red); $this->assertSame(42, $red->toInt()); $this->assertSame( 0, - (new Red(150))->subtract(new Red(255))->toInt(), + Red::at(150)->subtract(Red::at(255))->toInt(), ); } @@ -63,7 +63,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-42'); - new Red(-42); + Red::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -71,26 +71,26 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('512'); - new Red(512); + Red::of(512)->unwrap(); } public function testAtMaximum() { - $this->assertTrue((new Red(255))->atMaximum()); - $this->assertFalse((new Red(0))->atMaximum()); - $this->assertFalse((new Red(122))->atMaximum()); + $this->assertTrue(Red::at(255)->atMaximum()); + $this->assertFalse(Red::at(0)->atMaximum()); + $this->assertFalse(Red::at(122)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Red(255))->atMinimum()); - $this->assertTrue((new Red(0))->atMinimum()); - $this->assertFalse((new Red(122))->atMinimum()); + $this->assertFalse(Red::at(255)->atMinimum()); + $this->assertTrue(Red::at(0)->atMinimum()); + $this->assertFalse(Red::at(122)->atMinimum()); } public function testFromIntensity() { - $red = Red::fromIntensity(new Intensity(100))->match( + $red = Red::fromIntensity(Intensity::at(100))->match( static fn($red) => $red, static fn() => null, ); @@ -99,28 +99,28 @@ public function testFromIntensity() $this->assertSame(255, $red->toInt()); $this->assertSame( 191, - Red::fromIntensity(new Intensity(75))->match( + Red::fromIntensity(Intensity::at(75))->match( static fn($red) => $red->toInt(), static fn() => null, ), ); $this->assertSame( 128, - Red::fromIntensity(new Intensity(50))->match( + Red::fromIntensity(Intensity::at(50))->match( static fn($red) => $red->toInt(), static fn() => null, ), ); $this->assertSame( 64, - Red::fromIntensity(new Intensity(25))->match( + Red::fromIntensity(Intensity::at(25))->match( static fn($red) => $red->toInt(), static fn() => null, ), ); $this->assertSame( 0, - Red::fromIntensity(new Intensity(0))->match( + Red::fromIntensity(Intensity::at(0))->match( static fn($red) => $red->toInt(), static fn() => null, ), @@ -129,7 +129,7 @@ public function testFromIntensity() public function testEquals() { - $this->assertTrue((new Red(50))->equals(new Red(50))); - $this->assertFalse((new Red(100))->equals(new Red(50))); + $this->assertTrue(Red::at(50)->equals(Red::at(50))); + $this->assertFalse(Red::at(100)->equals(Red::at(50))); } } diff --git a/tests/SaturationTest.php b/tests/SaturationTest.php index 0b1fba1..9a11ffb 100644 --- a/tests/SaturationTest.php +++ b/tests/SaturationTest.php @@ -13,7 +13,7 @@ class SaturationTest extends TestCase { public function testInterface() { - $saturation = new Saturation(100); + $saturation = Saturation::at(100); $this->assertSame(100, $saturation->toInt()); $this->assertSame('100', $saturation->toString()); @@ -21,27 +21,27 @@ public function testInterface() public function testAdd() { - $saturation = (new Saturation(12))->add(new Saturation(30)); + $saturation = Saturation::at(12)->add(Saturation::at(30)); $this->assertInstanceOf(Saturation::class, $saturation); $this->assertSame(42, $saturation->toInt()); $this->assertSame( 100, - (new Saturation(50))->add(new Saturation(75))->toInt(), + Saturation::at(50)->add(Saturation::at(75))->toInt(), ); } public function testSub() { - $saturation = (new Saturation(54))->subtract(new Saturation(12)); + $saturation = Saturation::at(54)->subtract(Saturation::at(12)); $this->assertInstanceOf(Saturation::class, $saturation); $this->assertSame(42, $saturation->toInt()); $this->assertSame( 0, - (new Saturation(50))->subtract(new Saturation(75))->toInt(), + Saturation::at(50)->subtract(Saturation::at(75))->toInt(), ); } @@ -50,7 +50,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-42'); - new Saturation(-42); + Saturation::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -58,26 +58,26 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Saturation(101); + Saturation::of(101)->unwrap(); } public function testEquals() { - $this->assertTrue((new Saturation(50))->equals(new Saturation(50))); - $this->assertFalse((new Saturation(100))->equals(new Saturation(50))); + $this->assertTrue(Saturation::at(50)->equals(Saturation::at(50))); + $this->assertFalse(Saturation::at(100)->equals(Saturation::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Saturation(100))->atMaximum()); - $this->assertFalse((new Saturation(0))->atMaximum()); - $this->assertFalse((new Saturation(50))->atMaximum()); + $this->assertTrue(Saturation::at(100)->atMaximum()); + $this->assertFalse(Saturation::at(0)->atMaximum()); + $this->assertFalse(Saturation::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Saturation(100))->atMinimum()); - $this->assertTrue((new Saturation(0))->atMinimum()); - $this->assertFalse((new Saturation(50))->atMinimum()); + $this->assertFalse(Saturation::at(100)->atMinimum()); + $this->assertTrue(Saturation::at(0)->atMinimum()); + $this->assertFalse(Saturation::at(50)->atMinimum()); } } diff --git a/tests/YellowTest.php b/tests/YellowTest.php index 70c828d..07cd0d8 100644 --- a/tests/YellowTest.php +++ b/tests/YellowTest.php @@ -13,7 +13,7 @@ class YellowTest extends TestCase { public function testInterface() { - $yellow = new Yellow(50); + $yellow = Yellow::at(50); $this->assertSame(50, $yellow->toInt()); $this->assertSame('50', $yellow->toString()); @@ -24,7 +24,7 @@ public function testThrowWhenValueTooLow() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('-1'); - new Yellow(-1); + Yellow::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -32,14 +32,14 @@ public function testThrowWhenValueTooHigh() $this->expectException(InvalidValueRangeException::class); $this->expectExceptionMessage('101'); - new Yellow(101); + Yellow::of(101)->unwrap(); } public function testAdd() { - $yellow = new Yellow(50); + $yellow = Yellow::at(50); - $yellow2 = $yellow->add(new Yellow(25)); + $yellow2 = $yellow->add(Yellow::at(25)); $this->assertInstanceOf(Yellow::class, $yellow2); $this->assertNotSame($yellow, $yellow2); @@ -49,9 +49,9 @@ public function testAdd() public function testSubtract() { - $yellow = new Yellow(50); + $yellow = Yellow::at(50); - $yellow2 = $yellow->subtract(new Yellow(25)); + $yellow2 = $yellow->subtract(Yellow::at(25)); $this->assertInstanceOf(Yellow::class, $yellow2); $this->assertNotSame($yellow, $yellow2); @@ -61,21 +61,21 @@ public function testSubtract() public function testEquals() { - $this->assertTrue((new Yellow(50))->equals(new Yellow(50))); - $this->assertFalse((new Yellow(100))->equals(new Yellow(50))); + $this->assertTrue(Yellow::at(50)->equals(Yellow::at(50))); + $this->assertFalse(Yellow::at(100)->equals(Yellow::at(50))); } public function testAtMaximum() { - $this->assertTrue((new Yellow(100))->atMaximum()); - $this->assertFalse((new Yellow(0))->atMaximum()); - $this->assertFalse((new Yellow(50))->atMaximum()); + $this->assertTrue(Yellow::at(100)->atMaximum()); + $this->assertFalse(Yellow::at(0)->atMaximum()); + $this->assertFalse(Yellow::at(50)->atMaximum()); } public function testAtMinimum() { - $this->assertFalse((new Yellow(100))->atMinimum()); - $this->assertTrue((new Yellow(0))->atMinimum()); - $this->assertFalse((new Yellow(50))->atMinimum()); + $this->assertFalse(Yellow::at(100)->atMinimum()); + $this->assertTrue(Yellow::at(0)->atMinimum()); + $this->assertFalse(Yellow::at(50)->atMinimum()); } } From 0301d2192f4a9537078f8b9ab5613eb4114a1080 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Tue, 4 Nov 2025 14:25:05 +0100 Subject: [PATCH 12/20] remove custom exceptions --- CHANGELOG.md | 6 ++++++ src/Alpha.php | 6 +----- src/Black.php | 6 +----- src/Blue.php | 6 +----- src/CMYKA.php | 5 ++--- src/Cyan.php | 6 +----- src/Exception/DomainException.php | 8 -------- src/Exception/Exception.php | 8 -------- src/Exception/InvalidValueRangeException.php | 8 -------- src/Green.php | 6 +----- src/HSLA.php | 5 ++--- src/Hue.php | 6 +----- src/Intensity.php | 6 +----- src/Lightness.php | 6 +----- src/Magenta.php | 6 +----- src/RGBA.php | 17 ++++++++--------- src/Red.php | 6 +----- src/Saturation.php | 6 +----- src/Yellow.php | 6 +----- tests/AlphaTest.php | 9 +++------ tests/BlackTest.php | 9 +++------ tests/BlueTest.php | 5 ++--- tests/ColourTest.php | 3 +-- tests/CyanTest.php | 9 +++------ tests/GreenTest.php | 5 ++--- tests/HueTest.php | 9 +++------ tests/IntensityTest.php | 9 +++------ tests/LightnessTest.php | 9 +++------ tests/MagentaTest.php | 9 +++------ tests/RGBATest.php | 9 ++++----- tests/RedTest.php | 5 ++--- tests/SaturationTest.php | 9 +++------ tests/YellowTest.php | 9 +++------ 33 files changed, 68 insertions(+), 169 deletions(-) delete mode 100644 src/Exception/DomainException.php delete mode 100644 src/Exception/Exception.php delete mode 100644 src/Exception/InvalidValueRangeException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5817660..b7c46ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ - `Innmind\Colour\HSLA` constructor is now private, use `::from()` instead - `Innmind\Colour\RGBA` constructor is now private, use `::from()` instead +### Removed + +- `Innmind\Colour\Exception\Exception` +- `Innmind\Colour\Exception\DomainException` +- `Innmind\Colour\Exception\InvalidValueRangeException` + ### Fixed - PHP `8.4` deprecations diff --git a/src/Alpha.php b/src/Alpha.php index 926194e..2fae613 100644 --- a/src/Alpha.php +++ b/src/Alpha.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Alpha { private float $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(float $value) { if ($value < 0 || $value > 1) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Black.php b/src/Black.php index fbc2363..51a582e 100644 --- a/src/Black.php +++ b/src/Black.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Black { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Blue.php b/src/Blue.php index 55e4f31..244fb6c 100644 --- a/src/Blue.php +++ b/src/Blue.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Blue { private int $integer; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $integer) { if ($integer < 0 || $integer > 255) { - throw new InvalidValueRangeException((string) $integer); + throw new \OutOfBoundsException((string) $integer); } $this->integer = $integer; diff --git a/src/CMYKA.php b/src/CMYKA.php index df1a150..fc7e005 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\DomainException; use Innmind\Immutable\{ Str, Maybe, @@ -318,7 +317,7 @@ private static function withAlpha(Str $colour): Attempt return Maybe::all($cyan, $magenta, $yellow, $black, $alpha) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -354,6 +353,6 @@ private static function withoutAlpha(Str $colour): Attempt return Maybe::all($cyan, $magenta, $yellow, $black) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } } diff --git a/src/Cyan.php b/src/Cyan.php index e7ebd23..2d9fee9 100644 --- a/src/Cyan.php +++ b/src/Cyan.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Cyan { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Exception/DomainException.php b/src/Exception/DomainException.php deleted file mode 100644 index ef1da27..0000000 --- a/src/Exception/DomainException.php +++ /dev/null @@ -1,8 +0,0 @@ - 255) { - throw new InvalidValueRangeException((string) $integer); + throw new \OutOfBoundsException((string) $integer); } $this->integer = $integer; diff --git a/src/HSLA.php b/src/HSLA.php index bfecd92..1804048 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\DomainException; use Innmind\Immutable\{ Str, Maybe, @@ -301,7 +300,7 @@ private static function withAlpha(Str $colour): Attempt return Maybe::all($hue, $saturation, $lightness, $alpha) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -332,6 +331,6 @@ private static function withoutAlpha(Str $colour): Attempt return Maybe::all($hue, $saturation, $lightness) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } } diff --git a/src/Hue.php b/src/Hue.php index 1f8d096..8838a6a 100644 --- a/src/Hue.php +++ b/src/Hue.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Hue { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 359) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Intensity.php b/src/Intensity.php index bae4b39..4a6a089 100644 --- a/src/Intensity.php +++ b/src/Intensity.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Intensity { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Lightness.php b/src/Lightness.php index 646a239..8b3e2b0 100644 --- a/src/Lightness.php +++ b/src/Lightness.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Lightness { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Magenta.php b/src/Magenta.php index 9258084..f1b2307 100644 --- a/src/Magenta.php +++ b/src/Magenta.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Magenta { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/RGBA.php b/src/RGBA.php index f434160..a454ab8 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\DomainException; use Innmind\Immutable\{ Str, Maybe, @@ -331,7 +330,7 @@ private static function fromHexadecimalWithAlpha(Str $colour): Attempt $colour->length() !== 8 ) { /** @var Attempt */ - return Attempt::error(new DomainException('Invalid length')); + return Attempt::error(new \DomainException('Invalid length')); } $matches = $colour @@ -352,7 +351,7 @@ private static function fromHexadecimalWithAlpha(Str $colour): Attempt return Maybe::all($red, $green, $blue, $alpha) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -371,7 +370,7 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt $colour->length() !== 6 ) { /** @var Attempt */ - return Attempt::error(new DomainException('Invalid length')); + return Attempt::error(new \DomainException('Invalid length')); } $matches = $colour @@ -389,7 +388,7 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt return Maybe::all($red, $green, $blue) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -432,7 +431,7 @@ private static function fromRGBFunctionWithPoints(Str $colour): Attempt return Maybe::all($red, $green, $blue) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -466,7 +465,7 @@ private static function fromRGBFunctionWithPercents(Str $colour): Attempt return Maybe::all($red, $green, $blue) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -514,7 +513,7 @@ private static function fromRGBAFunctionWithPoints(Str $colour): Attempt return Maybe::all($red, $green, $blue, $alpha) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } /** @@ -553,6 +552,6 @@ private static function fromRGBAFunctionWithPercents(Str $colour): Attempt return Maybe::all($red, $green, $blue, $alpha) ->map(self::from(...)) - ->attempt(static fn() => new DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException($colour->toString())); } } diff --git a/src/Red.php b/src/Red.php index 1c81bb4..fdfe558 100644 --- a/src/Red.php +++ b/src/Red.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Red { private int $integer; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $integer) { if ($integer < 0 || $integer > 255) { - throw new InvalidValueRangeException((string) $integer); + throw new \OutOfBoundsException((string) $integer); } $this->integer = $integer; diff --git a/src/Saturation.php b/src/Saturation.php index 7cece0b..c414733 100644 --- a/src/Saturation.php +++ b/src/Saturation.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Saturation { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/src/Yellow.php b/src/Yellow.php index c90810d..7ea0326 100644 --- a/src/Yellow.php +++ b/src/Yellow.php @@ -3,7 +3,6 @@ namespace Innmind\Colour; -use Innmind\Colour\Exception\InvalidValueRangeException; use Innmind\Immutable\Attempt; /** @@ -13,13 +12,10 @@ final class Yellow { private int $value; - /** - * @throws InvalidValueRangeException - */ private function __construct(int $value) { if ($value < 0 || $value > 100) { - throw new InvalidValueRangeException((string) $value); + throw new \OutOfBoundsException((string) $value); } $this->value = $value; diff --git a/tests/AlphaTest.php b/tests/AlphaTest.php index 0d4b05a..7688511 100644 --- a/tests/AlphaTest.php +++ b/tests/AlphaTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Alpha, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Alpha; use Innmind\BlackBox\PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -49,7 +46,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-0.1'); Alpha::of(-0.1)->unwrap(); @@ -57,7 +54,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('1.1'); Alpha::of(1.1)->unwrap(); diff --git a/tests/BlackTest.php b/tests/BlackTest.php index 50c426c..cbe3916 100644 --- a/tests/BlackTest.php +++ b/tests/BlackTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Black, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Black; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BlackTest extends TestCase @@ -21,7 +18,7 @@ public function testInterface() public function testThrowWhenValueTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); Black::of(-1)->unwrap(); @@ -29,7 +26,7 @@ public function testThrowWhenValueTooLow() public function testThrowWhenValueTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Black::of(101)->unwrap(); diff --git a/tests/BlueTest.php b/tests/BlueTest.php index 1c73507..fcc4ebb 100644 --- a/tests/BlueTest.php +++ b/tests/BlueTest.php @@ -6,7 +6,6 @@ use Innmind\Colour\{ Blue, Intensity, - Exception\InvalidValueRangeException, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -60,7 +59,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); Blue::of(-42)->unwrap(); @@ -68,7 +67,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); Blue::of(512)->unwrap(); diff --git a/tests/ColourTest.php b/tests/ColourTest.php index 9df14d7..1d5e784 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -8,7 +8,6 @@ RGBA, HSLA, CMYKA, - Exception\DomainException, }; use Innmind\BlackBox\{ PHPUnit\Framework\TestCase, @@ -51,7 +50,7 @@ public function testReturnNothingForRandomStrings() public function testThrowWhenNoFormatRecognized() { - $this->expectException(DomainException::class); + $this->expectException(\DomainException::class); $this->expectExceptionMessage('foo'); Colour::of('foo'); diff --git a/tests/CyanTest.php b/tests/CyanTest.php index 2be0b35..0480a25 100644 --- a/tests/CyanTest.php +++ b/tests/CyanTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Cyan, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Cyan; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CyanTest extends TestCase @@ -21,7 +18,7 @@ public function testInterface() public function testThrowWhenValueTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); Cyan::of(-1)->unwrap(); @@ -29,7 +26,7 @@ public function testThrowWhenValueTooLow() public function testThrowWhenValueTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Cyan::of(101)->unwrap(); diff --git a/tests/GreenTest.php b/tests/GreenTest.php index 7be13c3..809f54c 100644 --- a/tests/GreenTest.php +++ b/tests/GreenTest.php @@ -6,7 +6,6 @@ use Innmind\Colour\{ Green, Intensity, - Exception\InvalidValueRangeException, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -60,7 +59,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); Green::of(-42)->unwrap(); @@ -68,7 +67,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); Green::of(512)->unwrap(); diff --git a/tests/HueTest.php b/tests/HueTest.php index 6f9d1bb..bb75e49 100644 --- a/tests/HueTest.php +++ b/tests/HueTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Hue, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Hue; use Innmind\BlackBox\PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -22,7 +19,7 @@ public function testInterface() public function testThrowWhenValueTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-20'); Hue::at(-20)->unwrap(); @@ -30,7 +27,7 @@ public function testThrowWhenValueTooLow() public function testThrowWhenValueTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('360'); Hue::at(360)->unwrap(); diff --git a/tests/IntensityTest.php b/tests/IntensityTest.php index e9dc134..47e64f3 100644 --- a/tests/IntensityTest.php +++ b/tests/IntensityTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Intensity, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Intensity; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntensityTest extends TestCase @@ -20,7 +17,7 @@ public function testInterface() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); Intensity::of(-1)->unwrap(); @@ -28,7 +25,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Intensity::of(101)->unwrap(); diff --git a/tests/LightnessTest.php b/tests/LightnessTest.php index 997aab3..c877c51 100644 --- a/tests/LightnessTest.php +++ b/tests/LightnessTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Lightness, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Lightness; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LightnessTest extends TestCase @@ -47,7 +44,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); Lightness::of(-42)->unwrap(); @@ -55,7 +52,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Lightness::of(101)->unwrap(); diff --git a/tests/MagentaTest.php b/tests/MagentaTest.php index afb1c20..1d75f66 100644 --- a/tests/MagentaTest.php +++ b/tests/MagentaTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Magenta, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Magenta; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MagentaTest extends TestCase @@ -21,7 +18,7 @@ public function testInterface() public function testThrowWhenValueTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); Magenta::of(-1)->unwrap(); @@ -29,7 +26,7 @@ public function testThrowWhenValueTooLow() public function testThrowWhenValueTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Magenta::of(101)->unwrap(); diff --git a/tests/RGBATest.php b/tests/RGBATest.php index 6458bff..ad8d240 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -11,7 +11,6 @@ RGBA, HSLA, CMYKA, - Exception\DomainException, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -310,7 +309,7 @@ public function testFromRGBFunctionWithPoints() public function testThrowWhenInvalidRGBFunctionWithPoints() { - $this->expectException(DomainException::class); + $this->expectException(\DomainException::class); $this->expectExceptionMessage('rgb(10, 20%, 30)'); RGBA::of('rgb(10, 20%, 30)'); @@ -329,7 +328,7 @@ public function testFromRGBFunctionWithPercents() public function testThrowWhenInvalidRGBFunctionWithPercents() { - $this->expectException(DomainException::class); + $this->expectException(\DomainException::class); $this->expectExceptionMessage('rgb(10, 20%, 30)'); RGBA::of('rgb(10, 20%, 30)'); @@ -367,7 +366,7 @@ public function testFromRGBAFunctionWithPoints() public function testThrowWhenInvalidRGBAFunctionWithPoints() { - $this->expectException(DomainException::class); + $this->expectException(\DomainException::class); $this->expectExceptionMessage('rgba(10, 20%, 30, 2.0)'); RGBA::of('rgba(10, 20%, 30, 2.0)'); @@ -386,7 +385,7 @@ public function testFromRGBAFunctionWithPercents() public function testThrowWhenInvalidRGBAFunctionWithPercents() { - $this->expectException(DomainException::class); + $this->expectException(\DomainException::class); $this->expectExceptionMessage('rgba(10, 20%, 30, 1)'); RGBA::of('rgba(10, 20%, 30, 1)'); diff --git a/tests/RedTest.php b/tests/RedTest.php index 4f3a80c..4e48814 100644 --- a/tests/RedTest.php +++ b/tests/RedTest.php @@ -6,7 +6,6 @@ use Innmind\Colour\{ Red, Intensity, - Exception\InvalidValueRangeException, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -60,7 +59,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); Red::of(-42)->unwrap(); @@ -68,7 +67,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); Red::of(512)->unwrap(); diff --git a/tests/SaturationTest.php b/tests/SaturationTest.php index 9a11ffb..efa0e4e 100644 --- a/tests/SaturationTest.php +++ b/tests/SaturationTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Saturation, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Saturation; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SaturationTest extends TestCase @@ -47,7 +44,7 @@ public function testSub() public function testThrowWhenValueIsTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); Saturation::of(-42)->unwrap(); @@ -55,7 +52,7 @@ public function testThrowWhenValueIsTooLow() public function testThrowWhenValueIsTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Saturation::of(101)->unwrap(); diff --git a/tests/YellowTest.php b/tests/YellowTest.php index 07cd0d8..0062519 100644 --- a/tests/YellowTest.php +++ b/tests/YellowTest.php @@ -3,10 +3,7 @@ namespace Tests\Innmind\Colour; -use Innmind\Colour\{ - Yellow, - Exception\InvalidValueRangeException, -}; +use Innmind\Colour\Yellow; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class YellowTest extends TestCase @@ -21,7 +18,7 @@ public function testInterface() public function testThrowWhenValueTooLow() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); Yellow::of(-1)->unwrap(); @@ -29,7 +26,7 @@ public function testThrowWhenValueTooLow() public function testThrowWhenValueTooHigh() { - $this->expectException(InvalidValueRangeException::class); + $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); Yellow::of(101)->unwrap(); From 06c1f79c934e05be1175a9178a2a6d6c61d5cf25 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Tue, 4 Nov 2025 14:45:34 +0100 Subject: [PATCH 13/20] use promoted properties --- src/Black.php | 24 ++++++++++++++---------- src/Blue.php | 24 ++++++++++++++---------- src/CMYKA.php | 21 +++++---------------- src/Cyan.php | 24 ++++++++++++++---------- src/Green.php | 24 ++++++++++++++---------- src/HSLA.php | 17 ++++------------- src/Hue.php | 30 +++++++++++++++--------------- src/Intensity.php | 24 ++++++++++++++---------- src/Lightness.php | 24 ++++++++++++++---------- src/Magenta.php | 24 ++++++++++++++---------- src/RGBA.php | 17 ++++------------- src/Red.php | 24 ++++++++++++++---------- src/Saturation.php | 24 ++++++++++++++---------- src/Yellow.php | 24 ++++++++++++++---------- tests/HueTest.php | 4 ++-- 15 files changed, 170 insertions(+), 159 deletions(-) diff --git a/src/Black.php b/src/Black.php index 51a582e..66ac6f6 100644 --- a/src/Black.php +++ b/src/Black.php @@ -10,15 +10,12 @@ */ final class Black { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $black): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/Blue.php b/src/Blue.php index 244fb6c..69e4231 100644 --- a/src/Blue.php +++ b/src/Blue.php @@ -10,15 +10,12 @@ */ final class Blue { - private int $integer; - - private function __construct(int $integer) - { - if ($integer < 0 || $integer > 255) { - throw new \OutOfBoundsException((string) $integer); - } - - $this->integer = $integer; + /** + * @param int<0, 255> $integer + */ + private function __construct( + private int $integer, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 255) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } /** @@ -100,6 +101,9 @@ public function atMinimum(): bool return $this->integer === 0; } + /** + * @return int<0, 255> + */ public function toInt(): int { return $this->integer; diff --git a/src/CMYKA.php b/src/CMYKA.php index fc7e005..3f6b2da 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -17,24 +17,13 @@ final class CMYKA private const PATTERN_WITH_ALPHA = '~^device-cmyk\((?\d{1,3})%, ?(?\d{1,3})%, ?(?\d{1,3})%, ?(?\d{1,3})%, ?(?[01]|0?\.\d+|1\.0)\)$~'; private const PATTERN_WITHOUT_ALPHA = '~^device-cmyk\((?\d{1,3})%, ?(?\d{1,3})%, ?(?\d{1,3})%, ?(?\d{1,3})%\)$~'; - private Cyan $cyan; - private Magenta $magenta; - private Yellow $yellow; - private Black $black; - private Alpha $alpha; - private function __construct( - Cyan $cyan, - Magenta $magenta, - Yellow $yellow, - Black $black, - Alpha $alpha, + private Cyan $cyan, + private Magenta $magenta, + private Yellow $yellow, + private Black $black, + private Alpha $alpha, ) { - $this->cyan = $cyan; - $this->magenta = $magenta; - $this->yellow = $yellow; - $this->black = $black; - $this->alpha = $alpha; } /** diff --git a/src/Cyan.php b/src/Cyan.php index 2d9fee9..3c1402c 100644 --- a/src/Cyan.php +++ b/src/Cyan.php @@ -10,15 +10,12 @@ */ final class Cyan { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $cyan): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/Green.php b/src/Green.php index f072eaf..54a0273 100644 --- a/src/Green.php +++ b/src/Green.php @@ -10,15 +10,12 @@ */ final class Green { - private int $integer; - - private function __construct(int $integer) - { - if ($integer < 0 || $integer > 255) { - throw new \OutOfBoundsException((string) $integer); - } - - $this->integer = $integer; + /** + * @param int<0, 255> $integer + */ + private function __construct( + private int $integer, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 255) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } /** @@ -102,6 +103,9 @@ public function atMinimum(): bool return $this->integer === 0; } + /** + * @return int<0, 255> + */ public function toInt(): int { return $this->integer; diff --git a/src/HSLA.php b/src/HSLA.php index 1804048..5048829 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -17,21 +17,12 @@ final class HSLA private const PATTERN_WITH_ALPHA = '~^hsla\((?\d{1,3}), ?(?\d{1,3})%, ?(?\d{1,3})%, ?(?[01]|0?\.\d+|1\.0)\)$~'; private const PATTERN_WITHOUT_ALPHA = '~^hsl\((?\d{1,3}), ?(?\d{1,3})%, ?(?\d{1,3})%\)$~'; - private Hue $hue; - private Saturation $saturation; - private Lightness $lightness; - private Alpha $alpha; - private function __construct( - Hue $hue, - Saturation $saturation, - Lightness $lightness, - Alpha $alpha, + private Hue $hue, + private Saturation $saturation, + private Lightness $lightness, + private Alpha $alpha, ) { - $this->hue = $hue; - $this->saturation = $saturation; - $this->lightness = $lightness; - $this->alpha = $alpha; } /** diff --git a/src/Hue.php b/src/Hue.php index 8838a6a..bbd9bd0 100644 --- a/src/Hue.php +++ b/src/Hue.php @@ -10,15 +10,12 @@ */ final class Hue { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 359) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 359> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,21 +35,21 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 359) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function rotateBy(int $degrees): self { - $degrees = $this->value + $degrees; + $degrees = ($this->value + $degrees) % 360; if ($degrees < 0) { return new self(360 + $degrees); } - if ($degrees > 359) { - return new self($degrees - 360); - } - return new self($degrees); } @@ -76,6 +73,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 359> + */ public function toInt(): int { return $this->value; diff --git a/src/Intensity.php b/src/Intensity.php index 4a6a089..4a2e340 100644 --- a/src/Intensity.php +++ b/src/Intensity.php @@ -10,15 +10,12 @@ */ final class Intensity { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,9 +35,16 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/Lightness.php b/src/Lightness.php index 8b3e2b0..667460e 100644 --- a/src/Lightness.php +++ b/src/Lightness.php @@ -10,15 +10,12 @@ */ final class Lightness { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $lightness): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/Magenta.php b/src/Magenta.php index f1b2307..ab02abc 100644 --- a/src/Magenta.php +++ b/src/Magenta.php @@ -10,15 +10,12 @@ */ final class Magenta { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $magenta): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/RGBA.php b/src/RGBA.php index a454ab8..8bcf531 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -21,21 +21,12 @@ final class RGBA private const RGBA_FUNCTION_PATTERN = '~^rgba\((?\d{1,3}), ?(?\d{1,3}), ?(?\d{1,3}), ?(?[01]|0?\.\d+|1\.0)\)$~'; private const PERCENTED_RGBA_FUNCTION_PATTERN = '~^rgba\((?\d{1,3})%, ?(?\d{1,3})%, ?(?\d{1,3})%, ?(?[01]|0?\.\d+|1\.0)\)$~'; - private Red $red; - private Blue $blue; - private Green $green; - private Alpha $alpha; - private function __construct( - Red $red, - Green $green, - Blue $blue, - Alpha $alpha, + private Red $red, + private Green $green, + private Blue $blue, + private Alpha $alpha, ) { - $this->red = $red; - $this->blue = $blue; - $this->green = $green; - $this->alpha = $alpha; } /** diff --git a/src/Red.php b/src/Red.php index fdfe558..aa7b456 100644 --- a/src/Red.php +++ b/src/Red.php @@ -10,15 +10,12 @@ */ final class Red { - private int $integer; - - private function __construct(int $integer) - { - if ($integer < 0 || $integer > 255) { - throw new \OutOfBoundsException((string) $integer); - } - - $this->integer = $integer; + /** + * @param int<0, 255> $integer + */ + private function __construct( + private int $integer, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 255) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } /** @@ -102,6 +103,9 @@ public function atMinimum(): bool return $this->integer === 0; } + /** + * @return int<0, 255> + */ public function toInt(): int { return $this->integer; diff --git a/src/Saturation.php b/src/Saturation.php index c414733..ee3034f 100644 --- a/src/Saturation.php +++ b/src/Saturation.php @@ -10,15 +10,12 @@ */ final class Saturation { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $saturation): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/src/Yellow.php b/src/Yellow.php index 7ea0326..697d7e7 100644 --- a/src/Yellow.php +++ b/src/Yellow.php @@ -10,15 +10,12 @@ */ final class Yellow { - private int $value; - - private function __construct(int $value) - { - if ($value < 0 || $value > 100) { - throw new \OutOfBoundsException((string) $value); - } - - $this->value = $value; + /** + * @param int<0, 100> $value + */ + private function __construct( + private int $value, + ) { } /** @@ -38,7 +35,11 @@ public static function at(int $value): self */ public static function of(int $value): Attempt { - return Attempt::of(static fn() => new self($value)); + if ($value < 0 || $value > 100) { + return Attempt::error(new \OutOfBoundsException((string) $value)); + } + + return Attempt::result(new self($value)); } public function add(self $yellow): self @@ -76,6 +77,9 @@ public function atMinimum(): bool return $this->value === 0; } + /** + * @return int<0, 100> + */ public function toInt(): int { return $this->value; diff --git a/tests/HueTest.php b/tests/HueTest.php index bb75e49..01084d2 100644 --- a/tests/HueTest.php +++ b/tests/HueTest.php @@ -22,7 +22,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-20'); - Hue::at(-20)->unwrap(); + Hue::of(-20)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -30,7 +30,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('360'); - Hue::at(360)->unwrap(); + Hue::of(360)->unwrap(); } #[DataProvider('rotations')] From e3c3459e6dc450dfd06f704aa161b22e99895b62 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Tue, 4 Nov 2025 15:04:15 +0100 Subject: [PATCH 14/20] keep the detail of each failing part --- src/CMYKA.php | 68 ++++++++++++---- src/HSLA.php | 56 ++++++++++---- src/RGBA.php | 180 +++++++++++++++++++++++++++++++------------ tests/ColourTest.php | 2 +- tests/RGBATest.php | 8 +- 5 files changed, 226 insertions(+), 88 deletions(-) diff --git a/src/CMYKA.php b/src/CMYKA.php index 3f6b2da..5a75b93 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -282,31 +282,50 @@ private static function withAlpha(Str $colour): Attempt ->get('cyan') ->filter(static fn($cyan) => \is_numeric($cyan)) ->map(static fn($cyan) => (int) $cyan) - ->flatMap(static fn($cyan) => Cyan::of($cyan)->maybe()); + ->attempt(static fn() => new \DomainException("Cyan not found in '{$colour->toString()}'")) + ->flatMap(static fn($cyan) => Cyan::of($cyan)); $magenta = $matches ->get('magenta') ->filter(static fn($magenta) => \is_numeric($magenta)) ->map(static fn($magenta) => (int) $magenta) - ->flatMap(static fn($magenta) => Magenta::of($magenta)->maybe()); + ->attempt(static fn() => new \DomainException("Magenta not found in '{$colour->toString()}'")) + ->flatMap(static fn($magenta) => Magenta::of($magenta)); $yellow = $matches ->get('yellow') ->filter(static fn($yellow) => \is_numeric($yellow)) ->map(static fn($yellow) => (int) $yellow) - ->flatMap(static fn($yellow) => Yellow::of($yellow)->maybe()); + ->attempt(static fn() => new \DomainException("Yellow not found in '{$colour->toString()}'")) + ->flatMap(static fn($yellow) => Yellow::of($yellow)); $black = $matches ->get('black') ->filter(static fn($black) => \is_numeric($black)) ->map(static fn($black) => (int) $black) - ->flatMap(static fn($black) => Black::of($black)->maybe()); + ->attempt(static fn() => new \DomainException("Black not found in '{$colour->toString()}'")) + ->flatMap(static fn($black) => Black::of($black)); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); - - return Maybe::all($cyan, $magenta, $yellow, $black, $alpha) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) + ->flatMap(static fn($alpha) => Alpha::of($alpha)); + + return $cyan->flatMap( + static fn($cyan) => $magenta->flatMap( + static fn($magenta) => $yellow->flatMap( + static fn($yellow) => $black->flatMap( + static fn($black) => $alpha->map( + static fn($alpha) => self::from( + $cyan, + $magenta, + $yellow, + $black, + $alpha, + ), + ), + ), + ), + ), + ); } /** @@ -323,25 +342,40 @@ private static function withoutAlpha(Str $colour): Attempt ->get('cyan') ->filter(static fn($cyan) => \is_numeric($cyan)) ->map(static fn($cyan) => (int) $cyan) - ->flatMap(static fn($cyan) => Cyan::of($cyan)->maybe()); + ->attempt(static fn() => new \DomainException("Cyan not found in '{$colour->toString()}'")) + ->flatMap(static fn($cyan) => Cyan::of($cyan)); $magenta = $matches ->get('magenta') ->filter(static fn($magenta) => \is_numeric($magenta)) ->map(static fn($magenta) => (int) $magenta) - ->flatMap(static fn($magenta) => Magenta::of($magenta)->maybe()); + ->attempt(static fn() => new \DomainException("Magenta not found in '{$colour->toString()}'")) + ->flatMap(static fn($magenta) => Magenta::of($magenta)); $yellow = $matches ->get('yellow') ->filter(static fn($yellow) => \is_numeric($yellow)) ->map(static fn($yellow) => (int) $yellow) - ->flatMap(static fn($yellow) => Yellow::of($yellow)->maybe()); + ->attempt(static fn() => new \DomainException("Yellow not found in '{$colour->toString()}'")) + ->flatMap(static fn($yellow) => Yellow::of($yellow)); $black = $matches ->get('black') ->filter(static fn($black) => \is_numeric($black)) ->map(static fn($black) => (int) $black) - ->flatMap(static fn($black) => Black::of($black)->maybe()); - - return Maybe::all($cyan, $magenta, $yellow, $black) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Black not found in '{$colour->toString()}'")) + ->flatMap(static fn($black) => Black::of($black)); + + return $cyan->flatMap( + static fn($cyan) => $magenta->flatMap( + static fn($magenta) => $yellow->flatMap( + static fn($yellow) => $black->map( + static fn($black) => self::from( + $cyan, + $magenta, + $yellow, + $black, + ), + ), + ), + ), + ); } } diff --git a/src/HSLA.php b/src/HSLA.php index 5048829..bd8d906 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -272,26 +272,41 @@ private static function withAlpha(Str $colour): Attempt ->get('hue') ->filter(static fn($hue) => \is_numeric($hue)) ->map(static fn($hue) => (int) $hue) - ->flatMap(static fn($hue) => Hue::of($hue)->maybe()); + ->attempt(static fn() => new \DomainException("Hue not found in '{$colour->toString()}'")) + ->flatMap(static fn($hue) => Hue::of($hue)); $saturation = $matches ->get('saturation') ->filter(static fn($saturation) => \is_numeric($saturation)) ->map(static fn($saturation) => (int) $saturation) - ->flatMap(static fn($saturation) => Saturation::of($saturation)->maybe()); + ->attempt(static fn() => new \DomainException("Saturation not found in '{$colour->toString()}'")) + ->flatMap(static fn($saturation) => Saturation::of($saturation)); $lightness = $matches ->get('lightness') ->filter(static fn($lightness) => \is_numeric($lightness)) ->map(static fn($lightness) => (int) $lightness) - ->flatMap(static fn($lightness) => Lightness::of($lightness)->maybe()); + ->attempt(static fn() => new \DomainException("Lightness not found in '{$colour->toString()}'")) + ->flatMap(static fn($lightness) => Lightness::of($lightness)); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); - - return Maybe::all($hue, $saturation, $lightness, $alpha) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) + ->flatMap(static fn($alpha) => Alpha::of($alpha)); + + return $hue->flatMap( + static fn($hue) => $saturation->flatMap( + static fn($saturation) => $lightness->flatMap( + static fn($lightness) => $alpha->map( + static fn($alpha) => self::from( + $hue, + $saturation, + $lightness, + $alpha, + ), + ), + ), + ), + ); } /** @@ -308,20 +323,31 @@ private static function withoutAlpha(Str $colour): Attempt ->get('hue') ->filter(static fn($hue) => \is_numeric($hue)) ->map(static fn($hue) => (int) $hue) - ->flatMap(static fn($hue) => Hue::of($hue)->maybe()); + ->attempt(static fn() => new \DomainException("Hue not found in '{$colour->toString()}'")) + ->flatMap(static fn($hue) => Hue::of($hue)); $saturation = $matches ->get('saturation') ->filter(static fn($saturation) => \is_numeric($saturation)) ->map(static fn($saturation) => (int) $saturation) - ->flatMap(static fn($saturation) => Saturation::of($saturation)->maybe()); + ->attempt(static fn() => new \DomainException("Saturation not found in '{$colour->toString()}'")) + ->flatMap(static fn($saturation) => Saturation::of($saturation)); $lightness = $matches ->get('lightness') ->filter(static fn($lightness) => \is_numeric($lightness)) ->map(static fn($lightness) => (int) $lightness) - ->flatMap(static fn($lightness) => Lightness::of($lightness)->maybe()); - - return Maybe::all($hue, $saturation, $lightness) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Lightness not found in '{$colour->toString()}'")) + ->flatMap(static fn($lightness) => Lightness::of($lightness)); + + return $hue->flatMap( + static fn($hue) => $saturation->flatMap( + static fn($saturation) => $lightness->map( + static fn($lightness) => self::from( + $hue, + $saturation, + $lightness, + ), + ), + ), + ); } } diff --git a/src/RGBA.php b/src/RGBA.php index 8bcf531..24af352 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -329,20 +329,35 @@ private static function fromHexadecimalWithAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->flatMap(static fn($red) => Red::fromHexadecimal($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Red::fromHexadecimal($red)); $green = $matches ->get('green') - ->flatMap(static fn($green) => Green::fromHexadecimal($green)->maybe()); + ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Green::fromHexadecimal($green)); $blue = $matches ->get('blue') - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)->maybe()); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); $alpha = $matches ->get('alpha') - ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)->maybe()); - - return Maybe::all($red, $green, $blue, $alpha) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) + ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->flatMap( + static fn($blue) => $alpha->map( + static fn($alpha) => self::from( + $red, + $green, + $blue, + $alpha, + ), + ), + ), + ), + ); } /** @@ -369,17 +384,28 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->flatMap(static fn($red) => Red::fromHexadecimal($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Red::fromHexadecimal($red)); $green = $matches ->get('green') - ->flatMap(static fn($green) => Green::fromHexadecimal($green)->maybe()); + ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Green::fromHexadecimal($green)); $blue = $matches ->get('blue') - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)->maybe()); - - return Maybe::all($red, $green, $blue) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->map( + static fn($blue) => self::from( + $red, + $green, + $blue, + ), + ), + ), + ); } /** @@ -408,21 +434,32 @@ private static function fromRGBFunctionWithPoints(Str $colour): Attempt ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Red::of($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Red::of($red)); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Green::of($green)->maybe()); + ->attempt(static fn() => new \DomainException("Grren not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Green::of($green)); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Blue::of($blue)->maybe()); - - return Maybe::all($red, $green, $blue) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Blue::of($blue)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->map( + static fn($blue) => self::from( + $red, + $green, + $blue, + ), + ), + ), + ); } /** @@ -439,24 +476,35 @@ private static function fromRGBFunctionWithPercents(Str $colour): Attempt ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Intensity::of($red)->maybe()) - ->flatMap(static fn($red) => Red::fromIntensity($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Intensity::of($red)) + ->flatMap(static fn($red) => Red::fromIntensity($red)); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Intensity::of($green)->maybe()) - ->flatMap(static fn($green) => Green::fromIntensity($green)->maybe()); + ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Intensity::of($green)) + ->flatMap(static fn($green) => Green::fromIntensity($green)); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Intensity::of($blue)->maybe()) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)->maybe()); - - return Maybe::all($red, $green, $blue) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Intensity::of($blue)) + ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->map( + static fn($blue) => self::from( + $red, + $green, + $blue, + ), + ), + ), + ); } /** @@ -485,26 +533,41 @@ private static function fromRGBAFunctionWithPoints(Str $colour): Attempt ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Red::of($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Red::of($red)); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Green::of($green)->maybe()); + ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Green::of($green)); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Blue::of($blue)->maybe()); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Blue::of($blue)); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); - - return Maybe::all($red, $green, $blue, $alpha) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) + ->flatMap(static fn($alpha) => Alpha::of($alpha)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->flatMap( + static fn($blue) => $alpha->map( + static fn($alpha) => self::from( + $red, + $green, + $blue, + $alpha, + ), + ), + ), + ), + ); } /** @@ -521,28 +584,43 @@ private static function fromRGBAFunctionWithPercents(Str $colour): Attempt ->get('red') ->filter(static fn($red) => \is_numeric($red)) ->map(static fn($red) => (int) $red) - ->flatMap(static fn($red) => Intensity::of($red)->maybe()) - ->flatMap(static fn($red) => Red::fromIntensity($red)->maybe()); + ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) + ->flatMap(static fn($red) => Intensity::of($red)) + ->flatMap(static fn($red) => Red::fromIntensity($red)); $green = $matches ->get('green') ->filter(static fn($green) => \is_numeric($green)) ->map(static fn($green) => (int) $green) - ->flatMap(static fn($green) => Intensity::of($green)->maybe()) - ->flatMap(static fn($green) => Green::fromIntensity($green)->maybe()); + ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) + ->flatMap(static fn($green) => Intensity::of($green)) + ->flatMap(static fn($green) => Green::fromIntensity($green)); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) - ->flatMap(static fn($blue) => Intensity::of($blue)->maybe()) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)->maybe()); + ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) + ->flatMap(static fn($blue) => Intensity::of($blue)) + ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); $alpha = $matches ->get('alpha') ->filter(static fn($alpha) => \is_numeric($alpha)) ->map(static fn($alpha) => (float) $alpha) - ->flatMap(static fn($alpha) => Alpha::of($alpha)->maybe()); - - return Maybe::all($red, $green, $blue, $alpha) - ->map(self::from(...)) - ->attempt(static fn() => new \DomainException($colour->toString())); + ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) + ->flatMap(static fn($alpha) => Alpha::of($alpha)); + + return $red->flatMap( + static fn($red) => $green->flatMap( + static fn($green) => $blue->flatMap( + static fn($blue) => $alpha->map( + static fn($alpha) => self::from( + $red, + $green, + $blue, + $alpha, + ), + ), + ), + ), + ); } } diff --git a/tests/ColourTest.php b/tests/ColourTest.php index 1d5e784..adf97c8 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -51,7 +51,7 @@ public function testReturnNothingForRandomStrings() public function testThrowWhenNoFormatRecognized() { $this->expectException(\DomainException::class); - $this->expectExceptionMessage('foo'); + $this->expectExceptionMessage("Cyan not found in 'foo'"); Colour::of('foo'); } diff --git a/tests/RGBATest.php b/tests/RGBATest.php index ad8d240..df17164 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -310,7 +310,7 @@ public function testFromRGBFunctionWithPoints() public function testThrowWhenInvalidRGBFunctionWithPoints() { $this->expectException(\DomainException::class); - $this->expectExceptionMessage('rgb(10, 20%, 30)'); + $this->expectExceptionMessage("Red not found in 'rgb(10, 20%, 30)'"); RGBA::of('rgb(10, 20%, 30)'); } @@ -329,7 +329,7 @@ public function testFromRGBFunctionWithPercents() public function testThrowWhenInvalidRGBFunctionWithPercents() { $this->expectException(\DomainException::class); - $this->expectExceptionMessage('rgb(10, 20%, 30)'); + $this->expectExceptionMessage("Red not found in 'rgb(10, 20%, 30)'"); RGBA::of('rgb(10, 20%, 30)'); } @@ -367,7 +367,7 @@ public function testFromRGBAFunctionWithPoints() public function testThrowWhenInvalidRGBAFunctionWithPoints() { $this->expectException(\DomainException::class); - $this->expectExceptionMessage('rgba(10, 20%, 30, 2.0)'); + $this->expectExceptionMessage("Red not found in 'rgba(10, 20%, 30, 2.0)'"); RGBA::of('rgba(10, 20%, 30, 2.0)'); } @@ -386,7 +386,7 @@ public function testFromRGBAFunctionWithPercents() public function testThrowWhenInvalidRGBAFunctionWithPercents() { $this->expectException(\DomainException::class); - $this->expectExceptionMessage('rgba(10, 20%, 30, 1)'); + $this->expectExceptionMessage("Red not found in 'rgba(10, 20%, 30, 1)'"); RGBA::of('rgba(10, 20%, 30, 1)'); } From 86a22222beb8f09e4a9bb754e2b56d021ff481db Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Tue, 4 Nov 2025 15:09:40 +0100 Subject: [PATCH 15/20] CS --- src/CMYKA.php | 36 +++++++++++------------ src/HSLA.php | 28 +++++++++--------- src/RGBA.php | 80 +++++++++++++++++++++++++-------------------------- 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/src/CMYKA.php b/src/CMYKA.php index 5a75b93..472ad54 100644 --- a/src/CMYKA.php +++ b/src/CMYKA.php @@ -280,34 +280,34 @@ private static function withAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $cyan = $matches ->get('cyan') - ->filter(static fn($cyan) => \is_numeric($cyan)) + ->filter(\is_numeric(...)) ->map(static fn($cyan) => (int) $cyan) ->attempt(static fn() => new \DomainException("Cyan not found in '{$colour->toString()}'")) - ->flatMap(static fn($cyan) => Cyan::of($cyan)); + ->flatMap(Cyan::of(...)); $magenta = $matches ->get('magenta') - ->filter(static fn($magenta) => \is_numeric($magenta)) + ->filter(\is_numeric(...)) ->map(static fn($magenta) => (int) $magenta) ->attempt(static fn() => new \DomainException("Magenta not found in '{$colour->toString()}'")) - ->flatMap(static fn($magenta) => Magenta::of($magenta)); + ->flatMap(Magenta::of(...)); $yellow = $matches ->get('yellow') - ->filter(static fn($yellow) => \is_numeric($yellow)) + ->filter(\is_numeric(...)) ->map(static fn($yellow) => (int) $yellow) ->attempt(static fn() => new \DomainException("Yellow not found in '{$colour->toString()}'")) - ->flatMap(static fn($yellow) => Yellow::of($yellow)); + ->flatMap(Yellow::of(...)); $black = $matches ->get('black') - ->filter(static fn($black) => \is_numeric($black)) + ->filter(\is_numeric(...)) ->map(static fn($black) => (int) $black) ->attempt(static fn() => new \DomainException("Black not found in '{$colour->toString()}'")) - ->flatMap(static fn($black) => Black::of($black)); + ->flatMap(Black::of(...)); $alpha = $matches ->get('alpha') - ->filter(static fn($alpha) => \is_numeric($alpha)) + ->filter(\is_numeric(...)) ->map(static fn($alpha) => (float) $alpha) ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); + ->flatMap(Alpha::of(...)); return $cyan->flatMap( static fn($cyan) => $magenta->flatMap( @@ -340,28 +340,28 @@ private static function withoutAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $cyan = $matches ->get('cyan') - ->filter(static fn($cyan) => \is_numeric($cyan)) + ->filter(\is_numeric(...)) ->map(static fn($cyan) => (int) $cyan) ->attempt(static fn() => new \DomainException("Cyan not found in '{$colour->toString()}'")) - ->flatMap(static fn($cyan) => Cyan::of($cyan)); + ->flatMap(Cyan::of(...)); $magenta = $matches ->get('magenta') - ->filter(static fn($magenta) => \is_numeric($magenta)) + ->filter(\is_numeric(...)) ->map(static fn($magenta) => (int) $magenta) ->attempt(static fn() => new \DomainException("Magenta not found in '{$colour->toString()}'")) - ->flatMap(static fn($magenta) => Magenta::of($magenta)); + ->flatMap(Magenta::of(...)); $yellow = $matches ->get('yellow') - ->filter(static fn($yellow) => \is_numeric($yellow)) + ->filter(\is_numeric(...)) ->map(static fn($yellow) => (int) $yellow) ->attempt(static fn() => new \DomainException("Yellow not found in '{$colour->toString()}'")) - ->flatMap(static fn($yellow) => Yellow::of($yellow)); + ->flatMap(Yellow::of(...)); $black = $matches ->get('black') - ->filter(static fn($black) => \is_numeric($black)) + ->filter(\is_numeric(...)) ->map(static fn($black) => (int) $black) ->attempt(static fn() => new \DomainException("Black not found in '{$colour->toString()}'")) - ->flatMap(static fn($black) => Black::of($black)); + ->flatMap(Black::of(...)); return $cyan->flatMap( static fn($cyan) => $magenta->flatMap( diff --git a/src/HSLA.php b/src/HSLA.php index bd8d906..94e507b 100644 --- a/src/HSLA.php +++ b/src/HSLA.php @@ -270,28 +270,28 @@ private static function withAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $hue = $matches ->get('hue') - ->filter(static fn($hue) => \is_numeric($hue)) + ->filter(\is_numeric(...)) ->map(static fn($hue) => (int) $hue) ->attempt(static fn() => new \DomainException("Hue not found in '{$colour->toString()}'")) - ->flatMap(static fn($hue) => Hue::of($hue)); + ->flatMap(Hue::of(...)); $saturation = $matches ->get('saturation') - ->filter(static fn($saturation) => \is_numeric($saturation)) + ->filter(\is_numeric(...)) ->map(static fn($saturation) => (int) $saturation) ->attempt(static fn() => new \DomainException("Saturation not found in '{$colour->toString()}'")) - ->flatMap(static fn($saturation) => Saturation::of($saturation)); + ->flatMap(Saturation::of(...)); $lightness = $matches ->get('lightness') - ->filter(static fn($lightness) => \is_numeric($lightness)) + ->filter(\is_numeric(...)) ->map(static fn($lightness) => (int) $lightness) ->attempt(static fn() => new \DomainException("Lightness not found in '{$colour->toString()}'")) - ->flatMap(static fn($lightness) => Lightness::of($lightness)); + ->flatMap(Lightness::of(...)); $alpha = $matches ->get('alpha') - ->filter(static fn($alpha) => \is_numeric($alpha)) + ->filter(\is_numeric(...)) ->map(static fn($alpha) => (float) $alpha) ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); + ->flatMap(Alpha::of(...)); return $hue->flatMap( static fn($hue) => $saturation->flatMap( @@ -321,22 +321,22 @@ private static function withoutAlpha(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $hue = $matches ->get('hue') - ->filter(static fn($hue) => \is_numeric($hue)) + ->filter(\is_numeric(...)) ->map(static fn($hue) => (int) $hue) ->attempt(static fn() => new \DomainException("Hue not found in '{$colour->toString()}'")) - ->flatMap(static fn($hue) => Hue::of($hue)); + ->flatMap(Hue::of(...)); $saturation = $matches ->get('saturation') - ->filter(static fn($saturation) => \is_numeric($saturation)) + ->filter(\is_numeric(...)) ->map(static fn($saturation) => (int) $saturation) ->attempt(static fn() => new \DomainException("Saturation not found in '{$colour->toString()}'")) - ->flatMap(static fn($saturation) => Saturation::of($saturation)); + ->flatMap(Saturation::of(...)); $lightness = $matches ->get('lightness') - ->filter(static fn($lightness) => \is_numeric($lightness)) + ->filter(\is_numeric(...)) ->map(static fn($lightness) => (int) $lightness) ->attempt(static fn() => new \DomainException("Lightness not found in '{$colour->toString()}'")) - ->flatMap(static fn($lightness) => Lightness::of($lightness)); + ->flatMap(Lightness::of(...)); return $hue->flatMap( static fn($hue) => $saturation->flatMap( diff --git a/src/RGBA.php b/src/RGBA.php index 24af352..c8a2763 100644 --- a/src/RGBA.php +++ b/src/RGBA.php @@ -330,19 +330,19 @@ private static function fromHexadecimalWithAlpha(Str $colour): Attempt $red = $matches ->get('red') ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Red::fromHexadecimal($red)); + ->flatMap(Red::fromHexadecimal(...)); $green = $matches ->get('green') ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Green::fromHexadecimal($green)); + ->flatMap(Green::fromHexadecimal(...)); $blue = $matches ->get('blue') ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); + ->flatMap(Blue::fromHexadecimal(...)); $alpha = $matches ->get('alpha') ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) - ->flatMap(static fn($alpha) => Alpha::fromHexadecimal($alpha)); + ->flatMap(Alpha::fromHexadecimal(...)); return $red->flatMap( static fn($red) => $green->flatMap( @@ -385,15 +385,15 @@ private static function fromHexadecimalWithoutAlpha(Str $colour): Attempt $red = $matches ->get('red') ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Red::fromHexadecimal($red)); + ->flatMap(Red::fromHexadecimal(...)); $green = $matches ->get('green') ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Green::fromHexadecimal($green)); + ->flatMap(Green::fromHexadecimal(...)); $blue = $matches ->get('blue') ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Blue::fromHexadecimal($blue)); + ->flatMap(Blue::fromHexadecimal(...)); return $red->flatMap( static fn($red) => $green->flatMap( @@ -432,22 +432,22 @@ private static function fromRGBFunctionWithPoints(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->filter(static fn($red) => \is_numeric($red)) + ->filter(\is_numeric(...)) ->map(static fn($red) => (int) $red) ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Red::of($red)); + ->flatMap(Red::of(...)); $green = $matches ->get('green') - ->filter(static fn($green) => \is_numeric($green)) + ->filter(\is_numeric(...)) ->map(static fn($green) => (int) $green) ->attempt(static fn() => new \DomainException("Grren not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Green::of($green)); + ->flatMap(Green::of(...)); $blue = $matches ->get('blue') - ->filter(static fn($blue) => \is_numeric($blue)) + ->filter(\is_numeric(...)) ->map(static fn($blue) => (int) $blue) ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Blue::of($blue)); + ->flatMap(Blue::of(...)); return $red->flatMap( static fn($red) => $green->flatMap( @@ -474,25 +474,25 @@ private static function fromRGBFunctionWithPercents(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->filter(static fn($red) => \is_numeric($red)) + ->filter(\is_numeric(...)) ->map(static fn($red) => (int) $red) ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Intensity::of($red)) - ->flatMap(static fn($red) => Red::fromIntensity($red)); + ->flatMap(Intensity::of(...)) + ->flatMap(Red::fromIntensity(...)); $green = $matches ->get('green') - ->filter(static fn($green) => \is_numeric($green)) + ->filter(\is_numeric(...)) ->map(static fn($green) => (int) $green) ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Intensity::of($green)) - ->flatMap(static fn($green) => Green::fromIntensity($green)); + ->flatMap(Intensity::of(...)) + ->flatMap(Green::fromIntensity(...)); $blue = $matches ->get('blue') - ->filter(static fn($blue) => \is_numeric($blue)) + ->filter(\is_numeric(...)) ->map(static fn($blue) => (int) $blue) ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Intensity::of($blue)) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); + ->flatMap(Intensity::of(...)) + ->flatMap(Blue::fromIntensity(...)); return $red->flatMap( static fn($red) => $green->flatMap( @@ -531,28 +531,28 @@ private static function fromRGBAFunctionWithPoints(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->filter(static fn($red) => \is_numeric($red)) + ->filter(\is_numeric(...)) ->map(static fn($red) => (int) $red) ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Red::of($red)); + ->flatMap(Red::of(...)); $green = $matches ->get('green') - ->filter(static fn($green) => \is_numeric($green)) + ->filter(\is_numeric(...)) ->map(static fn($green) => (int) $green) ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Green::of($green)); + ->flatMap(Green::of(...)); $blue = $matches ->get('blue') ->filter(static fn($blue) => \is_numeric($blue)) ->map(static fn($blue) => (int) $blue) ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Blue::of($blue)); + ->flatMap(Blue::of(...)); $alpha = $matches ->get('alpha') - ->filter(static fn($alpha) => \is_numeric($alpha)) + ->filter(\is_numeric(...)) ->map(static fn($alpha) => (float) $alpha) ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); + ->flatMap(Alpha::of(...)); return $red->flatMap( static fn($red) => $green->flatMap( @@ -582,31 +582,31 @@ private static function fromRGBAFunctionWithPercents(Str $colour): Attempt ->map(static fn($_, $match) => $match->toString()); $red = $matches ->get('red') - ->filter(static fn($red) => \is_numeric($red)) + ->filter(\is_numeric(...)) ->map(static fn($red) => (int) $red) ->attempt(static fn() => new \DomainException("Red not found in '{$colour->toString()}'")) - ->flatMap(static fn($red) => Intensity::of($red)) - ->flatMap(static fn($red) => Red::fromIntensity($red)); + ->flatMap(Intensity::of(...)) + ->flatMap(Red::fromIntensity(...)); $green = $matches ->get('green') - ->filter(static fn($green) => \is_numeric($green)) + ->filter(\is_numeric(...)) ->map(static fn($green) => (int) $green) ->attempt(static fn() => new \DomainException("Green not found in '{$colour->toString()}'")) - ->flatMap(static fn($green) => Intensity::of($green)) - ->flatMap(static fn($green) => Green::fromIntensity($green)); + ->flatMap(Intensity::of(...)) + ->flatMap(Green::fromIntensity(...)); $blue = $matches ->get('blue') - ->filter(static fn($blue) => \is_numeric($blue)) + ->filter(\is_numeric(...)) ->map(static fn($blue) => (int) $blue) ->attempt(static fn() => new \DomainException("Blue not found in '{$colour->toString()}'")) - ->flatMap(static fn($blue) => Intensity::of($blue)) - ->flatMap(static fn($blue) => Blue::fromIntensity($blue)); + ->flatMap(Intensity::of(...)) + ->flatMap(Blue::fromIntensity(...)); $alpha = $matches ->get('alpha') - ->filter(static fn($alpha) => \is_numeric($alpha)) + ->filter(\is_numeric(...)) ->map(static fn($alpha) => (float) $alpha) ->attempt(static fn() => new \DomainException("Alpha not found in '{$colour->toString()}'")) - ->flatMap(static fn($alpha) => Alpha::of($alpha)); + ->flatMap(Alpha::of(...)); return $red->flatMap( static fn($red) => $green->flatMap( From 3b2ded0c78422451260e819508e5a15afa6f7d4d Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 25 Jan 2026 14:47:34 +0100 Subject: [PATCH 16/20] tag dependencies --- .github/workflows/ci.yml | 8 ++++---- composer.json | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 779f162..2f3eecb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,11 +4,11 @@ on: [push, pull_request] jobs: blackbox: - uses: innmind/github-workflows/.github/workflows/black-box-matrix.yml@next + uses: innmind/github-workflows/.github/workflows/black-box-matrix.yml@main coverage: - uses: innmind/github-workflows/.github/workflows/coverage-matrix.yml@next + uses: innmind/github-workflows/.github/workflows/coverage-matrix.yml@main secrets: inherit psalm: - uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@next + uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main cs: - uses: innmind/github-workflows/.github/workflows/cs.yml@next + uses: innmind/github-workflows/.github/workflows/cs.yml@main diff --git a/composer.json b/composer.json index 8d70bf5..7ab0468 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require": { "php": "~8.4", - "innmind/immutable": "dev-next" + "innmind/immutable": "~6.0" }, "autoload": { "psr-4": { @@ -30,7 +30,7 @@ } }, "require-dev": { - "innmind/static-analysis": "^1.2.1", + "innmind/static-analysis": "~1.3", "innmind/black-box": "~6.5", "innmind/coding-standard": "~2.0" }, From 8c72f9f2dd028e9738b9b62fadd2910e72d3f415 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 25 Jan 2026 14:50:56 +0100 Subject: [PATCH 17/20] fix warnings --- tests/AlphaTest.php | 4 ++-- tests/BlackTest.php | 4 ++-- tests/BlueTest.php | 4 ++-- tests/ColourTest.php | 2 +- tests/CyanTest.php | 4 ++-- tests/GreenTest.php | 4 ++-- tests/HueTest.php | 4 ++-- tests/IntensityTest.php | 4 ++-- tests/LightnessTest.php | 4 ++-- tests/MagentaTest.php | 4 ++-- tests/RGBATest.php | 4 ++-- tests/RedTest.php | 4 ++-- tests/SaturationTest.php | 4 ++-- tests/YellowTest.php | 4 ++-- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/AlphaTest.php b/tests/AlphaTest.php index 7688511..2806c6b 100644 --- a/tests/AlphaTest.php +++ b/tests/AlphaTest.php @@ -49,7 +49,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-0.1'); - Alpha::of(-0.1)->unwrap(); + $_ = Alpha::of(-0.1)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -57,7 +57,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('1.1'); - Alpha::of(1.1)->unwrap(); + $_ = Alpha::of(1.1)->unwrap(); } public function testAtMaximum() diff --git a/tests/BlackTest.php b/tests/BlackTest.php index cbe3916..132ad72 100644 --- a/tests/BlackTest.php +++ b/tests/BlackTest.php @@ -21,7 +21,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); - Black::of(-1)->unwrap(); + $_ = Black::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -29,7 +29,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Black::of(101)->unwrap(); + $_ = Black::of(101)->unwrap(); } public function testAdd() diff --git a/tests/BlueTest.php b/tests/BlueTest.php index fcc4ebb..14836b3 100644 --- a/tests/BlueTest.php +++ b/tests/BlueTest.php @@ -62,7 +62,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); - Blue::of(-42)->unwrap(); + $_ = Blue::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -70,7 +70,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); - Blue::of(512)->unwrap(); + $_ = Blue::of(512)->unwrap(); } public function testAtMaximum() diff --git a/tests/ColourTest.php b/tests/ColourTest.php index adf97c8..78938d3 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -53,7 +53,7 @@ public function testThrowWhenNoFormatRecognized() $this->expectException(\DomainException::class); $this->expectExceptionMessage("Cyan not found in 'foo'"); - Colour::of('foo'); + $_ = Colour::of('foo'); } public function testLiterals() diff --git a/tests/CyanTest.php b/tests/CyanTest.php index 0480a25..b9bb10d 100644 --- a/tests/CyanTest.php +++ b/tests/CyanTest.php @@ -21,7 +21,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); - Cyan::of(-1)->unwrap(); + $_ = Cyan::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -29,7 +29,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Cyan::of(101)->unwrap(); + $_ = Cyan::of(101)->unwrap(); } public function testAdd() diff --git a/tests/GreenTest.php b/tests/GreenTest.php index 809f54c..025aaee 100644 --- a/tests/GreenTest.php +++ b/tests/GreenTest.php @@ -62,7 +62,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); - Green::of(-42)->unwrap(); + $_ = Green::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -70,7 +70,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); - Green::of(512)->unwrap(); + $_ = Green::of(512)->unwrap(); } public function testAtMaximum() diff --git a/tests/HueTest.php b/tests/HueTest.php index 01084d2..8536b56 100644 --- a/tests/HueTest.php +++ b/tests/HueTest.php @@ -22,7 +22,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-20'); - Hue::of(-20)->unwrap(); + $_ = Hue::of(-20)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -30,7 +30,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('360'); - Hue::of(360)->unwrap(); + $_ = Hue::of(360)->unwrap(); } #[DataProvider('rotations')] diff --git a/tests/IntensityTest.php b/tests/IntensityTest.php index 47e64f3..532de5b 100644 --- a/tests/IntensityTest.php +++ b/tests/IntensityTest.php @@ -20,7 +20,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); - Intensity::of(-1)->unwrap(); + $_ = Intensity::of(-1)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -28,6 +28,6 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Intensity::of(101)->unwrap(); + $_ = Intensity::of(101)->unwrap(); } } diff --git a/tests/LightnessTest.php b/tests/LightnessTest.php index c877c51..80014af 100644 --- a/tests/LightnessTest.php +++ b/tests/LightnessTest.php @@ -47,7 +47,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); - Lightness::of(-42)->unwrap(); + $_ = Lightness::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -55,7 +55,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Lightness::of(101)->unwrap(); + $_ = Lightness::of(101)->unwrap(); } public function testEquals() diff --git a/tests/MagentaTest.php b/tests/MagentaTest.php index 1d75f66..c70346f 100644 --- a/tests/MagentaTest.php +++ b/tests/MagentaTest.php @@ -21,7 +21,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); - Magenta::of(-1)->unwrap(); + $_ = Magenta::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -29,7 +29,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Magenta::of(101)->unwrap(); + $_ = Magenta::of(101)->unwrap(); } public function testAdd() diff --git a/tests/RGBATest.php b/tests/RGBATest.php index df17164..4c06739 100644 --- a/tests/RGBATest.php +++ b/tests/RGBATest.php @@ -312,7 +312,7 @@ public function testThrowWhenInvalidRGBFunctionWithPoints() $this->expectException(\DomainException::class); $this->expectExceptionMessage("Red not found in 'rgb(10, 20%, 30)'"); - RGBA::of('rgb(10, 20%, 30)'); + $_ = RGBA::of('rgb(10, 20%, 30)'); } public function testFromRGBFunctionWithPercents() @@ -331,7 +331,7 @@ public function testThrowWhenInvalidRGBFunctionWithPercents() $this->expectException(\DomainException::class); $this->expectExceptionMessage("Red not found in 'rgb(10, 20%, 30)'"); - RGBA::of('rgb(10, 20%, 30)'); + $_ = RGBA::of('rgb(10, 20%, 30)'); } public function testFromRGBFunction() diff --git a/tests/RedTest.php b/tests/RedTest.php index 4e48814..e5532a3 100644 --- a/tests/RedTest.php +++ b/tests/RedTest.php @@ -62,7 +62,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); - Red::of(-42)->unwrap(); + $_ = Red::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -70,7 +70,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('512'); - Red::of(512)->unwrap(); + $_ = Red::of(512)->unwrap(); } public function testAtMaximum() diff --git a/tests/SaturationTest.php b/tests/SaturationTest.php index efa0e4e..a468525 100644 --- a/tests/SaturationTest.php +++ b/tests/SaturationTest.php @@ -47,7 +47,7 @@ public function testThrowWhenValueIsTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-42'); - Saturation::of(-42)->unwrap(); + $_ = Saturation::of(-42)->unwrap(); } public function testThrowWhenValueIsTooHigh() @@ -55,7 +55,7 @@ public function testThrowWhenValueIsTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Saturation::of(101)->unwrap(); + $_ = Saturation::of(101)->unwrap(); } public function testEquals() diff --git a/tests/YellowTest.php b/tests/YellowTest.php index 0062519..68ddaf9 100644 --- a/tests/YellowTest.php +++ b/tests/YellowTest.php @@ -21,7 +21,7 @@ public function testThrowWhenValueTooLow() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('-1'); - Yellow::of(-1)->unwrap(); + $_ = Yellow::of(-1)->unwrap(); } public function testThrowWhenValueTooHigh() @@ -29,7 +29,7 @@ public function testThrowWhenValueTooHigh() $this->expectException(\OutOfBoundsException::class); $this->expectExceptionMessage('101'); - Yellow::of(101)->unwrap(); + $_ = Yellow::of(101)->unwrap(); } public function testAdd() From b411255332dfa2c9da9585b3448f3abe0e18ef57 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 25 Jan 2026 14:52:01 +0100 Subject: [PATCH 18/20] add extensive CI --- .github/workflows/extensive.yml | 12 ++++++++++++ blackbox.php | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 .github/workflows/extensive.yml diff --git a/.github/workflows/extensive.yml b/.github/workflows/extensive.yml new file mode 100644 index 0000000..257f139 --- /dev/null +++ b/.github/workflows/extensive.yml @@ -0,0 +1,12 @@ +name: Extensive CI + +on: + push: + tags: + - '*' + paths: + - '.github/workflows/extensive.yml' + +jobs: + blackbox: + uses: innmind/github-workflows/.github/workflows/extensive.yml@main diff --git a/blackbox.php b/blackbox.php index 31365e1..d6f7152 100644 --- a/blackbox.php +++ b/blackbox.php @@ -10,6 +10,10 @@ }; Application::new($argv) + ->when( + \getenv('BLACKBOX_SET_SIZE') !== false, + static fn(Application $app) => $app->scenariiPerProof((int) \getenv('BLACKBOX_SET_SIZE')), + ) ->when( \getenv('ENABLE_COVERAGE') !== false, static fn(Application $app) => $app From a868d52d03ff09bf834a64380ccf6b0af9aa1bb7 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 25 Jan 2026 15:04:35 +0100 Subject: [PATCH 19/20] update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7c46ca..6ffd95e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## [Unreleased] +### Added + +- `Innmind\Colour\CMYKA::attempt()` +- `Innmind\Colour\Colour::attempt()` +- `Innmind\Colour\HSLA::attempt()` +- `Innmind\Colour\RGBA::attempt()` + ### Changed - Requires PHP `8.4` From 7176e07343ec7af0d7164114b43f273e31850ffa Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 25 Jan 2026 15:17:46 +0100 Subject: [PATCH 20/20] make sure all colours are convertible to one another without errors --- tests/ColourTest.php | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/ColourTest.php b/tests/ColourTest.php index 78938d3..cfd8f15 100644 --- a/tests/ColourTest.php +++ b/tests/ColourTest.php @@ -8,6 +8,17 @@ RGBA, HSLA, CMYKA, + Alpha, + Black, + Blue, + Cyan, + Green, + Hue, + Lightness, + Magenta, + Red, + Saturation, + Yellow, }; use Innmind\BlackBox\{ PHPUnit\Framework\TestCase, @@ -70,6 +81,68 @@ public function testFromLiteral(Colour|RGBA $colour, string $hex) $this->assertSame($hex, $rgba->toHexadecimal()); } + public function testAllColoursAreConvertibleToAnyFormat(): BlackBox\Proof + { + $alpha = Set::realNumbers() + ->between(0, 1) + ->map(static fn($value) => Alpha::of($value)->unwrap()) + ->nullable(); + $rgba = Set::compose( + RGBA::from(...), + Set::integers() + ->between(0, 255) + ->map(Red::at(...)), + Set::integers() + ->between(0, 255) + ->map(Green::at(...)), + Set::integers() + ->between(0, 255) + ->map(Blue::at(...)), + $alpha, + ); + $cmyka = Set::compose( + CMYKA::from(...), + Set::integers() + ->between(1, 100) + ->map(Cyan::at(...)), + Set::integers() + ->between(1, 100) + ->map(Magenta::at(...)), + Set::integers() + ->between(1, 100) + ->map(Yellow::at(...)), + Set::integers() + ->between(1, 100) + ->map(Black::at(...)), + $alpha, + ); + $hsla = Set::compose( + HSLA::from(...), + Set::integers() + ->between(0, 359) + ->map(Hue::at(...)), + Set::integers() + ->between(0, 100) + ->map(Saturation::at(...)), + Set::integers() + ->between(0, 100) + ->map(Lightness::at(...)), + $alpha, + ); + + return $this + ->forAll(Set::either( + $rgba, + $cmyka, + $hsla, + )) + ->prove(function($colour) { + $this->assertInstanceOf(RGBA::class, $colour->toRGBA()); + $this->assertInstanceOf(CMYKA::class, $colour->toCMYKA()); + $this->assertInstanceOf(HSLA::class, $colour->toHSLA()); + }); + } + public static function literals() { return [