From 9dfeb75e40065538321dd902ec2655021a5024e6 Mon Sep 17 00:00:00 2001 From: Romain <7681951+romainnorberg@users.noreply.github.com> Date: Fri, 25 Aug 2023 05:12:16 +0200 Subject: [PATCH] Upgrade to PHP 8 (#6) * PHP 8 support * Update CS fixer --- .github/workflows/php-cs-fixer.yml | 2 +- .github/workflows/run-tests.yml | 10 ++--- .gitignore | 2 + .php-cs-fixer.dist.php | 53 ++++++++++++++++++++++++++ .php_cs.dist | 43 --------------------- CHANGELOG.md | 5 --- LICENSE | 2 +- README.md | 4 -- composer.json | 8 ++-- phpunit.xml.dist | 32 ++++++---------- src/Contracts/ResidueInterface.php | 6 +-- src/Exception/DecimalException.php | 4 +- src/Exception/DivideException.php | 4 +- src/Exception/ResidueModeException.php | 4 +- src/Exception/StepException.php | 4 +- src/Residue.php | 9 ++--- tests/ResidueExceptionTest.php | 3 +- tests/ResidueSplitTest.php | 38 +++++++++--------- 18 files changed, 110 insertions(+), 123 deletions(-) create mode 100644 .php-cs-fixer.dist.php delete mode 100644 .php_cs.dist delete mode 100644 CHANGELOG.md diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index 3265fae..5db1f59 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -11,4 +11,4 @@ jobs: - name: Package uses: docker://oskarstark/php-cs-fixer-ga with: - args: --config=.php_cs.dist --diff --dry-run + args: --config=.php-cs-fixer.dist.php --diff --dry-run diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 6eb0372..eddd984 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest] - php: [7.4] + php: [8.2, 8.3] dependency-version: [prefer-lowest, prefer-stable] include: - testbench: 5.* @@ -18,16 +18,16 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v1 + uses: actions/checkout@v3 - name: Cache dependencies - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.composer/cache/files key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - name: Setup PHP - uses: shivammathur/setup-php@v1 + uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: json @@ -41,7 +41,7 @@ jobs: run: vendor/bin/phpunit --coverage-clover=coverage.xml - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml diff --git a/.gitignore b/.gitignore index 9d54ff0..6f5c409 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ composer.lock /vendor/ .php_cs.cache .phpunit.result.cache +.phpunit.cache/* +phpunit.xml.dist.bak coverage.xml .coverage diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..c91ed17 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,53 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$fileHeaderComment = << +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. +COMMENT; + +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__) + ->exclude('config') +; + +return (new PhpCsFixer\Config()) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + // Binary operators should be surrounded by space as configured. + 'binary_operator_spaces' => [ + 'operators' => ['=>' => 'align_single_space_minimal'], + ], + 'array_syntax' => ['syntax' => 'short'], + 'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'], + 'linebreak_after_opening_tag' => true, + 'mb_str_functions' => true, + 'no_php4_constructor' => true, + 'no_superfluous_phpdoc_tags' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_imports' => true, + 'php_unit_strict' => false, + 'phpdoc_order' => true, + 'semicolon_after_instruction' => true, + 'strict_comparison' => true, + 'strict_param' => true, + 'trailing_comma_in_multiline' => true, + 'php_unit_method_casing' => false, + 'php_unit_test_annotation' => false, + 'visibility_required' => false, + ]) + ->setFinder($finder) + ->setCacheFile(__DIR__.'/.php_cs.cache') +; diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index c5d38d1..0000000 --- a/.php_cs.dist +++ /dev/null @@ -1,43 +0,0 @@ - -For the full copyright and license information, please view the LICENSE -file that was distributed with this source code. -COMMENT; - -$finder = PhpCsFixer\Finder::create() - ->in(__DIR__) - ->exclude('config') -; - -return PhpCsFixer\Config::create() - ->setRiskyAllowed(true) - ->setRules([ - '@Symfony' => true, - '@Symfony:risky' => true, - // Binary operators should be surrounded by space as configured. - 'binary_operator_spaces' => [ - 'operators' => ['=>' => 'align_single_space_minimal'], - ], - 'array_syntax' => ['syntax' => 'short'], - 'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'], - 'linebreak_after_opening_tag' => true, - 'mb_str_functions' => true, - 'no_php4_constructor' => true, - 'no_superfluous_phpdoc_tags' => true, - 'no_unreachable_default_argument_value' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_imports' => true, - 'php_unit_strict' => false, - 'phpdoc_order' => true, - 'semicolon_after_instruction' => true, - 'strict_comparison' => true, - 'strict_param' => true, - 'trailing_comma_in_multiline_array' => true - ]) - ->setFinder($finder) - ->setCacheFile(__DIR__.'/.php_cs.cache') -; diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 14932ad..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,5 +0,0 @@ -Changelog -All notable changes to this packages will be documented in this file - -1.0.0 - 202X-XX-XX -initial release diff --git a/LICENSE b/LICENSE index 059ee59..11bd76e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Romain Norberg +Copyright (c) 2023 Romain Norberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6503f89..282d181 100644 --- a/README.md +++ b/README.md @@ -147,10 +147,6 @@ array(3) { composer test ``` -## Changelog - -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. - ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. diff --git a/composer.json b/composer.json index e657274..fe0cabc 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,12 @@ }, "type": "library", "require": { - "php": "^7.4" + "php": "^8.2" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.16", - "phpstan/phpstan": "^0.12.49", - "phpunit/phpunit": "^8.0", + "friendsofphp/php-cs-fixer": "^3.0", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^10.0", "spatie/phpunit-watcher": "^1.22" }, "authors": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2d1dda5..6333974 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,22 +1,14 @@ - - - - tests - - - - - src/ - - + + + + + tests + + + + + src/ + + diff --git a/src/Contracts/ResidueInterface.php b/src/Contracts/ResidueInterface.php index 80b1ee9..f824fc8 100644 --- a/src/Contracts/ResidueInterface.php +++ b/src/Contracts/ResidueInterface.php @@ -9,10 +9,10 @@ namespace Romainnorberg\Residue\Contracts; -use Generator; - interface ResidueInterface { + public const DEFAULT_DECIMAL = 2; + public const SPLIT_MODE_ALLOCATE = 'allocate'; public const SPLIT_MODE_EQUITY = 'equity'; public const SPLIT_MODES = [ @@ -41,7 +41,7 @@ public function decimal(int $decimal): self; * or * 101/3 with step 1 will split again into [33, 33, 33] */ - public function split(string $mode = self::SPLIT_MODE_ALLOCATE): Generator; + public function split(string $mode = self::SPLIT_MODE_ALLOCATE): \Generator; public function toArray(string $mode = self::SPLIT_MODE_ALLOCATE): array; diff --git a/src/Exception/DecimalException.php b/src/Exception/DecimalException.php index 65c9133..f55d15f 100644 --- a/src/Exception/DecimalException.php +++ b/src/Exception/DecimalException.php @@ -9,8 +9,6 @@ namespace Romainnorberg\Residue\Exception; -use InvalidArgumentException; - -class DecimalException extends InvalidArgumentException +class DecimalException extends \InvalidArgumentException { } diff --git a/src/Exception/DivideException.php b/src/Exception/DivideException.php index 72c695e..65536a7 100644 --- a/src/Exception/DivideException.php +++ b/src/Exception/DivideException.php @@ -9,8 +9,6 @@ namespace Romainnorberg\Residue\Exception; -use InvalidArgumentException; - -class DivideException extends InvalidArgumentException +class DivideException extends \InvalidArgumentException { } diff --git a/src/Exception/ResidueModeException.php b/src/Exception/ResidueModeException.php index 266ca54..518c518 100644 --- a/src/Exception/ResidueModeException.php +++ b/src/Exception/ResidueModeException.php @@ -9,8 +9,6 @@ namespace Romainnorberg\Residue\Exception; -use InvalidArgumentException; - -class ResidueModeException extends InvalidArgumentException +class ResidueModeException extends \InvalidArgumentException { } diff --git a/src/Exception/StepException.php b/src/Exception/StepException.php index fdd6eed..3af9303 100644 --- a/src/Exception/StepException.php +++ b/src/Exception/StepException.php @@ -9,8 +9,6 @@ namespace Romainnorberg\Residue\Exception; -use InvalidArgumentException; - -class StepException extends InvalidArgumentException +class StepException extends \InvalidArgumentException { } diff --git a/src/Residue.php b/src/Residue.php index 6607b82..c9edca1 100644 --- a/src/Residue.php +++ b/src/Residue.php @@ -11,7 +11,6 @@ namespace Romainnorberg\Residue; -use Generator; use Romainnorberg\Residue\Contracts\ResidueInterface; use Romainnorberg\Residue\Exception\CannotGetRemainderException; use Romainnorberg\Residue\Exception\DecimalException; @@ -24,7 +23,7 @@ final class Residue implements ResidueInterface private float $value; private bool $isNegative; private int $divider = 1; - private int $decimal = 2; + private int $decimal = ResidueInterface::DEFAULT_DECIMAL; private float $step; private float $remainder; @@ -73,7 +72,7 @@ public static function create(float $value): self * - Defining rm according to `M` * - Iterate [n as 1 ... Dv] : yield pⁿ */ - public function split(string $mode = self::SPLIT_MODE_ALLOCATE): Generator + public function split(string $mode = self::SPLIT_MODE_ALLOCATE): \Generator { if (!\in_array($mode, self::SPLIT_MODES, true)) { throw new ResidueModeException(sprintf('Accepted modes are : %s', implode(', ', self::SPLIT_MODES))); @@ -92,8 +91,8 @@ public function split(string $mode = self::SPLIT_MODE_ALLOCATE): Generator for ($i = 1; $i <= $this->divider; ++$i) { $xn = $defaultNumberOfSteps; - if (self::SPLIT_MODE_ALLOCATE === $mode && - ($defaultNumberOfSteps * $this->divider + $i) <= $maxNumberOfSteps + if (self::SPLIT_MODE_ALLOCATE === $mode + && ($defaultNumberOfSteps * $this->divider + $i) <= $maxNumberOfSteps ) { ++$xn; } diff --git a/tests/ResidueExceptionTest.php b/tests/ResidueExceptionTest.php index 976e27a..5bb0006 100644 --- a/tests/ResidueExceptionTest.php +++ b/tests/ResidueExceptionTest.php @@ -45,6 +45,7 @@ public function it_should_throw_cannot_get_remainder_exception(): void /** * @test + * * @dataProvider provideDataForDivideException */ public function it_should_throw_divide_exception(int $minusThanOne): void @@ -80,7 +81,7 @@ public function it_should_throw_decimal_exception(): void ->decimal(-1); } - public function provideDataForDivideException() + static public function provideDataForDivideException() { return [ [0], diff --git a/tests/ResidueSplitTest.php b/tests/ResidueSplitTest.php index 69db3a7..5e4ec8a 100644 --- a/tests/ResidueSplitTest.php +++ b/tests/ResidueSplitTest.php @@ -25,7 +25,7 @@ protected function it_should_split_with_mode( ): void { $this->assertEquals( $value, - array_sum($expected) + $expectedRemainder, 'Something goes wrong in your test data set, value have to be equal to the sum of expected values + remainder as is: value = SUM($expected) + $expectedRemainder). Check your data set.' + round(array_sum($expected) + $expectedRemainder, (int) mb_strpos(strrev($value), '.')), 'Something goes wrong in your test data set, value have to be equal to the sum of expected values + remainder as is: value = SUM($expected) + $expectedRemainder). Check your data set.' ); $residue = Residue::create($value)->divideBy($divider); @@ -44,12 +44,11 @@ protected function it_should_split_with_mode( $this->assertEquals($expected, $split); $this->assertEquals($expectedRemainder, $remainder); - - $this->assertEquals($value, array_sum($split) + $remainder); } /** * @test + * * @dataProvider dataProviderAllocateValues */ public function it_should_split_with_mode_allocate( @@ -61,18 +60,19 @@ public function it_should_split_with_mode_allocate( float $expectedRemainder ): void { $this->it_should_split_with_mode( - Residue::SPLIT_MODE_ALLOCATE, - $value, - $divider, - $decimalValue, - $step, - $expected, - $expectedRemainder + mode: Residue::SPLIT_MODE_ALLOCATE, + value: $value, + divider: $divider, + decimalValue: $decimalValue, + step: $step, + expected: $expected, + expectedRemainder: $expectedRemainder ); } /** * @test + * * @dataProvider dataProviderEquityValues */ public function it_should_split_with_mode_equity( @@ -84,17 +84,17 @@ public function it_should_split_with_mode_equity( float $expectedRemainder ): void { $this->it_should_split_with_mode( - Residue::SPLIT_MODE_EQUITY, - $value, - $divider, - $decimalValue, - $step, - $expected, - $expectedRemainder + mode: Residue::SPLIT_MODE_EQUITY, + value: $value, + divider: $divider, + decimalValue: $decimalValue, + step: $step, + expected: $expected, + expectedRemainder: $expectedRemainder ); } - public function dataProviderAllocateValues() + static public function dataProviderAllocateValues() { yield '-4/3 - Step 0.01' => [ -4, // value @@ -306,7 +306,7 @@ public function dataProviderAllocateValues() ]; } - public function dataProviderEquityValues() + static public function dataProviderEquityValues() { yield '-4/3 - Step 0.01' => [ -4, // value