From 602ba87172ea8623376ff3921c9a290cf1f889ce Mon Sep 17 00:00:00 2001 From: Yevhenii Kovalenko Date: Wed, 31 Jan 2024 13:56:08 +0100 Subject: [PATCH] Refactor with PHP 8.1 --- .github/workflows/phpunit.yml | 5 +- .github/workflows/static-analysis.yml | 3 +- .gitignore | 2 +- .php-cs-fixer.php | 43 +++++ .php_cs | 26 --- composer.json | 97 ++++++----- grumphp.yaml | 2 +- phpstan.neon | 9 +- phpunit.xml.dist | 1 + src/Client/Client.php | 2 +- src/Client/Serializer.php | 2 +- src/Enum/PointFunctionsType.php | 172 +++---------------- src/Enum/PointType.php | 32 +--- src/Request/FindPointsRequest.php | 6 +- src/Request/FindPointsRequestBuilder.php | 10 +- src/Response/ErrorResponse.php | 13 +- src/Response/Struct/Item.php | 4 +- src/Response/Struct/ItemCollection.php | 1 + tests/Integration/Command/FindPointsTest.php | 6 +- tests/MockGuzzleTrait.php | 2 +- 20 files changed, 159 insertions(+), 279 deletions(-) create mode 100755 .php-cs-fixer.php delete mode 100755 .php_cs diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index f7b899f..b00a2d6 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -11,12 +11,13 @@ jobs: strategy: matrix: php-version: - - "7.4" + - "8.1" + - "8.2" deps: - "normal" include: - deps: "low" - php-version: "7.4" + php-version: "8.1" steps: - name: "Checkout" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index eb26563..090caf2 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -11,7 +11,8 @@ jobs: strategy: matrix: php-version: - - "7.4" + - "8.1" + - "8.2" steps: - name: "Checkout" diff --git a/.gitignore b/.gitignore index 4d30792..799ce4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /vendor/ -\.php_cs\.cache +\.php-cs-fixer\.cache .phpunit.result.cache composer.lock /.idea diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100755 index 0000000..4a5f997 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,43 @@ +getFinder() + ->exclude( + [ + 'vendor', + ] + ) + ->in(__DIR__); + +return $config + ->setRules( + [ + '@Symfony' => true, + 'strict_param' => false, + 'array_syntax' => ['syntax' => 'short'], + 'concat_space' => ['spacing' => 'one'], + 'phpdoc_align' => [], + 'phpdoc_summary' => false, + 'void_return' => false, + 'phpdoc_var_without_name' => false, + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + 'modernize_types_casting' => true, + 'function_declaration' => false, + 'ordered_imports' => [ + 'imports_order' => [ + 'class', + 'function', + 'const', + ], + 'sort_algorithm' => 'alpha', + ], + 'phpdoc_separation' => ['skip_unlisted_annotations' => true], + 'nullable_type_declaration' => true, + 'nullable_type_declaration_for_default_null_value' => true, + 'trailing_comma_in_multiline' => ['elements' => ['arrays', 'parameters']], + ] + ); diff --git a/.php_cs b/.php_cs deleted file mode 100755 index 3362983..0000000 --- a/.php_cs +++ /dev/null @@ -1,26 +0,0 @@ -exclude( - [ - 'vendor', - ] - ) - ->in(__DIR__); - -return PhpCsFixer\Config::create() - ->setRules( - [ - '@Symfony' => true, - 'strict_param' => false, - 'array_syntax' => ['syntax' => 'short'], - 'concat_space' => ['spacing' => 'one'], - 'phpdoc_align' => [], - 'phpdoc_summary' => false, - 'void_return' => false, - 'phpdoc_var_without_name' => false, - 'phpdoc_to_comment' => false, - 'single_line_throw' => false, - ] - ) - ->setFinder($finder); diff --git a/composer.json b/composer.json index 9430cd9..c5569e7 100755 --- a/composer.json +++ b/composer.json @@ -1,50 +1,53 @@ { - "name": "answear/inpost-pickup-point-bundle", - "description": "API Client for Inpost.", - "type": "symfony-bundle", - "license": "MIT", - "require": { - "php": ">=7.4", - "ext-json": "*", - "guzzlehttp/guzzle": "^6.0", - "marc-mabe/php-enum": "^3.0|^4.3", - "symfony/http-kernel": "^4.4|^5.0", - "symfony/serializer": "^4.4|^5.0", - "webmozart/assert": "^1.3" - }, - "require-dev": { - "roave/security-advisories": "dev-master", - "phpunit/phpunit": "^8.4", - "symfony/phpunit-bridge": "^5.0", - "phpro/grumphp": "^0.20", - "friendsofphp/php-cs-fixer": "^2.16", - "phpstan/phpstan": "^0.12.32", - "phpstan/phpstan-webmozart-assert": "^0.12.2" - }, - "autoload": { - "psr-4": { - "Answear\\InpostBundle\\": "src/" + "name": "answear/inpost-pickup-point-bundle", + "description": "API Client for Inpost.", + "type": "symfony-bundle", + "license": "MIT", + "require": { + "php": "^8.1", + "ext-json": "*", + "guzzlehttp/guzzle": "^6.0", + "phpstan/phpstan-deprecation-rules": "^1.1", + "symfony/http-kernel": "^6.0", + "symfony/serializer": "^6.0", + "webmozart/assert": "^1.11" + }, + "require-dev": { + "roave/security-advisories": "dev-master", + "phpunit/phpunit": "^9.6.16", + "symfony/phpunit-bridge": "6.1.*", + "phpro/grumphp": "^1.16", + "friendsofphp/php-cs-fixer": "^3.48", + "phpstan/phpstan": "^1.10.57", + "phpstan/phpstan-webmozart-assert": "^1.2.4" + }, + "autoload": { + "psr-4": { + "Answear\\InpostBundle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Answear\\InpostBundle\\Tests\\": "tests/" + } + }, + "scripts": { + "post-install-cmd": [ + "GrumPHP\\Composer\\DevelopmentIntegrator::integrate" + ], + "post-update-cmd": [ + "GrumPHP\\Composer\\DevelopmentIntegrator::integrate" + ] + }, + "extra": { + "grumphp": { + "config-default-path": "grumphp.yaml" + } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "phpro/grumphp": true + } } - }, - "autoload-dev": { - "psr-4": { - "Answear\\InpostBundle\\Tests\\": "tests/" - } - }, - "scripts": { - "post-install-cmd": [ - "GrumPHP\\Composer\\DevelopmentIntegrator::integrate" - ], - "post-update-cmd": [ - "GrumPHP\\Composer\\DevelopmentIntegrator::integrate" - ] - }, - "extra": { - "grumphp": { - "config-default-path": "grumphp.yaml" - } - }, - "config": { - "sort-packages": true - } } diff --git a/grumphp.yaml b/grumphp.yaml index 759f2b3..3808677 100755 --- a/grumphp.yaml +++ b/grumphp.yaml @@ -18,7 +18,7 @@ grumphp: phpcsfixer: allow_risky: true cache_file: ~ - config: './.php_cs' + config: './.php-cs-fixer.php' using_cache: true verbose: true phpstan: diff --git a/phpstan.neon b/phpstan.neon index d238de6..e90db49 100755 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,14 @@ includes: - vendor/phpstan/phpstan-webmozart-assert/extension.neon + - vendor/phpstan/phpstan-deprecation-rules/rules.neon parameters: level: 5 + checkNullables: true + checkUnionTypes: true + treatPhpDocTypesAsCertain: false paths: - - %rootDir%/../../../src \ No newline at end of file + - %rootDir%/../../../src + excludePaths: + - src/Tests/* + - tests/* \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d03a175..20e1bee 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,6 +15,7 @@ > + diff --git a/src/Client/Client.php b/src/Client/Client.php index 4877e74..7abedb6 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -17,7 +17,7 @@ class Client private ClientInterface $client; public function __construct( - ?ClientInterface $client = null + ?ClientInterface $client = null, ) { $this->client = $client ?? new GuzzleClient( [ diff --git a/src/Client/Serializer.php b/src/Client/Serializer.php index 3928f34..8b43422 100644 --- a/src/Client/Serializer.php +++ b/src/Client/Serializer.php @@ -19,7 +19,7 @@ public function serialize(Request $request): string { return $this->getSerializer()->serialize( $request, - static::FORMAT, + self::FORMAT, [Normalizer\AbstractObjectNormalizer::SKIP_NULL_VALUES => true] ); } diff --git a/src/Enum/PointFunctionsType.php b/src/Enum/PointFunctionsType.php index 2a577ac..7425adc 100644 --- a/src/Enum/PointFunctionsType.php +++ b/src/Enum/PointFunctionsType.php @@ -4,152 +4,30 @@ namespace Answear\InpostBundle\Enum; -use MabeEnum\Enum; - -class PointFunctionsType extends Enum +enum PointFunctionsType: string { - public const PARCEL = 'parcel'; - public const PARCEL_SEND = 'parcel_send'; - public const PARCEL_COLLECT = 'parcel_collect'; - public const PARCEL_REVERSE_RETURN_SEND = 'parcel_reverse_return_send'; - public const STANDARD_LETTER_COLLECT = 'standard_letter_collect'; - public const STANDARD_LETTER_SEND = 'standard_letter_send'; - public const ALLEGRO_PARCEL_COLLECT = 'allegro_parcel_collect'; - public const ALLEGRO_PARCEL_SEND = 'allegro_parcel_send'; - public const ALLEGRO_PARCEL_REVERSE_RETURN_SEND = 'allegro_parcel_reverse_return_send'; - public const ALLEGRO_LETTER_COLLECT = 'allegro_letter_collect'; - public const ALLEGRO_LETTER_SEND = 'allegro_letter_send'; - public const ALLEGRO_LETTER_REVERSE_RETURN_SEND = 'allegro_letter_reverse_return_send'; - public const ALLEGRO_COURIER_COLLECT = 'allegro_courier_collect'; - public const ALLEGRO_COURIER_SEND = 'allegro_courier_send'; - public const ALLEGRO_COURIER_REVERSE_RETURN_SEND = 'allegro_courier_reverse_return_send'; - public const STANDARD_COURIER_COLLECT = 'standard_courier_collect'; - public const STANDARD_COURIER_SEND = 'standard_courier_send'; - public const STANDARD_COURIER_REVERSE_RETURN_SEND = 'standard_courier_reverse_return_send'; - public const AIR_ON_AIRPORT = 'air_on_airport'; - public const AIR_OUTSIDE_AIRPORT = 'air_outside_airport'; - public const COOL_PARCEL_COLLECT = 'cool_parcel_collect'; - public const LAUNDRY = 'laundry'; - public const LAUNDRY_COLLECT = 'laundry_collect'; - public const AVIZO = 'avizo'; - - public static function parcel(): self - { - return static::get(static::PARCEL); - } - - public static function parcelSend(): self - { - return static::get(static::PARCEL_SEND); - } - - public static function parcelCollect(): self - { - return static::get(static::PARCEL_COLLECT); - } - - public static function parcelReverseReturnSend(): self - { - return static::get(static::PARCEL_REVERSE_RETURN_SEND); - } - - public static function standardLetterCollect(): self - { - return static::get(static::STANDARD_LETTER_COLLECT); - } - - public static function standardLetterSend(): self - { - return static::get(static::STANDARD_LETTER_SEND); - } - - public static function allegroParcelCollect(): self - { - return static::get(static::ALLEGRO_PARCEL_COLLECT); - } - - public static function allegroParcelSend(): self - { - return static::get(static::ALLEGRO_PARCEL_SEND); - } - - public static function allegroParcelReverseReturnSend(): self - { - return static::get(static::ALLEGRO_PARCEL_REVERSE_RETURN_SEND); - } - - public static function allegroLetterCollect(): self - { - return static::get(static::ALLEGRO_LETTER_COLLECT); - } - - public static function allegroLetterSend(): self - { - return static::get(static::ALLEGRO_LETTER_SEND); - } - - public static function allegroLetterReverseReturnSend(): self - { - return static::get(static::ALLEGRO_LETTER_REVERSE_RETURN_SEND); - } - - public static function allegroCourierCollect(): self - { - return static::get(static::ALLEGRO_COURIER_COLLECT); - } - - public static function allegroCourierSend(): self - { - return static::get(static::ALLEGRO_COURIER_SEND); - } - - public static function allegroCourierReverseReturnSend(): self - { - return static::get(static::ALLEGRO_COURIER_REVERSE_RETURN_SEND); - } - - public static function standardCourierCollect(): self - { - return static::get(static::STANDARD_COURIER_COLLECT); - } - - public static function standardCourierSend(): self - { - return static::get(static::STANDARD_COURIER_SEND); - } - - public static function standardCourierReverseReturnSend(): self - { - return static::get(static::STANDARD_COURIER_REVERSE_RETURN_SEND); - } - - public static function airOnAirport(): self - { - return static::get(static::AIR_ON_AIRPORT); - } - - public static function airOutsideAirport(): self - { - return static::get(static::AIR_OUTSIDE_AIRPORT); - } - - public static function coolParcelCollect(): self - { - return static::get(static::COOL_PARCEL_COLLECT); - } - - public static function laundry(): self - { - return static::get(static::LAUNDRY); - } - - public static function laundryCollect(): self - { - return static::get(static::LAUNDRY_COLLECT); - } - - public static function avizo(): self - { - return static::get(static::AVIZO); - } + case Parcel = 'parcel'; + case ParcelSend = 'parcel_send'; + case ParcelCollect = 'parcel_collect'; + case ParcelReverseReturnSend = 'parcel_reverse_return_send'; + case StandardLetterCollect = 'standard_letter_collect'; + case StandardLetterSend = 'standard_letter_send'; + case AllegroParcelCollect = 'allegro_parcel_collect'; + case AllegroParcelSend = 'allegro_parcel_send'; + case AllegroParcelReverseReturnSend = 'allegro_parcel_reverse_return_send'; + case AllegroLetterCollect = 'allegro_letter_collect'; + case AllegroLetterSend = 'allegro_letter_send'; + case AllegroLetterReverseReturnSend = 'allegro_letter_reverse_return_send'; + case AllegroCourierCollect = 'allegro_courier_collect'; + case AllegroCourierSend = 'allegro_courier_send'; + case AllegroCourierReverseReturnSend = 'allegro_courier_reverse_return_send'; + case StandardCourierCollect = 'standard_courier_collect'; + case StandardCourierSend = 'standard_courier_send'; + case StandardCourierReverseReturnSend = 'standard_courier_reverse_return_send'; + case AirOnAirport = 'air_on_airport'; + case AirOutsideAirport = 'air_outside_airport'; + case CoolParcelCollect = 'cool_parcel_collect'; + case Laundry = 'laundry'; + case LaundryCollect = 'laundry_collect'; + case Avizo = 'avizo'; } diff --git a/src/Enum/PointType.php b/src/Enum/PointType.php index 34056f8..a661c3a 100644 --- a/src/Enum/PointType.php +++ b/src/Enum/PointType.php @@ -4,32 +4,10 @@ namespace Answear\InpostBundle\Enum; -use MabeEnum\Enum; - -class PointType extends Enum +enum PointType: string { - public const PARCEL_LOCKER = 'parcel_locker'; - public const POP = 'pop'; - public const PARCEL_LOCKER_ONLY = 'parcel_locker_only'; - public const PARCEL_LOCKER_SUPERPOP = 'parcel_locker_superpop'; - - public static function parcelLocker(): self - { - return static::get(static::PARCEL_LOCKER); - } - - public static function pop(): self - { - return static::get(static::POP); - } - - public static function parcelLockerOnly(): self - { - return static::get(static::PARCEL_LOCKER_ONLY); - } - - public static function parcelLockerSuperPop(): self - { - return static::get(static::PARCEL_LOCKER_SUPERPOP); - } + case ParcelLocker = 'parcel_locker'; + case Pop = 'pop'; + case ParcelLockerOnly = 'parcel_locker_only'; + case ParcelLockerSuperpop = 'parcel_locker_superpop'; } diff --git a/src/Request/FindPointsRequest.php b/src/Request/FindPointsRequest.php index 826f6ec..724098a 100644 --- a/src/Request/FindPointsRequest.php +++ b/src/Request/FindPointsRequest.php @@ -19,14 +19,14 @@ public function __construct(array $criteria = []) public function getRequestUrl(): string { if (count($this->searchCriteria) > 0) { - return static::ENDPOINT . '?' . http_build_query($this->searchCriteria); + return self::ENDPOINT . '?' . http_build_query($this->searchCriteria); } - return static::ENDPOINT; + return self::ENDPOINT; } public function getMethod(): string { - return static::HTTP_METHOD; + return self::HTTP_METHOD; } } diff --git a/src/Request/FindPointsRequestBuilder.php b/src/Request/FindPointsRequestBuilder.php index fd7a22b..8c91187 100644 --- a/src/Request/FindPointsRequestBuilder.php +++ b/src/Request/FindPointsRequestBuilder.php @@ -32,7 +32,7 @@ public function setNames(array $names): self public function setType(PointType $type): self { - $this->criteria['type'] = $type->getValue(); + $this->criteria['type'] = $type->value; return $this; } @@ -42,8 +42,8 @@ public function setType(PointType $type): self */ public function setTypes(array $types): self { - $this->criteria['type'] = implode(self::SEPARATOR, array_map(function ($type) { - return $type->getValue(); + $this->criteria['type'] = implode(self::SEPARATOR, array_map(static function ($type) { + return $type->value; }, $types)); return $this; @@ -51,7 +51,7 @@ public function setTypes(array $types): self public function setFunction(PointFunctionsType $function): self { - $this->criteria['functions'] = $function->getValue(); + $this->criteria['functions'] = $function->value; return $this; } @@ -62,7 +62,7 @@ public function setFunction(PointFunctionsType $function): self public function setFunctions(array $functions): self { $this->criteria['functions'] = implode(self::SEPARATOR, array_map(function ($type) { - return $type->getValue(); + return $type->value; }, $functions)); return $this; diff --git a/src/Response/ErrorResponse.php b/src/Response/ErrorResponse.php index 085b155..798987b 100644 --- a/src/Response/ErrorResponse.php +++ b/src/Response/ErrorResponse.php @@ -8,18 +8,11 @@ class ErrorResponse { - public string $message; - public string $id; - public int $code; - public function __construct( - string $message, - string $id, - int $code + public string $message, + public string $id, + public int $code, ) { - $this->message = $message; - $this->id = $id; - $this->code = $code; } public static function isErrorResponse(array $response): bool diff --git a/src/Response/Struct/Item.php b/src/Response/Struct/Item.php index fe7692a..0a9ab38 100644 --- a/src/Response/Struct/Item.php +++ b/src/Response/Struct/Item.php @@ -78,7 +78,7 @@ private static function getType(array $types): array { $pointType = []; foreach ($types as $type) { - $pointType[] = PointType::get($type); + $pointType[] = PointType::from($type); } return $pointType; @@ -93,7 +93,7 @@ private static function getFunctionsTypes(array $functions): array { $pointFunctionsTypes = []; foreach ($functions as $function) { - $pointFunctionsTypes[] = PointFunctionsType::get($function); + $pointFunctionsTypes[] = PointFunctionsType::from($function); } return $pointFunctionsTypes; diff --git a/src/Response/Struct/ItemCollection.php b/src/Response/Struct/ItemCollection.php index 3e44714..9de3585 100644 --- a/src/Response/Struct/ItemCollection.php +++ b/src/Response/Struct/ItemCollection.php @@ -26,6 +26,7 @@ public function __construct(array $offices) /** * @return Item[] */ + #[\ReturnTypeWillChange] public function getIterator(): iterable { foreach ($this->offices as $key => $office) { diff --git a/tests/Integration/Command/FindPointsTest.php b/tests/Integration/Command/FindPointsTest.php index 7ccf1b6..d28b00c 100644 --- a/tests/Integration/Command/FindPointsTest.php +++ b/tests/Integration/Command/FindPointsTest.php @@ -55,7 +55,7 @@ private function assertPoint(FindPointsResponse $response): void $this->assertNotNull($point); $this->assertSame($point->id, 'ADA01M'); $this->assertSame($point->name, 'ADA01M'); - $this->assertSame($point->type, [PointType::parcelLocker()]); + $this->assertSame($point->type, [PointType::ParcelLocker]); $this->assertSame($point->status, 'Operating'); $this->assertInstanceOf(ItemLocation::class, $point->location); $this->assertSame($point->location->longitude, 22.264049625); @@ -79,7 +79,7 @@ private function assertPoint(FindPointsResponse $response): void $this->assertNull($point->addressDetails->flatNumber); $this->assertNull($point->phoneNumber); $this->assertSame($point->paymentPointDescr, 'Płatność internetowa PayByLink oraz Blik'); - $this->assertSame($point->functions, [PointFunctionsType::parcelCollect(), PointFunctionsType::parcelSend()]); + $this->assertSame($point->functions, [PointFunctionsType::ParcelCollect, PointFunctionsType::ParcelSend]); $this->assertSame($point->partnerId, 0); $this->assertFalse($point->isNext); $this->assertTrue($point->paymentAvailable); @@ -94,7 +94,7 @@ private function getCommand(): FindPoints return new FindPoints($this->client, new Serializer()); } - private function getSuccessfulBody(): string + private function getSuccessfulBody(): string|false { return file_get_contents(__DIR__ . '/data/exampleResponse.json'); } diff --git a/tests/MockGuzzleTrait.php b/tests/MockGuzzleTrait.php index 1ae83ed..98bf408 100644 --- a/tests/MockGuzzleTrait.php +++ b/tests/MockGuzzleTrait.php @@ -27,7 +27,7 @@ public function setupGuzzleClient(): Client return new Client(['handler' => $handlerStack]); } - public function mockGuzzleResponse(Response $response) + public function mockGuzzleResponse(Response $response): void { $this->guzzleHandler->append($response); }