From 33aa8c3ebcc94fc20a72dd597788afb7b1133ac4 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Wed, 28 Jun 2023 22:48:05 +0200 Subject: [PATCH 01/12] Support Rector in order to update the code base --- composer.json | 1 + rector.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 031e326..b69c6b5 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "require-dev": { "phpstan/phpstan": "^1.5", "phpunit/phpunit": "^8.5.23", + "rector/rector": "^0.17.1", "vimeo/psalm": "^4.0|^5.0" }, "suggest": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..ffe8ef6 --- /dev/null +++ b/rector.php @@ -0,0 +1,22 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + // $rectorConfig->sets([ + // LevelSetList::UP_TO_PHP_72 + // ]); +}; From f1e04e27fa16564a1e1d7fe332e9c67c8f5dc3d6 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Wed, 28 Jun 2023 22:48:50 +0200 Subject: [PATCH 02/12] Include Makefile in order to run rector --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..74ce439 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +rector: + vendor/bin/rector process src + +rector-dry: + vendor/bin/rector process src --dry-run \ No newline at end of file From e62f46ff32e45b0a658a9a2c2818c7f1a7cd6329 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Wed, 28 Jun 2023 22:50:46 +0200 Subject: [PATCH 03/12] Run make rector in order to apply InlineConstructorDefaultToPropertyRector rule --- src/Request/ODataRequestData.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Request/ODataRequestData.php b/src/Request/ODataRequestData.php index 7228b0d..924cd2f 100644 --- a/src/Request/ODataRequestData.php +++ b/src/Request/ODataRequestData.php @@ -24,12 +24,12 @@ final class ODataRequestData implements ODataRequestDataInterface /** * @var int */ - private $top; + private $top = Snelstart::MAX_RESULTS; /** * @var int */ - private $skip; + private $skip = 0; /** * @var string @@ -48,8 +48,6 @@ final class ODataRequestData implements ODataRequestDataInterface public function __construct() { - $this->top = Snelstart::MAX_RESULTS; - $this->skip = 0; } public function getFilter(): array From ec105ae030453f902d60dfa35b80e7270f8327ce Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Wed, 28 Jun 2023 22:51:55 +0200 Subject: [PATCH 04/12] Run make rector in order to apply RandomFunctionRector and CountOnNullRector rule --- rector.php | 6 +++--- src/Exception/SnelstartApiErrorException.php | 2 +- src/Secure/CachedAccessTokenConnection.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rector.php b/rector.php index ffe8ef6..99d522d 100644 --- a/rector.php +++ b/rector.php @@ -16,7 +16,7 @@ $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); // define sets of rules - // $rectorConfig->sets([ - // LevelSetList::UP_TO_PHP_72 - // ]); + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_72 + ]); }; diff --git a/src/Exception/SnelstartApiErrorException.php b/src/Exception/SnelstartApiErrorException.php index 5f997df..a4a3645 100644 --- a/src/Exception/SnelstartApiErrorException.php +++ b/src/Exception/SnelstartApiErrorException.php @@ -11,7 +11,7 @@ final class SnelstartApiErrorException extends \RuntimeException public static function handleError(array $body): self { if (isset($body["modelState"])) { - $errorMessages = [ sprintf("%d validation failures occurred.", \count($body["modelState"])) ]; + $errorMessages = [ sprintf("%d validation failures occurred.", is_array($body["modelState"]) || $body["modelState"] instanceof \Countable ? \count($body["modelState"]) : 0) ]; foreach ($body["modelState"] as $field => $modelStateErrors) { $errorMessages[] = $field . ": "; diff --git a/src/Secure/CachedAccessTokenConnection.php b/src/Secure/CachedAccessTokenConnection.php index 174d623..56dc0fb 100644 --- a/src/Secure/CachedAccessTokenConnection.php +++ b/src/Secure/CachedAccessTokenConnection.php @@ -79,6 +79,6 @@ public function getToken(?BearerTokenInterface $bearerToken = null): AccessToken protected function getItemKey(): string { - return self::CACHE_ITEM_PREFIX . \spl_object_hash($this) . mt_rand(0, 99); + return self::CACHE_ITEM_PREFIX . \spl_object_hash($this) . random_int(0, 99); } } \ No newline at end of file From 91b81d8fec3c603a41e08584e58644497e3540d0 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 19:53:25 +0200 Subject: [PATCH 05/12] Run make rector to update code to php 7.3 --- rector.php | 2 +- src/Exception/SnelstartApiErrorException.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index 99d522d..9a89516 100644 --- a/rector.php +++ b/rector.php @@ -17,6 +17,6 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_72 + LevelSetList::UP_TO_PHP_73, ]); }; diff --git a/src/Exception/SnelstartApiErrorException.php b/src/Exception/SnelstartApiErrorException.php index a4a3645..795dfa3 100644 --- a/src/Exception/SnelstartApiErrorException.php +++ b/src/Exception/SnelstartApiErrorException.php @@ -11,7 +11,7 @@ final class SnelstartApiErrorException extends \RuntimeException public static function handleError(array $body): self { if (isset($body["modelState"])) { - $errorMessages = [ sprintf("%d validation failures occurred.", is_array($body["modelState"]) || $body["modelState"] instanceof \Countable ? \count($body["modelState"]) : 0) ]; + $errorMessages = [ sprintf("%d validation failures occurred.", is_countable($body["modelState"]) ? \count($body["modelState"]) : 0) ]; foreach ($body["modelState"] as $field => $modelStateErrors) { $errorMessages[] = $field . ": "; @@ -39,6 +39,6 @@ public static function handleError(array $body): self return new static($body["Message"] ?? $body["message"], 400); } - throw new static("Unknown exception. Message body: " . \json_encode($body), 400); + throw new static("Unknown exception. Message body: " . \json_encode($body, JSON_THROW_ON_ERROR), 400); } } \ No newline at end of file From f5ef875f769a90dac8db6311d5ce7a73989a6799 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 19:54:34 +0200 Subject: [PATCH 06/12] Update composer to support php 7.3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b69c6b5..40a0e1b 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=7.2", + "php": ">=7.3", "ext-json": "*", "ext-mbstring": "*", "guzzlehttp/guzzle": "^6.4|^7.0", From 28227dc77f9f68c7634d729142c0a27d7e247f68 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:04:51 +0200 Subject: [PATCH 07/12] Update rector to update the code to PHP 7.4 All applied rules are skipped. The team need to decide which rules they want to apply and which to skip --- rector.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/rector.php b/rector.php index 9a89516..ce03657 100644 --- a/rector.php +++ b/rector.php @@ -4,7 +4,10 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; +use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; +use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; use Rector\Set\ValueObject\LevelSetList; +use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ @@ -17,6 +20,12 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_73, + LevelSetList::UP_TO_PHP_74, + ]); + + $rectorConfig->skip([ + TypedPropertyFromAssignsRector::class, + NullCoalescingOperatorRector::class, // https://wiki.php.net/rfc/null_coalesce_equal_operator + ClosureToArrowFunctionRector::class, // https://wiki.php.net/rfc/arrow_functions_v2 ]); }; From b721f8f56e68eb2a8740fb5f821f3446cd812ddf Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:05:13 +0200 Subject: [PATCH 08/12] Update composer to support PHP 7.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 40a0e1b..e4d3f8a 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=7.3", + "php": ">=7.4", "ext-json": "*", "ext-mbstring": "*", "guzzlehttp/guzzle": "^6.4|^7.0", From 985cf9391be6447a5e989547da6db2f3fe71c067 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:16:20 +0200 Subject: [PATCH 09/12] Update code to support phpunit 8 --- rector.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index ce03657..ae8ab81 100644 --- a/rector.php +++ b/rector.php @@ -6,6 +6,7 @@ use Rector\Config\RectorConfig; use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; +use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; @@ -21,11 +22,12 @@ // define sets of rules $rectorConfig->sets([ LevelSetList::UP_TO_PHP_74, + PHPUnitSetList::PHPUNIT_80, ]); $rectorConfig->skip([ - TypedPropertyFromAssignsRector::class, - NullCoalescingOperatorRector::class, // https://wiki.php.net/rfc/null_coalesce_equal_operator - ClosureToArrowFunctionRector::class, // https://wiki.php.net/rfc/arrow_functions_v2 +// TypedPropertyFromAssignsRector::class, +// NullCoalescingOperatorRector::class, // https://wiki.php.net/rfc/null_coalesce_equal_operator +// ClosureToArrowFunctionRector::class, // https://wiki.php.net/rfc/arrow_functions_v2 ]); }; From abae38d0f95eff88d25c5a812754d8ac8c9fdb8b Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:34:47 +0200 Subject: [PATCH 10/12] Update code to support phpunit 9.6 --- composer.json | 2 +- rector.php | 2 +- src/Request/BaseRequest.php | 4 ++-- tests/Request/BaseRequestTest.php | 4 +++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index e4d3f8a..68f562a 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "require-dev": { "phpstan/phpstan": "^1.5", - "phpunit/phpunit": "^8.5.23", + "phpunit/phpunit": "^8.5|^9.6", "rector/rector": "^0.17.1", "vimeo/psalm": "^4.0|^5.0" }, diff --git a/rector.php b/rector.php index ae8ab81..388c43d 100644 --- a/rector.php +++ b/rector.php @@ -22,7 +22,7 @@ // define sets of rules $rectorConfig->sets([ LevelSetList::UP_TO_PHP_74, - PHPUnitSetList::PHPUNIT_80, + PHPUnitSetList::PHPUNIT_91, ]); $rectorConfig->skip([ diff --git a/src/Request/BaseRequest.php b/src/Request/BaseRequest.php index 817b3b3..39c1763 100644 --- a/src/Request/BaseRequest.php +++ b/src/Request/BaseRequest.php @@ -4,6 +4,7 @@ use Money\Money; use Ramsey\Uuid\UuidInterface; +use RuntimeException; use SnelstartPHP\Model\BaseObject; use SnelstartPHP\Model\SnelstartObject; use SnelstartPHP\Serializer\RequestSerializerInterface; @@ -53,8 +54,7 @@ public function prepareAddOrEditRequestForSerialization(BaseObject $object, stri } if (!$methodExists) { - \trigger_error(sprintf("There is no method (get or is) on object %s for property %s", get_class($object), $editableAttributeName), \E_USER_NOTICE); - continue; + throw new RuntimeException(sprintf("There is no method (get or is) on object %s for property %s", get_class($object), $editableAttributeName)); } $value = $object->{$methodName}(); diff --git a/tests/Request/BaseRequestTest.php b/tests/Request/BaseRequestTest.php index 26ba840..a2756b7 100644 --- a/tests/Request/BaseRequestTest.php +++ b/tests/Request/BaseRequestTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; +use RuntimeException; use SnelstartPHP\Model\BaseObject; use SnelstartPHP\Model\SnelstartObject; use SnelstartPHP\Serializer\RequestSerializerInterface; @@ -45,7 +46,8 @@ public function testEditableArgumentsCallForNotExistingMethod(): void $object::$editableAttributes = [ "notExistingProperty" ]; - $this->expectNotice(); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('There is no method (get or is) on object SnelstartPHP\\Tests\\stubs\\SimpleRequestObjectStub for property notExistingProperty'); $this->assertEmpty($request->prepareAddOrEditRequestForSerialization($object)); } From b7d7ae3e78e05df6f2cefbbab26b1e1ec8c3cb91 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:54:11 +0200 Subject: [PATCH 11/12] Ignore PHP 7.2 and 7.3 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8616f1b..3933082 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4, 8.0, 8.1] + php: [7.4, 8.0, 8.1] stability: [prefer-lowest, prefer-stable] name: PHP ${{ matrix.php }} - ${{ matrix.stability }} From 00a8b6c59c3d43a198b3baa8611dff0f37bf8e98 Mon Sep 17 00:00:00 2001 From: Harry van der Valk Date: Thu, 29 Jun 2023 20:55:35 +0200 Subject: [PATCH 12/12] Skip Rector rules --- rector.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index 388c43d..a00510b 100644 --- a/rector.php +++ b/rector.php @@ -26,8 +26,8 @@ ]); $rectorConfig->skip([ -// TypedPropertyFromAssignsRector::class, -// NullCoalescingOperatorRector::class, // https://wiki.php.net/rfc/null_coalesce_equal_operator -// ClosureToArrowFunctionRector::class, // https://wiki.php.net/rfc/arrow_functions_v2 + TypedPropertyFromAssignsRector::class, + NullCoalescingOperatorRector::class, // https://wiki.php.net/rfc/null_coalesce_equal_operator + ClosureToArrowFunctionRector::class, // https://wiki.php.net/rfc/arrow_functions_v2 ]); };