diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..afda7bf --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# upgrade and run prettier +a60f11f94e6e5ba351411e4103eccd5c281a68bf \ No newline at end of file diff --git a/.prettierrc b/.prettierrc index 3088371..62e64c2 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,5 @@ { + "plugins": ["@prettier/plugin-php"], "phpVersion": "8.1", "singleQuote": true, "trailingCommaPHP": true diff --git a/.scenarios.lock/symfony4/composer.json b/.scenarios.lock/symfony4/composer.json index 93660a0..a7bcefe 100644 --- a/.scenarios.lock/symfony4/composer.json +++ b/.scenarios.lock/symfony4/composer.json @@ -23,11 +23,13 @@ "require-dev": { "symfony/cache": "^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^10.0", - "phpstan/phpstan": "^1.10", "mapado/php-cs-fixer-config": "^3.2", "g1a/composer-test-scenarios": "^3.0", "giggsey/libphonenumber-for-php": "^8.0", - "friendsofphp/php-cs-fixer": "^3.0.0" + "friendsofphp/php-cs-fixer": "^3.0.0", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", + "phpstan/extension-installer": "^1.3" }, "suggest": { "giggsey/libphonenumber-for-php": "^1.1 to manage integration with phone number bundle" diff --git a/.scenarios.lock/symfony4/composer.lock b/.scenarios.lock/symfony4/composer.lock index 7225287..bef2c5f 100644 --- a/.scenarios.lock/symfony4/composer.lock +++ b/.scenarios.lock/symfony4/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "31afb68d378d934156e63265f0e26b95", + "content-hash": "2daf88ba874ea49fe1b6fadf5a7e1706", "packages": [ { "name": "friendsofphp/proxy-manager-lts", @@ -2780,6 +2780,50 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.1" + }, + "time": "2024-06-10T08:20:49+00:00" + }, { "name": "phpstan/phpstan", "version": "1.11.8", @@ -2838,6 +2882,58 @@ ], "time": "2024-07-24T07:01:22+00:00" }, + { + "name": "phpstan/phpstan-phpunit", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-phpunit.git", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/f3ea021866f4263f07ca3636bf22c64be9610c11", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.11" + }, + "conflict": { + "phpunit/phpunit": "<7.0" + }, + "require-dev": { + "nikic/php-parser": "^4.13.0", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-strict-rules": "^1.5.1", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPUnit extensions and rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-phpunit/issues", + "source": "https://github.com/phpstan/phpstan-phpunit/tree/1.4.0" + }, + "time": "2024-04-20T06:39:00+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "10.1.15", diff --git a/.scenarios.lock/symfony5/composer.json b/.scenarios.lock/symfony5/composer.json index b9c4abd..120f541 100644 --- a/.scenarios.lock/symfony5/composer.json +++ b/.scenarios.lock/symfony5/composer.json @@ -23,11 +23,13 @@ "require-dev": { "symfony/cache": "^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^10.0", - "phpstan/phpstan": "^1.10", "mapado/php-cs-fixer-config": "^3.2", "g1a/composer-test-scenarios": "^3.0", "giggsey/libphonenumber-for-php": "^8.0", - "friendsofphp/php-cs-fixer": "^3.0.0" + "friendsofphp/php-cs-fixer": "^3.0.0", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", + "phpstan/extension-installer": "^1.3" }, "suggest": { "giggsey/libphonenumber-for-php": "^1.1 to manage integration with phone number bundle" diff --git a/.scenarios.lock/symfony5/composer.lock b/.scenarios.lock/symfony5/composer.lock index af42e3d..4c6acf3 100644 --- a/.scenarios.lock/symfony5/composer.lock +++ b/.scenarios.lock/symfony5/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "dafaa5f0f0454c2348aee22fbcda546c", + "content-hash": "0c8926337a9da0dadfee7f0c60ed558b", "packages": [ { "name": "friendsofphp/proxy-manager-lts", @@ -2557,6 +2557,50 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.1" + }, + "time": "2024-06-10T08:20:49+00:00" + }, { "name": "phpstan/phpstan", "version": "1.11.8", @@ -2615,6 +2659,58 @@ ], "time": "2024-07-24T07:01:22+00:00" }, + { + "name": "phpstan/phpstan-phpunit", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-phpunit.git", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/f3ea021866f4263f07ca3636bf22c64be9610c11", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.11" + }, + "conflict": { + "phpunit/phpunit": "<7.0" + }, + "require-dev": { + "nikic/php-parser": "^4.13.0", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-strict-rules": "^1.5.1", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPUnit extensions and rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-phpunit/issues", + "source": "https://github.com/phpstan/phpstan-phpunit/tree/1.4.0" + }, + "time": "2024-04-20T06:39:00+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "10.1.15", diff --git a/.scenarios.lock/symfony6/composer.json b/.scenarios.lock/symfony6/composer.json index fe0b7b7..6a76298 100644 --- a/.scenarios.lock/symfony6/composer.json +++ b/.scenarios.lock/symfony6/composer.json @@ -23,11 +23,13 @@ "require-dev": { "symfony/cache": "^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^10.0", - "phpstan/phpstan": "^1.10", "mapado/php-cs-fixer-config": "^3.2", "g1a/composer-test-scenarios": "^3.0", "giggsey/libphonenumber-for-php": "^8.0", - "friendsofphp/php-cs-fixer": "^3.0.0" + "friendsofphp/php-cs-fixer": "^3.0.0", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", + "phpstan/extension-installer": "^1.3" }, "suggest": { "giggsey/libphonenumber-for-php": "^1.1 to manage integration with phone number bundle" diff --git a/.scenarios.lock/symfony6/composer.lock b/.scenarios.lock/symfony6/composer.lock index 2bbb10c..a109e1e 100644 --- a/.scenarios.lock/symfony6/composer.lock +++ b/.scenarios.lock/symfony6/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "33e34bc751a3443ccfc4b79e15999608", + "content-hash": "794954cb714357e7864c37b296056f59", "packages": [ { "name": "friendsofphp/proxy-manager-lts", @@ -2550,6 +2550,50 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.1" + }, + "time": "2024-06-10T08:20:49+00:00" + }, { "name": "phpstan/phpstan", "version": "1.11.8", @@ -2608,6 +2652,58 @@ ], "time": "2024-07-24T07:01:22+00:00" }, + { + "name": "phpstan/phpstan-phpunit", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-phpunit.git", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/f3ea021866f4263f07ca3636bf22c64be9610c11", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.11" + }, + "conflict": { + "phpunit/phpunit": "<7.0" + }, + "require-dev": { + "nikic/php-parser": "^4.13.0", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-strict-rules": "^1.5.1", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPUnit extensions and rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-phpunit/issues", + "source": "https://github.com/phpstan/phpstan-phpunit/tree/1.4.0" + }, + "time": "2024-04-20T06:39:00+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "10.1.15", diff --git a/Tests/Units/Model/ModelHydratorTest.php b/Tests/Units/Model/ModelHydratorTest.php index e498655..9f0c018 100644 --- a/Tests/Units/Model/ModelHydratorTest.php +++ b/Tests/Units/Model/ModelHydratorTest.php @@ -4,7 +4,6 @@ namespace Mapado\RestClientSdk\Tests\Units\Model; -use PHPUnit\Framework\TestCase; use Mapado\RestClientSdk\Mapping; use Mapado\RestClientSdk\Mapping\Attribute; use Mapado\RestClientSdk\Mapping\ClassMetadata; @@ -12,93 +11,95 @@ use Mapado\RestClientSdk\Model\Serializer; use Mapado\RestClientSdk\SdkClient; use Mapado\RestClientSdk\UnitOfWork; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; /** - * @covers ModelHydrator + * @covers \ModelHydrator */ class ModelHydratorTest extends TestCase { - private $sdk; + private SdkClient&MockObject $sdk; - private $unitOfWork; + private UnitOfWork $unitOfWork; - public function setUp(): void + protected function setUp(): void { parent::setUp(); - $fooMetadata = new ClassMetadata("foo", "Acme\Foo", ""); + $fooMetadata = new ClassMetadata('foo', "Acme\Foo", ''); - $mapping = new Mapping("/v1"); + $mapping = new Mapping('/v1'); $mapping->setMapping([$fooMetadata]); $this->unitOfWork = new UnitOfWork($mapping); - $this->sdk = $this->createStub(\Mapado\RestClientSdk\SdkClient::class); - $this->sdk->method("getMapping")->willReturn($mapping); + $this->sdk = $this->createStub(SdkClient::class); + $this->sdk->method('getMapping')->willReturn($mapping); } - public function testConvertId() + public function testConvertId(): void { $testedInstance = new ModelHydrator($this->sdk); $this->assertEquals( - "/v1/foo/2", + '/v1/foo/2', $testedInstance->convertId(2, "Acme\Foo") ); $this->assertEquals( - "/v1/foo/2", - $testedInstance->convertId("/v1/foo/2", "Acme\Foo") + '/v1/foo/2', + $testedInstance->convertId('/v1/foo/2', "Acme\Foo") ); } - public function testConvertIdWithoutMappingPrefix() + public function testConvertIdWithoutMappingPrefix(): void { - $fooMetadata = new ClassMetadata("foo", "Acme\Foo", ""); + $fooMetadata = new ClassMetadata('foo', "Acme\Foo", ''); $mapping = new Mapping(); $mapping->setMapping([$fooMetadata]); $sdk = $this->createMock(SdkClient::class); - $sdk->method("getMapping")->willReturn($mapping); + $sdk->method('getMapping')->willReturn($mapping); $testedInstance = new ModelHydrator($sdk); $this->assertEquals( - "/foo/2", + '/foo/2', $testedInstance->convertId(2, "Acme\Foo") ); $this->assertEquals( - "/foo/2", - $testedInstance->convertId("/foo/2", "Acme\Foo") + '/foo/2', + $testedInstance->convertId('/foo/2', "Acme\Foo") ); } - public function testHydrateJsonLdItem() + public function testHydrateJsonLdItem(): void { $productMetadata = new ClassMetadata( - "product", + 'product', "Mapado\RestClientSdk\Tests\Model\JsonLd\Product", "Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository" ); $productMetadata->setAttributeList([ - new Attribute("@id", "id", "string", true), - new Attribute("value", "value", "string"), - new Attribute("currency", "currency", "string"), + new Attribute('@id', 'id', 'string', true), + new Attribute('value', 'value', 'string'), + new Attribute('currency', 'currency', 'string'), ]); $mapping = new Mapping(); $mapping->setMapping([$productMetadata]); $sdk = $this->createMock(SdkClient::class); - $sdk->method("getMapping")->willReturn($mapping); - $sdk->method("getSerializer")->willReturn( + $sdk->method('getMapping')->willReturn($mapping); + $sdk->method('getSerializer')->willReturn( new Serializer($mapping, $this->unitOfWork) ); $testedInstance = new ModelHydrator($sdk); // test one json-ld entity $productArray = json_decode( - file_get_contents(__DIR__ . "/../../data/product.json-ld.json"), + file_get_contents(__DIR__ . '/../../data/product.json-ld.json'), true ); @@ -112,11 +113,11 @@ public function testHydrateJsonLdItem() $product ); - $this->assertEquals("/products/1", $product->getId()); + $this->assertEquals('/products/1', $product->getId()); // test a json-ld list $productListArray = json_decode( - file_get_contents(__DIR__ . "/../../data/productList.json-ld.json"), + file_get_contents(__DIR__ . '/../../data/productList.json-ld.json'), true ); @@ -138,35 +139,35 @@ public function testHydrateJsonLdItem() "Mapado\RestClientSdk\Tests\Model\JsonLd\Product", $product ); - $this->assertSame("/products/1", $product->getId()); + $this->assertSame('/products/1', $product->getId()); - $this->assertSame("/products/2", $productList[1]->getId()); + $this->assertSame('/products/2', $productList[1]->getId()); } - public function testHydrateHalItem() + public function testHydrateHalItem(): void { $orderMetadata = new ClassMetadata( - "order", + 'order', "Mapado\RestClientSdk\Tests\Model\Hal\Order", "Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository" ); $orderMetadata->setAttributeList([ - new Attribute("_links.self.href", "id", "string", true), - new Attribute("total", "total", "float"), - new Attribute("currency", "currency", "string"), - new Attribute("status", "status", "string"), + new Attribute('_links.self.href', 'id', 'string', true), + new Attribute('total', 'total', 'float'), + new Attribute('currency', 'currency', 'string'), + new Attribute('status', 'status', 'string'), ]); $mapping = new Mapping(); $mapping->setConfig([ - "collectionKey" => "_embedded.ea:order", + 'collectionKey' => '_embedded.ea:order', ]); $mapping->setMapping([$orderMetadata]); $sdk = $this->createMock(SdkClient::class); - $sdk->method("getMapping")->willReturn($mapping); - $sdk->method("getSerializer")->willReturn( + $sdk->method('getMapping')->willReturn($mapping); + $sdk->method('getSerializer')->willReturn( new Serializer($mapping, $this->unitOfWork) ); @@ -174,7 +175,7 @@ public function testHydrateHalItem() // test one hal entity $orderArray = json_decode( - file_get_contents(__DIR__ . "/../../data/order.hal.json"), + file_get_contents(__DIR__ . '/../../data/order.hal.json'), true ); $order = $testedInstance->hydrate( @@ -186,12 +187,12 @@ public function testHydrateHalItem() "Mapado\RestClientSdk\Tests\Model\Hal\Order", $order ); - $this->assertSame("shipped", $order->getStatus()); - $this->assertSame("/orders/123", $order->getId()); + $this->assertSame('shipped', $order->getStatus()); + $this->assertSame('/orders/123', $order->getId()); // test a json-ld list $orderListArray = json_decode( - file_get_contents(__DIR__ . "/../../data/orderList.hal.json"), + file_get_contents(__DIR__ . '/../../data/orderList.hal.json'), true ); $orderList = $testedInstance->hydrateList( @@ -212,7 +213,7 @@ public function testHydrateHalItem() $order ); - $this->assertSame("/orders/123", $order->getId()); - $this->assertSame("/orders/124", $orderList[1]->getId()); + $this->assertSame('/orders/123', $order->getId()); + $this->assertSame('/orders/124', $orderList[1]->getId()); } } diff --git a/Tests/Units/Model/SerializerTest.php b/Tests/Units/Model/SerializerTest.php index e546cd0..e55376d 100644 --- a/Tests/Units/Model/SerializerTest.php +++ b/Tests/Units/Model/SerializerTest.php @@ -4,8 +4,6 @@ namespace Mapado\RestClientSdk\Tests\Units\Model; -use DateTime; -use DateTimeImmutable; use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; use Mapado\RestClientSdk\EntityRepository; @@ -28,14 +26,11 @@ use PHPUnit\Framework\TestCase; /** - * @covers Serializer + * @covers \Serializer */ class SerializerTest extends TestCase { - /** - * @var UnitOfWork - */ - private $unitOfWork; + private UnitOfWork $unitOfWork; private Serializer $testedInstance; @@ -55,8 +50,8 @@ public function testJsonEncode(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [], 'order' => null, @@ -77,7 +72,7 @@ public function testJsonEncode(): void $this->assertSame('/v1/carts/8', $cart->getId()); $this->assertSame('payed', $cart->getStatus()); $this->assertEquals( - new DateTime('2015-09-20T12:08:00'), + new \DateTime('2015-09-20T12:08:00'), $cart->getCreatedAt() ); $this->assertEmpty($cart->getCartItemList()); @@ -102,20 +97,20 @@ public function testJsonEncodeRelationWithLink(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ '@id' => '/v1/cart_items/16', 'amount' => 1, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-11-04 15:13:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-11-04 15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'Jane', ], 'cart' => '/v1/carts/8', @@ -157,19 +152,19 @@ public function testJsonEncodeRelationWithoutLink(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'product' => '/v1/products/10', @@ -210,19 +205,19 @@ public function testSerializeThreeLevel(): void [ 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'product' => '/v1/products/10', @@ -254,19 +249,19 @@ public function testJsonEncodeRelationWithoutLinkMultipleLevel(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'cartItemDetailList' => [ @@ -301,20 +296,20 @@ public function testJsonEncodeMixRelations(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ '@id' => '/v1/cart_items/16', 'amount' => 1, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-11-04 15:13:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-11-04 15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'Jane', ], 'cart' => '/v1/carts/8', @@ -323,13 +318,13 @@ public function testJsonEncodeMixRelations(): void ], [ 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'product' => '/v1/products/10', @@ -392,19 +387,19 @@ public function testMultipleLevelSerialization(): void [ 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'product' => '/v1/products/10', @@ -432,20 +427,20 @@ public function testLinkedUnserialize(): void '+330123456789', PhoneNumberFormat::INTERNATIONAL ), - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ '@id' => '/v1/cart_items/16', 'amount' => 2, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-09-20T12:11:00+00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-09-20T15:00:00+00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'John', ], 'product' => '/v1/products/10', @@ -483,13 +478,13 @@ public function testLinkedUnserialize(): void $this->assertSame('/v1/cart_items/16', $cartItem->getId()); $this->assertSame(2, $cartItem->getAmount()); $this->assertEquals( - new DateTime('2015-09-20T12:11:00+00:00'), + new \DateTime('2015-09-20T12:11:00+00:00'), $cartItem->getCreatedAt() ); $this->assertSame( [ - 'when' => (new DateTime('2015-09-20T15:00:00+00:00'))->format( - DateTime::RFC3339 + 'when' => (new \DateTime('2015-09-20T15:00:00+00:00'))->format( + \DateTime::RFC3339 ), 'who' => 'John', ], @@ -553,8 +548,8 @@ public function testSerializeNullValues(): void [ 'status' => null, 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [], 'order' => null, @@ -599,20 +594,20 @@ public function testWeirdIdentifier(): void 'weirdId' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'weirdId' => '/v1/cart_items/16', 'amount' => 1, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-11-04 15:13:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-11-04 15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'Jane', ], 'cart' => '/v1/carts/8', @@ -635,20 +630,20 @@ public function testWeirdIdentifier(): void 'weirdId' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [ [ 'weirdId' => '/v1/cart_items/16', 'amount' => 1, - 'createdAt' => (new DateTime( + 'createdAt' => (new \DateTime( '2015-11-04 15:13:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'data' => [ - 'when' => (new DateTime( + 'when' => (new \DateTime( '2015-11-04 15:00:00' - ))->format(DateTime::RFC3339), + ))->format(\DateTime::RFC3339), 'who' => 'Jane', ], 'cart' => '/v1/carts/8', @@ -694,8 +689,8 @@ public function testDeserializeWithExtraFields(): void '@id' => '/v1/carts/8', 'status' => 'payed', 'clientPhoneNumber' => '+33 1 23 45 67 89', - 'createdAt' => (new DateTime('2015-09-20T12:08:00'))->format( - DateTime::RFC3339 + 'createdAt' => (new \DateTime('2015-09-20T12:08:00'))->format( + \DateTime::RFC3339 ), 'cart_items' => [], 'order' => null, @@ -1110,7 +1105,7 @@ private function createNewCart(): \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart { $cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart(); $cart->setStatus('payed'); - $cart->setCreatedAt(new DateTime('2015-09-20 12:08:00')); + $cart->setCreatedAt(new \DateTime('2015-09-20 12:08:00')); $phoneNumberUtil = PhoneNumberUtil::getInstance(); $clientPhoneNumber = $phoneNumberUtil->parse( @@ -1135,9 +1130,9 @@ private function createKnownCartItem(): \Mapado\RestClientSdk\Tests\Model\JsonLd $cartItem = $this->createNewCartItem(); $cartItem->setId('/v1/cart_items/16'); $cartItem->setAmount(1); - $cartItem->setCreatedAt(new DateTimeImmutable('2015-11-04 15:13:00')); + $cartItem->setCreatedAt(new \DateTimeImmutable('2015-11-04 15:13:00')); $cartItem->setData([ - 'when' => new DateTimeImmutable('2015-11-04 15:00:00'), + 'when' => new \DateTimeImmutable('2015-11-04 15:00:00'), 'who' => 'Jane', ]); $cartItem->setCart($this->createCart()); @@ -1150,9 +1145,9 @@ private function createNewCartItem( ): \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem { $cartItem = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem(); $cartItem->setAmount(2); - $cartItem->setCreatedAt(new DateTimeImmutable('2015-09-20 12:11:00')); + $cartItem->setCreatedAt(new \DateTimeImmutable('2015-09-20 12:11:00')); $cartItem->setData([ - 'when' => new DateTime('2015-09-20 15:00:00'), + 'when' => new \DateTime('2015-09-20 15:00:00'), 'who' => 'John', ]); @@ -1197,9 +1192,9 @@ private function createNewInstance(Mapping $mapping = null): void $this->testedInstance = new Serializer($mapping, $this->unitOfWork); $restClient = $this->createMock( - \Mapado\RestClientSdk\RestClient::class + RestClient::class ); - $sdk = $this->getMockBuilder(\Mapado\RestClientSdk\SdkClient::class) + $sdk = $this->getMockBuilder(SdkClient::class) ->setConstructorArgs([ $restClient, $mapping, @@ -1239,7 +1234,7 @@ private function getCartRepositoryMock( string $modelName ): EntityRepository { $repository = $this->getMockBuilder( - \Mapado\RestClientSdk\EntityRepository::class + EntityRepository::class ) ->setConstructorArgs([$sdk, $restClient, $unitOfWork, $modelName]) ->getMock(); diff --git a/Tests/Units/SdkClientRegistryTest.php b/Tests/Units/SdkClientRegistryTest.php index 0b3a675..50c3cf3 100644 --- a/Tests/Units/SdkClientRegistryTest.php +++ b/Tests/Units/SdkClientRegistryTest.php @@ -8,10 +8,11 @@ use Mapado\RestClientSdk\Mapping; use Mapado\RestClientSdk\SdkClient; use Mapado\RestClientSdk\SdkClientRegistry; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** - * @covers SdkClientRegistry + * @covers \SdkClientRegistry */ class SdkClientRegistryTest extends TestCase { @@ -40,12 +41,8 @@ public function testGetSdkClientForClass(): void $sdkClientList = $this->createSdkClientList(['foo', 'bar']); $fooMapping = $this->createMock(Mapping::class); $barMapping = $this->createMock(Mapping::class); - $fooMapping->method('hasClassMetadata')->willReturnCallback(function ($name) { - return 0 === mb_strpos($name, 'Foo'); - }); - $barMapping->method('hasClassMetadata')->willReturnCallback(function ($name) { - return 0 === mb_strpos($name, 'Bar'); - }); + $fooMapping->method('hasClassMetadata')->willReturnCallback(fn ($name) => 0 === mb_strpos($name, 'Foo')); + $barMapping->method('hasClassMetadata')->willReturnCallback(fn ($name) => 0 === mb_strpos($name, 'Bar')); $sdkClientList['foo']->method('getMapping')->willReturn($fooMapping); $sdkClientList['bar']->method('getMapping')->willReturn($barMapping); @@ -65,7 +62,7 @@ public function testGetSdkClientForClass(): void /** * @param array $nameList * - * @return array + * @return array */ private function createSdkClientList(array $nameList): array { @@ -77,4 +74,4 @@ private function createSdkClientList(array $nameList): array return $sdkClientList; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index dee63b3..ae7924a 100644 --- a/composer.json +++ b/composer.json @@ -23,18 +23,20 @@ "require-dev": { "symfony/cache": "^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^10.0", - "phpstan/phpstan": "^1.10", "mapado/php-cs-fixer-config": "^3.2", "g1a/composer-test-scenarios": "^3.0", "giggsey/libphonenumber-for-php": "^8.0", - "friendsofphp/php-cs-fixer": "^3.0.0" + "friendsofphp/php-cs-fixer": "^3.0.0", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", + "phpstan/extension-installer": "^1.3" }, "suggest": { "giggsey/libphonenumber-for-php": "^1.1 to manage integration with phone number bundle" }, "scripts": { "test": [ - "vendor/bin/phpstan analyze src/", + "vendor/bin/phpstan analyze", "vendor/bin/phpunit" ], "post-install-cmd": [ diff --git a/composer.lock b/composer.lock index c59fca7..b7d9f80 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c9eb792c784fa13d784d7efec137764a", + "content-hash": "c21256c2efe7df5c0279b6c1c2d1fe1f", "packages": [ { "name": "friendsofphp/proxy-manager-lts", @@ -2278,18 +2278,62 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "reference": "f6b87faf9fc7978eab2f7919a8760bc9f58f9203", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.1" + }, + "time": "2024-06-10T08:20:49+00:00" + }, { "name": "phpstan/phpstan", - "version": "1.10.55", + "version": "1.11.8", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "9a88f9d18ddf4cf54c922fbeac16c4cb164c5949" + "reference": "6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9a88f9d18ddf4cf54c922fbeac16c4cb164c5949", - "reference": "9a88f9d18ddf4cf54c922fbeac16c4cb164c5949", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec", + "reference": "6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec", "shasum": "" }, "require": { @@ -2332,13 +2376,61 @@ { "url": "https://github.com/phpstan", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", - "type": "tidelift" } ], - "time": "2024-01-08T12:32:40+00:00" + "time": "2024-07-24T07:01:22+00:00" + }, + { + "name": "phpstan/phpstan-phpunit", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-phpunit.git", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/f3ea021866f4263f07ca3636bf22c64be9610c11", + "reference": "f3ea021866f4263f07ca3636bf22c64be9610c11", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.11" + }, + "conflict": { + "phpunit/phpunit": "<7.0" + }, + "require-dev": { + "nikic/php-parser": "^4.13.0", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-strict-rules": "^1.5.1", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPUnit extensions and rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-phpunit/issues", + "source": "https://github.com/phpstan/phpstan-phpunit/tree/1.4.0" + }, + "time": "2024-04-20T06:39:00+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/package.json b/package.json index da1eaa5..6ceb4fa 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "dependencies": { - "@prettier/plugin-php": "^0.19.7", + "@prettier/plugin-php": "^0.22.2", "husky": "^3.0.5", "lint-staged": "^9.4.0", - "prettier": "^1.18.2" + "prettier": "^3.3.3" }, "scripts": { "phpstan": "vendor/bin/phpstan analyse src/ -c phpstan.neon", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..8b095a5 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,66 @@ +parameters: + ignoreErrors: + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:addIdentifiers\\(\\) has parameter \\$dirtyFields with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:addIdentifiers\\(\\) has parameter \\$newSerializedModel with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:addIdentifiers\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:findOldRelation\\(\\) has parameter \\$oldValue with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:findOldRelation\\(\\) has parameter \\$relationValue with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:findOldRelation\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyData\\(\\) has parameter \\$newSerializedModel with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyData\\(\\) has parameter \\$oldSerializedModel with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyData\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyFields\\(\\) has parameter \\$newSerializedModel with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyFields\\(\\) has parameter \\$oldSerializedModel with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getDirtyFields\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php + + - + message: "#^Method Mapado\\\\RestClientSdk\\\\UnitOfWork\\:\\:getEntityId\\(\\) has parameter \\$stringOrEntity with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UnitOfWork.php diff --git a/phpstan.neon b/phpstan.neon index 0b19d65..a99a768 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,13 @@ +includes: + - 'phpstan-baseline.neon' + parameters: level: max - inferPrivatePropertyTypeFromConstructor: true ignoreErrors: [] - checkMissingIterableValueType: false - checkGenericClassInNonGenericObjectType: false + + typeAliases: + SerializerContext: 'array{serializeRelations?: array}' + + paths: + - src + # - Tests/Units diff --git a/src/Collection/Collection.php b/src/Collection/Collection.php index 3dbcc60..cd22caf 100644 --- a/src/Collection/Collection.php +++ b/src/Collection/Collection.php @@ -5,20 +5,18 @@ namespace Mapado\RestClientSdk\Collection; /** - * Class Collection + * @template E + * @template ExtraProperty * - * @author Florent Clerc + * @implements \IteratorAggregate + * @implements \ArrayAccess */ -class Collection implements - \IteratorAggregate, - \Serializable, - \Countable, - \ArrayAccess +class Collection implements \IteratorAggregate, \Serializable, \Countable, \ArrayAccess { /** * The elements of the collection. * - * @var array + * @var array */ private $elements; @@ -28,24 +26,24 @@ class Collection implements * or "hydra:totalItems" for JSON-LD * or anything you want to really ("foo" is OK for exemple) * - * @var array + * @var array */ private $extraProperties; /** - * @param array $elements the data elements as an array - * @param array $extraProperties the extra properties + * @param array $elements the data elements as an array + * @param array $extraProperties the extra properties */ public function __construct( array $elements = [], - array $extraProperties = [] + array $extraProperties = [], ) { $this->elements = $elements; $this->extraProperties = $extraProperties; } /** - * @return array + * @return array */ public function __serialize(): array { @@ -53,8 +51,6 @@ public function __serialize(): array } /** - * {@inheritdoc} - * * @param string $values */ public function __unserialize($values): void @@ -68,6 +64,8 @@ public function __unserialize($values): void /** * Returns inner elements collection. + * + * @return array */ public function toArray(): array { @@ -75,8 +73,6 @@ public function toArray(): array } /** - * {@inheritdoc} - * * @deprecated `serialize` method is deprecated, `__serialize` is used instead. See https://php.watch/versions/8.1/serializable-deprecated */ public function serialize(): string @@ -92,9 +88,6 @@ public function unserialize($data): void $this->__unserialize($data); } - /** - * {@inheritdoc} - */ public function count(): int { return count($this->elements); @@ -109,10 +102,9 @@ public function getTotalItems(): int } /** - * {@inheritdoc} - * * @param mixed|null $offset - * @param mixed $value + * + * @phpstan-param E $value */ public function offsetSet($offset, $value): void { @@ -124,8 +116,6 @@ public function offsetSet($offset, $value): void } /** - * {@inheritdoc} - * * @param mixed|null $offset */ public function offsetExists($offset): bool @@ -134,8 +124,6 @@ public function offsetExists($offset): bool } /** - * {@inheritdoc} - * * @param mixed|null $offset */ public function offsetUnset($offset): void @@ -144,11 +132,11 @@ public function offsetUnset($offset): void } /** - * {@inheritdoc} - * * @param mixed|null $offset * * @return mixed|null + * + * @phpstan-return E|null */ public function offsetGet($offset): mixed { @@ -156,7 +144,7 @@ public function offsetGet($offset): mixed } /** - * {@inheritdoc} + * @return \ArrayIterator */ public function getIterator(): \ArrayIterator { @@ -164,7 +152,7 @@ public function getIterator(): \ArrayIterator } /** - * getExtraProperties + * @return array */ public function getExtraProperties(): array { @@ -188,12 +176,14 @@ public function getIntExtraProperty(string $key): ?int /** * return the value of an extra property * - * @return mixed + * @phpstan-return ?ExtraProperty */ public function getExtraProperty(string $key) { if (isset($this->extraProperties[$key])) { return $this->extraProperties[$key]; } + + return null; } } diff --git a/src/Collection/HalCollection.php b/src/Collection/HalCollection.php index b963bf4..94e7704 100644 --- a/src/Collection/HalCollection.php +++ b/src/Collection/HalCollection.php @@ -5,9 +5,9 @@ namespace Mapado\RestClientSdk\Collection; /** - * Class HalCollection - * - * @author Julien Deniau + * @template E + * @template ExtraProperty + * @extends Collection */ class HalCollection extends Collection { diff --git a/src/Collection/HydraPaginatedCollection.php b/src/Collection/HydraPaginatedCollection.php index a766666..e5b27f0 100644 --- a/src/Collection/HydraPaginatedCollection.php +++ b/src/Collection/HydraPaginatedCollection.php @@ -5,9 +5,9 @@ namespace Mapado\RestClientSdk\Collection; /** - * Class HydraPaginatedCollection - * - * @author Florent Clerc + * @template E + * @template ExtraProperty + * @extends Collection */ class HydraPaginatedCollection extends Collection { diff --git a/src/EntityRepository.php b/src/EntityRepository.php index 65cde95..42bec2f 100644 --- a/src/EntityRepository.php +++ b/src/EntityRepository.php @@ -13,37 +13,33 @@ use Mapado\RestClientSdk\Mapping\ClassMetadata; use Psr\Http\Message\ResponseInterface; +/** + * @template E of object + * @template ExtraParams + */ class EntityRepository { /** - * REST Client. - * * @var RestClient */ protected $restClient; /** - * SDK Client. - * * @var SdkClient */ protected $sdk; /** - * @var class-string + * @var class-string */ protected $entityName; /** - * classMetadataCache - * * @var ClassMetadata */ private $classMetadataCache; /** - * unitOfWork - * * @var UnitOfWork */ private $unitOfWork; @@ -53,13 +49,13 @@ class EntityRepository * * @param SdkClient $sdkClient The client to connect to the datasource with * @param RestClient $restClient The client to process the http requests - * @param class-string $entityName The entity to work with + * @param class-string $entityName The entity to work with */ public function __construct( SdkClient $sdkClient, RestClient $restClient, UnitOfWork $unitOfWork, - string $entityName + string $entityName, ) { $this->sdk = $sdkClient; $this->restClient = $restClient; @@ -70,7 +66,9 @@ public function __construct( /** * Adds support for magic finders. * - * @return array|object|null the found entity/entities + * @param array $arguments + * + * @return array|object|null the found entity/entities */ public function __call(string $method, array $arguments) { @@ -86,12 +84,18 @@ public function __call(string $method, array $arguments) break; default: - throw new \BadMethodCallException('Undefined method \'' . $method . '\'. The method name must start with - either findBy or findOneBy!'); + throw new \BadMethodCallException( + 'Undefined method \'' . + $method . + '\'. The method name must start with + either findBy or findOneBy!', + ); } if (empty($arguments)) { - throw new SdkException('You need to pass a parameter to ' . $method); + throw new SdkException( + 'You need to pass a parameter to ' . $method, + ); } $mapping = $this->sdk->getMapping(); @@ -102,6 +106,7 @@ public function __call(string $method, array $arguments) if (!empty($fieldName)) { $queryParams = [$fieldName => current($arguments)]; } else { + /** @var array $queryParams */ $queryParams = current($arguments); } $path .= @@ -130,7 +135,7 @@ public function __call(string $method, array $arguments) if (null !== $hydratedData) { $this->unitOfWork->registerClean( $identifier, - $hydratedData + $hydratedData, ); } $this->saveToCache($identifier, $hydratedData); @@ -160,7 +165,9 @@ public function __call(string $method, array $arguments) /** * find - finds one item of the entity based on the @REST\Id field in the entity * - * @param array $queryParams query parameters to add to the query + * @param array $queryParams query parameters to add to the query + * + * @phpstan-return ?E */ public function find(string|int $id, array $queryParams = []): ?object { @@ -170,6 +177,7 @@ public function find(string|int $id, array $queryParams = []): ?object $id = $this->addQueryParameter($id, $queryParams); // if entity is found in cache, return it + /** @var ?E */ $entityFromCache = $this->fetchFromCache($id); if ($entityFromCache) { return $entityFromCache; @@ -178,6 +186,7 @@ public function find(string|int $id, array $queryParams = []): ?object $data = $this->restClient->get($id); $data = $this->assertNotObject($data, __METHOD__); + /** @var ?E */ $entity = $hydrator->hydrate($data, $this->entityName); // cache entity @@ -189,6 +198,9 @@ public function find(string|int $id, array $queryParams = []): ?object return $entity; } + /** + * @return Collection + */ public function findAll(): Collection { $mapping = $this->sdk->getMapping(); @@ -201,7 +213,11 @@ public function findAll(): Collection // if entityList is found in cache, return it if (false !== $entityListFromCache) { if (!$entityListFromCache instanceof Collection) { - throw new \RuntimeException('Entity list in cache should be an instance of ' . Collection::class . '. This should not happen.'); + throw new \RuntimeException( + 'Entity list in cache should be an instance of ' . + Collection::class . + '. This should not happen.', + ); } return $entityListFromCache; @@ -211,6 +227,7 @@ public function findAll(): Collection $data = $this->assertNotObject($data, __METHOD__); $hydrator = $this->sdk->getModelHydrator(); + /** @var Collection */ $entityList = $hydrator->hydrateList($data, $this->entityName); // cache entity list @@ -219,7 +236,9 @@ public function findAll(): Collection // then cache each entity from list foreach ($entityList as $entity) { if (!is_object($entity)) { - throw new \RuntimeException("Entity should be an object. This should not happen."); + throw new \RuntimeException( + 'Entity should be an object. This should not happen.', + ); } $identifier = $entity->{$this->getClassMetadata()->getIdGetter()}(); @@ -234,6 +253,8 @@ public function findAll(): Collection /** * remove entity * + * @phpstan-param E $model + * * @TODO STILL NEEDS TO BE CONVERTED TO ENTITY MODEL */ public function remove(object $model): void @@ -245,17 +266,23 @@ public function remove(object $model): void $this->restClient->delete($identifier); } + /** + * @phpstan-param E $model + * @phpstan-param SerializerContext $serializationContext + * + * @param array $queryParams + */ public function update( object $model, array $serializationContext = [], - array $queryParams = [] + array $queryParams = [], ): object { $identifier = $model->{$this->getClassMetadata()->getIdGetter()}(); $serializer = $this->sdk->getSerializer(); $newSerializedModel = $serializer->serialize( $model, $this->entityName, - $serializationContext + $serializationContext, ); $oldModel = $this->unitOfWork->getDirtyEntity($identifier); @@ -263,12 +290,12 @@ public function update( $oldSerializedModel = $serializer->serialize( $oldModel, $this->entityName, - $serializationContext + $serializationContext, ); $newSerializedModel = $this->unitOfWork->getDirtyData( $newSerializedModel, $oldSerializedModel, - $this->getClassMetadata() + $this->getClassMetadata(), ); } @@ -283,16 +310,24 @@ public function update( $out = $hydrator->hydrate($data, $this->entityName); if (null === $out) { - throw new HydratorException("Unable to convert data from PUT request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?"); + throw new HydratorException( + "Unable to convert data from PUT request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?", + ); } return $out; } + /** + * @phpstan-param E $model + * @phpstan-param SerializerContext $serializationContext + * + * @param array $queryParams + */ public function persist( object $model, array $serializationContext = [], - array $queryParams = [] + array $queryParams = [], ): object { $mapping = $this->sdk->getMapping(); $prefix = $mapping->getIdPrefix(); @@ -308,17 +343,20 @@ public function persist( $diff = $this->unitOfWork->getDirtyData( $newSerializedModel, $oldSerializedModel, - $this->getClassMetadata() + $this->getClassMetadata(), ); $data = $this->restClient->post( $this->addQueryParameter($path, $queryParams), - $diff + $diff, ); $data = $this->assertNotObject($data, __METHOD__); if (null === $data) { - throw new RestException("No data found after sending a `POST` request to {$path}. Did the server returned a 4xx or 5xx status code?", $path); + throw new RestException( + "No data found after sending a `POST` request to {$path}. Did the server returned a 4xx or 5xx status code?", + $path, + ); } $hydrator = $this->sdk->getModelHydrator(); @@ -326,7 +364,9 @@ public function persist( $out = $hydrator->hydrate($data, $this->entityName); if (null === $out) { - throw new HydratorException("Unable to convert data from POST request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?"); + throw new HydratorException( + "Unable to convert data from POST request ({$path}) to an instance of {$this->entityName}. Maybe you have a custom hydrator returning null?", + ); } return $out; @@ -343,7 +383,9 @@ protected function fetchFromCache(string $key): object|false $cacheData = $cacheItem->get(); if (!is_object($cacheData)) { - throw new \RuntimeException('Cache data should be an object. This should not happen.'); + throw new \RuntimeException( + 'Cache data should be an object. This should not happen.', + ); } return $cacheData; @@ -388,9 +430,12 @@ protected function removeFromCache(string $key): bool return true; } + /** + * @param array $params + */ protected function addQueryParameter( string $path, - array $params = [] + array $params = [], ): string { if (empty($params)) { return $path; @@ -399,13 +444,18 @@ protected function addQueryParameter( return $path . '?' . http_build_query($params); } + /** + * @param array $queryParameters + * + * @return array + */ private function convertQueryParameters(array $queryParameters): array { $mapping = $this->sdk->getMapping(); return array_map(function ($item) use ($mapping) { if (is_object($item)) { - $classname = get_class($item); + $classname = $item::class; if ($mapping->hasClassMetadata($classname)) { $idGetter = $mapping @@ -425,7 +475,9 @@ private function normalizeCacheKey(string $key): string $out = preg_replace('~[\\/\{\}@:\(\)]~', '_', $key); if (null === $out) { - throw new \RuntimeException('Unable to normalize cache key. This should not happen.'); + throw new \RuntimeException( + 'Unable to normalize cache key. This should not happen.', + ); } return $out; @@ -443,7 +495,13 @@ private function getClassMetadata(): ClassMetadata } /** - * @param array|ResponseInterface|null $data + * @template I + * + * @param array|ResponseInterface|null $data + * + * @phpstan-assert array $data + * + * @return array */ private function assertArray($data, string $methodName): array { @@ -451,15 +509,21 @@ private function assertArray($data, string $methodName): array return $data; } - $type = null === $data ? 'null' : get_class($data); + $type = null === $data ? 'null' : $data::class; - throw new UnexpectedTypeException("Return of method {$methodName} should be an array. {$type} given."); + throw new UnexpectedTypeException( + "Return of method {$methodName} should be an array. {$type} given.", + ); } /** - * @param array|ResponseInterface|null $data + * @template I + * + * @param array|ResponseInterface|null $data * - * @return array|null + * @phpstan-assert array|null $data + * + * @return array|null */ private function assertNotObject($data, string $methodName) { @@ -467,8 +531,10 @@ private function assertNotObject($data, string $methodName) return $data; } - $type = get_class($data); + $type = $data::class; - throw new UnexpectedTypeException("Return of method {$methodName} should be an array. {$type} given."); + throw new UnexpectedTypeException( + "Return of method {$methodName} should be an array. {$type} given.", + ); } } diff --git a/src/Exception/RestException.php b/src/Exception/RestException.php index e42352a..cd430e3 100644 --- a/src/Exception/RestException.php +++ b/src/Exception/RestException.php @@ -4,16 +4,10 @@ namespace Mapado\RestClientSdk\Exception; -use Exception; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -/** - * Class RestException - * - * @author Julien Deniau - */ class RestException extends \RuntimeException { /** @@ -22,7 +16,7 @@ class RestException extends \RuntimeException private $path; /** - * @var array + * @var array */ private $params; @@ -36,12 +30,13 @@ class RestException extends \RuntimeException */ private $request; + /** @param array $params */ public function __construct( string $message, string $path, array $params = [], int $code = 0, - ?Exception $previous = null + \Exception $previous = null, ) { parent::__construct($message, $code, $previous); $this->path = $path; @@ -57,6 +52,7 @@ public function getPath(): string return $this->path; } + /** @return array */ public function getParams(): array { return $this->params; diff --git a/src/Helper/ArrayHelper.php b/src/Helper/ArrayHelper.php index 51fc1b2..efbccb2 100644 --- a/src/Helper/ArrayHelper.php +++ b/src/Helper/ArrayHelper.php @@ -8,21 +8,22 @@ * Some array helpers. * Greatly inspired by Laravel's helper: https://github.com/rappasoft/laravel-helpers * - * @author Julien Deniau + * @template T */ class ArrayHelper { /** * Get an item from an array using "dot" notation. * - * @param mixed $default + * @param array $array + * @param T $default * - * @return mixed + * @return array|T */ public static function arrayGet( array $array, ?string $key, - $default = null + $default = null, ): mixed { if (null === $key) { return $array; @@ -44,6 +45,8 @@ public static function arrayGet( /** * Check if an item exists in an array using "dot" notation. + * + * @param array $array */ public static function arrayHas(array $array, ?string $key): bool { @@ -68,6 +71,10 @@ public static function arrayHas(array $array, ?string $key): bool /** * Flatten a multi-dimensional associative array with dots. + * + * @param array $array + * + * @return array */ public static function arrayDot(array $array, string $prepend = ''): array { @@ -76,7 +83,7 @@ public static function arrayDot(array $array, string $prepend = ''): array if (is_array($value) && !empty($value)) { $results = array_merge( $results, - static::arrayDot($value, $prepend . $key . '.') + static::arrayDot($value, $prepend . $key . '.'), ); } else { $results[$prepend . $key] = $value; @@ -86,16 +93,26 @@ public static function arrayDot(array $array, string $prepend = ''): array return $results; } + /** + * @param array $array1 + * @param array $array2 + * + * @return array + */ public static function arrayDiffAssocRecursive( array $array1, - array $array2 + array $array2, ): array { return array_diff_assoc( static::arrayDot($array1), - static::arrayDot($array2) + static::arrayDot($array2), ); } + /** + * @param array $array1 + * @param array $array2 + */ public static function arraySame(array $array1, array $array2): bool { return empty(static::arrayDiffAssocRecursive($array1, $array2)); @@ -104,9 +121,9 @@ public static function arraySame(array $array1, array $array2): bool /** * Return the default value of the given value. * - * @param mixed $value + * @param T|\Closure $value * - * @return mixed + * @return T */ public static function value($value) { diff --git a/src/Mapping.php b/src/Mapping.php index a11d7c9..6e1819d 100644 --- a/src/Mapping.php +++ b/src/Mapping.php @@ -8,9 +8,8 @@ use Mapado\RestClientSdk\Mapping\ClassMetadata; /** - * Class Mapping - * - * @author Julien Deniau + * @phpstan-type MappingConfigInput array{collectionKey?: string} + * @phpstan-type MappingConfig array{collectionKey: string} */ class Mapping { @@ -32,10 +31,15 @@ class Mapping private $classMetadataList = []; /** - * @var array + * @var array + * + * @phpstan-var MappingConfig */ private $config; + /** + * @phpstan-param MappingConfigInput $config + */ public function __construct(string $idPrefix = '', array $config = []) { $this->idPrefix = $idPrefix; @@ -48,11 +52,17 @@ public function getIdPrefix(): string return $this->idPrefix; } + /** + * @phpstan-return MappingConfig + */ public function getConfig(): array { return $this->config; } + /** + * @phpstan-param MappingConfigInput $config + */ public function setConfig(array $config): self { $this->config = array_merge(self::DEFAULT_CONFIG, $config); @@ -92,9 +102,7 @@ public function getModelName(string $key): string */ public function getMappingKeys(): array { - return array_map(function (ClassMetadata $classMetadata) { - return $classMetadata->getKey(); - }, $this->classMetadataList); + return array_map(fn (ClassMetadata $classMetadata) => $classMetadata->getKey(), $this->classMetadataList); } /** @@ -126,7 +134,7 @@ public function getKeyFromModel(string $modelName): string } throw new MappingException( - 'Model name ' . $modelName . ' not found in mapping' + 'Model name ' . $modelName . ' not found in mapping', ); } @@ -201,7 +209,7 @@ private function parseKeyFromId(string $id): ?string private function checkMappingExistence( string $key, - bool $checkModelName = false + bool $checkModelName = false, ): void { if (empty($key)) { throw new MappingException('key is not set'); @@ -215,7 +223,7 @@ private function checkMappingExistence( if ($checkModelName) { if (empty($metadata->getModelName())) { throw new MappingException( - $key . ' key is mapped but the model name is empty' + $key . ' key is mapped but the model name is empty', ); } } @@ -224,8 +232,8 @@ private function checkMappingExistence( private function removePrefix(string $value): string { if ( - $this->idPrefixLength > 0 && - 0 === mb_strpos($value, $this->idPrefix) + $this->idPrefixLength > 0 + && 0 === mb_strpos($value, $this->idPrefix) ) { return mb_substr($value, $this->idPrefixLength); } diff --git a/src/Mapping/Attribute.php b/src/Mapping/Attribute.php index 84b8edb..3ade600 100644 --- a/src/Mapping/Attribute.php +++ b/src/Mapping/Attribute.php @@ -36,9 +36,9 @@ class Attribute */ public function __construct( string $serializedKey, - ?string $attributeName = null, - ?string $type = null, - bool $isIdentifier = false + string $attributeName = null, + string $type = null, + bool $isIdentifier = false, ) { if (empty($serializedKey)) { throw new \InvalidArgumentException('attribute name must be set'); diff --git a/src/Mapping/Attributes/Attribute.php b/src/Mapping/Attributes/Attribute.php index 906a722..fd521d7 100644 --- a/src/Mapping/Attributes/Attribute.php +++ b/src/Mapping/Attributes/Attribute.php @@ -1,11 +1,15 @@ key = $key; $this->modelName = $modelName; @@ -83,6 +83,7 @@ public function getModelName(): string /** * @param class-string $modelName + * * @return $this */ public function setModelName(string $modelName): self @@ -124,8 +125,8 @@ public function getIdentifierAttribute(): Attribute sprintf( 'Ressource "%s" does not contains an identifier. You can not call %s. You may want to call `hasIdentifierAttribute` before.', $this->modelName, - __METHOD__ - ) + __METHOD__, + ), ); } @@ -158,8 +159,8 @@ public function setAttributeList($attributeList): self sprintf( 'Class metadata for model "%s" already has an identifier named "%s". Only one identifier is allowed.', $this->modelName, - $this->identifierAttribute->getSerializedKey() - ) + $this->identifierAttribute->getSerializedKey(), + ), ); } @@ -236,7 +237,7 @@ public function getIdSerializeKey(): string /** * return default serialize model with null value or empty array on relations * - * @return array + * @return array|null> */ public function getDefaultSerializedModel(): array { diff --git a/src/Mapping/Driver/AttributeDriver.php b/src/Mapping/Driver/AttributeDriver.php index d6be5ef..dcc69f5 100644 --- a/src/Mapping/Driver/AttributeDriver.php +++ b/src/Mapping/Driver/AttributeDriver.php @@ -30,21 +30,23 @@ public function loadDirectory(string $path): array new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path, - \FilesystemIterator::SKIP_DOTS + \FilesystemIterator::SKIP_DOTS, ), - \RecursiveIteratorIterator::LEAVES_ONLY + \RecursiveIteratorIterator::LEAVES_ONLY, ), '/^.+\.php$/i', - \RecursiveRegexIterator::GET_MATCH + \RecursiveRegexIterator::GET_MATCH, ); $classes = []; $includedFiles = []; - /** @var array $file */ foreach ($iterator as $file) { - $sourceFile = $file[0] ?? null; - if (is_string($sourceFile) && !preg_match('(^phar:)i', $sourceFile)) { + $sourceFile = is_array($file) ? $file[0] : null; + if ( + is_string($sourceFile) + && !preg_match('(^phar:)i', $sourceFile) + ) { $sourceFile = realpath($sourceFile); } @@ -90,10 +92,13 @@ public function loadClassname(string $classname): array * @throws \ReflectionException */ private function getClassMetadataForClassname( - string $classname + string $classname, ): ?ClassMetadata { $reflClass = new \ReflectionClass($classname); - $classAttribute = $this->getClassAttribute($reflClass, Attributes\Entity::class); + $classAttribute = $this->getClassAttribute( + $reflClass, + Attributes\Entity::class, + ); if (!$classAttribute) { return null; @@ -103,27 +108,39 @@ private function getClassMetadataForClassname( $relationList = []; foreach ($reflClass->getProperties() as $property) { // manage attributes - $propertyAttribute = $this->getPropertyAttribute($property, Attributes\Attribute::class); + $propertyAttribute = $this->getPropertyAttribute( + $property, + Attributes\Attribute::class, + ); if ($propertyAttribute) { - $propertyIdAttribute = $this->getPropertyAttribute($property, Attributes\Id::class); + $propertyIdAttribute = $this->getPropertyAttribute( + $property, + Attributes\Id::class, + ); $attributeList[] = new Attribute( $propertyAttribute->name, $property->getName(), $propertyAttribute->type, - $propertyIdAttribute instanceof Attributes\Id + $propertyIdAttribute instanceof Attributes\Id, ); } else { - $relation = $this->getPropertyAttribute($property, Attributes\OneToMany::class); + $relation = $this->getPropertyAttribute( + $property, + Attributes\OneToMany::class, + ); if (!$relation) { - $relation = $this->getPropertyAttribute($property, Attributes\ManyToOne::class); + $relation = $this->getPropertyAttribute( + $property, + Attributes\ManyToOne::class, + ); } if ($relation) { $attributeList[] = new Attribute( $relation->name, - $property->getName() + $property->getName(), ); $targetEntity = $relation->targetEntity; @@ -132,14 +149,14 @@ private function getClassMetadataForClassname( mb_substr( $classname, 0, - mb_strrpos($classname, '\\') + 1 + mb_strrpos($classname, '\\') + 1, ) . $targetEntity; } $relationList[] = new Relation( $relation->name, $relation->type, - $targetEntity + $targetEntity, ); } } @@ -148,7 +165,7 @@ private function getClassMetadataForClassname( $classMetadata = new ClassMetadata( $classAttribute->key, $classname, - $classAttribute->repository + $classAttribute->repository, ); $classMetadata->setAttributeList($attributeList); $classMetadata->setRelationList($relationList); @@ -157,39 +174,51 @@ private function getClassMetadataForClassname( } /** - * @template T + * @template T of object * * @param class-string $classname * - * @return T|null + * @return new|null */ - private function getPropertyAttribute(\ReflectionProperty $property, string $classname) - { + private function getPropertyAttribute( + \ReflectionProperty $property, + string $classname, + ) { return $this->getAttribute($property, $classname); } /** - * @template T + * @template T of Attributes\Entity * - * @param class-string $className + * @param class-string $classname + * @param \ReflectionClass $reflectionClass * - * @return T|null + * @return new|null */ - private function getClassAttribute(\ReflectionClass $reflectionClass, string $className) - { - return $this->getAttribute($reflectionClass, $className); + private function getClassAttribute( + \ReflectionClass $reflectionClass, + string $classname, + ) { + return $this->getAttribute($reflectionClass, $classname); } /** - * @template T + * @template T of object * - * @param class-string $className + * @param \ReflectionClass|\ReflectionProperty $reflection + * @param class-string $classname * - * @return T|null + * @return new|null */ - private function getAttribute(\ReflectionClass|\ReflectionProperty $reflection, string $className) - { - $attribute = $reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null; + private function getAttribute( + \ReflectionClass|\ReflectionProperty $reflection, + string $classname, + ) { + $attribute = + $reflection->getAttributes( + $classname, + \ReflectionAttribute::IS_INSTANCEOF, + )[0] ?? null; if (!$attribute instanceof \ReflectionAttribute) { return null; @@ -197,4 +226,4 @@ private function getAttribute(\ReflectionClass|\ReflectionProperty $reflection, return $attribute->newInstance(); } -} \ No newline at end of file +} diff --git a/src/Mapping/Relation.php b/src/Mapping/Relation.php index 357ec56..f574195 100644 --- a/src/Mapping/Relation.php +++ b/src/Mapping/Relation.php @@ -32,7 +32,7 @@ class Relation public function __construct( string $serializedKey, string $type, - string $targetEntity + string $targetEntity, ) { $this->serializedKey = $serializedKey; $this->type = $type; diff --git a/src/Model/ModelHydrator.php b/src/Model/ModelHydrator.php index eb35515..d6e5e58 100644 --- a/src/Model/ModelHydrator.php +++ b/src/Model/ModelHydrator.php @@ -10,11 +10,6 @@ use Mapado\RestClientSdk\Helper\ArrayHelper; use Mapado\RestClientSdk\SdkClient; -/** - * Class ModelHydrator - * - * @author Julien Deniau - */ class ModelHydrator { /** @@ -45,6 +40,10 @@ public function convertId(string|int $id, string $modelName): string return $id; } + /** + * @param array|null $data + * @param class-string $modelName + */ public function hydrate(?array $data, string $modelName): ?object { $mapping = $this->sdk->getMapping(); @@ -57,7 +56,10 @@ public function hydrate(?array $data, string $modelName): ?object /** * convert API response to Collection containing entities * + * @param ?array $data * @param class-string $modelName + * + * @return Collection */ public function hydrateList(?array $data, string $modelName): Collection { @@ -73,7 +75,10 @@ public function hydrateList(?array $data, string $modelName): Collection /** * convert list of data as array to Collection containing entities * + * @param array $data * @param class-string $modelName + * + * @return Collection */ private function deserializeAll(array $data, string $modelName): Collection { @@ -82,34 +87,44 @@ private function deserializeAll(array $data, string $modelName): Collection $itemList = ArrayHelper::arrayGet($data, $collectionKey); if (!is_array($itemList)) { - throw new \RuntimeException(sprintf( - 'Unable to deserialize collection, %s key not found in response', - $collectionKey - )); + throw new \RuntimeException( + sprintf( + 'Unable to deserialize collection, %s key not found in response', + $collectionKey, + ), + ); } - $itemList = array_map(fn (?array $member) => $this->deserialize($member, $modelName), $itemList); + $itemList = array_map( + fn(?array $member) => $this->deserialize($member, $modelName), + $itemList, + ); $extraProperties = array_filter( $data, - fn ($key) => $key !== $collectionKey, - \ARRAY_FILTER_USE_KEY + fn($key) => $key !== $collectionKey, + \ARRAY_FILTER_USE_KEY, ); /** @var class-string $collectionClassName */ $collectionClassName = $this->guessCollectionClassname($data); if (!class_exists($collectionClassName)) { - throw new \RuntimeException("Seem's like $collectionClassName does not exist"); + throw new \RuntimeException( + "Seem's like $collectionClassName does not exist", + ); } - /** @var Collection */ - return new $collectionClassName($itemList, $extraProperties); + /** @var Collection */ + $out = new $collectionClassName($itemList, $extraProperties); + + return $out; } /** * convert array to entity * + * @param array|null $data * @param class-string $modelName */ private function deserialize(?array $data, string $modelName): ?object @@ -123,6 +138,8 @@ private function deserialize(?array $data, string $modelName): ?object /** * guess collection classname according to response data + * + * @param array $data */ private function guessCollectionClassname(array $data): string { diff --git a/src/Model/Serializer.php b/src/Model/Serializer.php index c97ddbf..6f98f7f 100644 --- a/src/Model/Serializer.php +++ b/src/Model/Serializer.php @@ -4,9 +4,7 @@ namespace Mapado\RestClientSdk\Model; -use DateTime; use DateTimeImmutable; -use DateTimeInterface; use libphonenumber\PhoneNumber; use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; @@ -22,9 +20,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessor; /** - * Class Serializer - * - * @author Julien Deniau + * @phpstan-type SerializerData array */ class Serializer { @@ -69,17 +65,21 @@ public function setSdk(SdkClient $sdk): self /** * serialize entity for POST and PUT + * + * @phpstan-param SerializerContext $context + * + * @return array */ public function serialize( object $entity, string $modelName, - array $context = [] + array $context = [], ): array { $out = $this->recursiveSerialize($entity, $modelName, 0, $context); if (is_string($out)) { throw new \RuntimeException( - 'recursiveSerialize should return an array for level 0 of serialization. This should not happen.' + 'recursiveSerialize should return an array for level 0 of serialization. This should not happen.', ); } @@ -87,7 +87,7 @@ public function serialize( } /** - * @param array $data + * @param SerializerData $data * @param class-string $className */ public function deserialize(array $data, string $className): object @@ -102,7 +102,7 @@ public function deserialize(array $data, string $className): object if (!is_object($instance)) { throw new \RuntimeException( - "The class $className is not instantiable" + "The class $className is not instantiable", ); } @@ -126,13 +126,13 @@ public function deserialize(array $data, string $className): object } elseif (is_array($value)) { $targetEntity = $relation->getTargetEntity(); $relationClassMetadata = $this->mapping->getClassMetadata( - $targetEntity + $targetEntity, ); if ($relation->isManyToOne()) { $value = $this->deserialize( $value, - $relationClassMetadata->getModelName() + $relationClassMetadata->getModelName(), ); } else { // One-To-Many association @@ -143,7 +143,7 @@ public function deserialize(array $data, string $className): object } elseif (is_array($item)) { $list[] = $this->deserialize( $item, - $relationClassMetadata->getModelName() + $relationClassMetadata->getModelName(), ); } } @@ -157,20 +157,20 @@ public function deserialize(array $data, string $className): object if ('datetime' === $attribute->getType()) { if (!is_string($value)) { throw new \RuntimeException( - "The value for $attributeName to cast to datetime value should be a string" + "The value for $attributeName to cast to datetime value should be a string", ); } $this->setDateTimeValue( $instance, $attributeName, - $value + $value, ); } else { $this->propertyAccessor->setValue( $instance, $attributeName, - $value + $value, ); } } @@ -190,7 +190,7 @@ public function deserialize(array $data, string $className): object if ($identifier) { $this->unitOfWork->registerClean( (string) $identifier, - $instance + $instance, ); } } @@ -202,14 +202,16 @@ public function deserialize(array $data, string $className): object /** * If provided class name is abstract (a base class), the real class name (child class) * may be available in some data fields. + * + * @phpstan-param SerializerData $data */ private function resolveRealClassName( array $data, - string $className + string $className, ): string { - if (!empty($data['@id'])) { + if (!empty($data['@id']) && is_string($data['@id'])) { $classMetadata = $this->mapping->tryGetClassMetadataById( - $data['@id'] + $data['@id'], ); if ($classMetadata) { @@ -222,13 +224,15 @@ private function resolveRealClassName( } /** - * @return array|string + * @param array{"serializeRelation"?: bool}|SerializerContext $context + * + * @return array|string */ private function recursiveSerialize( object $entity, string $modelName, int $level = 0, - array $context = [] + array $context = [], ) { $classMetadata = $this->mapping->getClassMetadata($modelName); @@ -252,16 +256,16 @@ private function recursiveSerialize( continue; } $relation = $classMetadata->getRelation( - $attribute->getSerializedKey() + $attribute->getSerializedKey(), ); $data = $entity->{$method}(); if ( - null === $data && - $relation && - $relation->isManyToOne() && - $level > 0 + null === $data + && $relation + && $relation->isManyToOne() + && $level > 0 ) { /* We only serialize the root many-to-one relations to prevent, hopefully, @@ -270,23 +274,23 @@ private function recursiveSerialize( CartItem entities explicitly bound to a null Cart instead of the created/updated Cart. */ continue; - } elseif ($data instanceof DateTimeInterface) { + } elseif ($data instanceof \DateTimeInterface) { $data = $data->format('c'); } elseif (is_object($data) && $data instanceof PhoneNumber) { $phoneNumberUtil = PhoneNumberUtil::getInstance(); $data = $phoneNumberUtil->format( $data, - PhoneNumberFormat::INTERNATIONAL + PhoneNumberFormat::INTERNATIONAL, ); } elseif ( - is_object($data) && - $relation && - $this->mapping->hasClassMetadata( - $relation->getTargetEntity() + is_object($data) + && $relation + && $this->mapping->hasClassMetadata( + $relation->getTargetEntity(), ) ) { $relationClassMetadata = $this->mapping->getClassMetadata( - $relation->getTargetEntity() + $relation->getTargetEntity(), ); if (!$relationClassMetadata->hasIdentifierAttribute()) { @@ -294,7 +298,7 @@ private function recursiveSerialize( $data, $relation->getTargetEntity(), $level + 1, - $context + $context, ); } else { $idAttribute = $relationClassMetadata->getIdentifierAttribute(); @@ -302,8 +306,8 @@ private function recursiveSerialize( 'get' . ucfirst($idAttribute->getAttributeName()); if ( - method_exists($data, $idGetter) && - $data->{$idGetter}() + method_exists($data, $idGetter) + && $data->{$idGetter}() ) { $data = $data->{$idGetter}(); } elseif ($relation->isManyToOne()) { @@ -311,7 +315,7 @@ private function recursiveSerialize( continue; } else { throw new SdkException( - 'Case not allowed for now' + 'Case not allowed for now', ); } } @@ -319,27 +323,27 @@ private function recursiveSerialize( } elseif (is_array($data)) { $newData = []; foreach ($data as $key => $item) { - if ($item instanceof DateTimeInterface) { + if ($item instanceof \DateTimeInterface) { $newData[$key] = $item->format('c'); } elseif ( - is_object($item) && - $relation && - $this->mapping->hasClassMetadata( - $relation->getTargetEntity() + is_object($item) + && $relation + && $this->mapping->hasClassMetadata( + $relation->getTargetEntity(), ) ) { $serializeRelation = - !empty($context['serializeRelations']) && - in_array( + !empty($context['serializeRelations']) + && in_array( $relation->getSerializedKey(), - $context['serializeRelations'] + $context['serializeRelations'], ); $newData[$key] = $this->recursiveSerialize( $item, $relation->getTargetEntity(), $level + 1, - ['serializeRelation' => $serializeRelation] + ['serializeRelation' => $serializeRelation], ); } else { $newData[$key] = $item; @@ -366,20 +370,20 @@ private function getClassMetadataFromId(string $id): ?ClassMetadata private function getClassMetadata(object $entity): ClassMetadata { - return $this->mapping->getClassMetadata(get_class($entity)); + return $this->mapping->getClassMetadata($entity::class); } private function throwIfAttributeIsNotWritable( object $instance, - string $attribute + string $attribute, ): void { if (!$this->propertyAccessor->isWritable($instance, $attribute)) { throw new MissingSetterException( sprintf( 'Property %s is not writable for class %s. Please make it writable. You can check the property-access documentation here : https://symfony.com/doc/current/components/property_access.html#writing-to-objects', $attribute, - get_class($instance) - ) + $instance::class, + ), ); } } @@ -387,25 +391,25 @@ private function throwIfAttributeIsNotWritable( private function setDateTimeValue( object $instance, string $attributeName, - string $value + string $value, ): void { try { $this->propertyAccessor->setValue( $instance, $attributeName, - new DateTime($value) + new \DateTime($value), ); } catch (InvalidArgumentException $e) { if ( false === mb_strpos( $e->getMessage(), - 'Expected argument of type "DateTimeImmutable", "DateTime" given' // symfony < 4.4 message - ) && - false === + 'Expected argument of type "DateTimeImmutable", "DateTime" given', // symfony < 4.4 message + ) + && false === mb_strpos( $e->getMessage(), - 'Expected argument of type "DateTimeImmutable", "instance of DateTime" given' // symfony >= 4.4 message + 'Expected argument of type "DateTimeImmutable", "instance of DateTime" given', // symfony >= 4.4 message ) ) { // not an issue with DateTimeImmutable, then rethrow exception @@ -416,7 +420,7 @@ private function setDateTimeValue( $this->propertyAccessor->setValue( $instance, $attributeName, - new DateTimeImmutable($value) + new \DateTimeImmutable($value), ); } } diff --git a/src/RestClient.php b/src/RestClient.php index 8c29536..8b2dd15 100644 --- a/src/RestClient.php +++ b/src/RestClient.php @@ -14,9 +14,15 @@ use Symfony\Component\HttpFoundation\Request; /** - * Class RestClient - * - * @author Julien Deniau + * @phpstan-type RequestHistory array{ + * 'method': string, + * 'url': string, + * 'parameters': array, + * 'response': ?ResponseInterface, + * 'responseBody': ?mixed, + * 'queryTime': float, + * 'backtrace': array, + * } */ class RestClient { @@ -36,7 +42,9 @@ class RestClient private $logHistory; /** - * @var array + * @var array> + * + * @phpstan-var array */ private $requestHistory; @@ -47,7 +55,7 @@ class RestClient public function __construct( ClientInterface $httpClient, - ?string $baseUrl = null + string $baseUrl = null, ) { $this->httpClient = $httpClient; $this->baseUrl = @@ -77,6 +85,11 @@ public function setLogHistory(bool $logHistory): self return $this; } + /** + * @return array> + * + * @phpstan-return array + */ public function getRequestHistory(): array { return $this->requestHistory; @@ -85,7 +98,9 @@ public function getRequestHistory(): array /** * get a path * - * @return array|ResponseInterface|null + * @param array $parameters + * + * @return array|ResponseInterface|null * * @throws RestException */ @@ -104,7 +119,7 @@ public function get(string $path, array $parameters = []) $path, [], 7, - $e + $e, ); } catch (TransferException $e) { throw new RestException( @@ -112,7 +127,7 @@ public function get(string $path, array $parameters = []) $path, [], 1, - $e + $e, ); } } @@ -132,13 +147,16 @@ public function delete(string $path): void $path, [], 2, - $e + $e, ); } } /** - * @return array|ResponseInterface + * @param array $data + * @param array $parameters + * + * @return array|ResponseInterface * * @throws RestClientException * @throws RestException @@ -150,7 +168,7 @@ public function post(string $path, array $data, array $parameters = []) return $this->executeRequest( 'POST', $this->baseUrl . $path, - $parameters + $parameters, ); } catch (ClientException $e) { throw new RestClientException( @@ -158,7 +176,7 @@ public function post(string $path, array $data, array $parameters = []) $path, [], 3, - $e + $e, ); } catch (TransferException $e) { throw new RestException( @@ -166,13 +184,16 @@ public function post(string $path, array $data, array $parameters = []) $path, [], 4, - $e + $e, ); } } /** - * @return array|ResponseInterface + * @param array $data + * @param array $parameters + * + * @return array|ResponseInterface * * @throws RestClientException * @throws RestException @@ -185,7 +206,7 @@ public function put(string $path, array $data, array $parameters = []) return $this->executeRequest( 'PUT', $this->baseUrl . $path, - $parameters + $parameters, ); } catch (ClientException $e) { throw new RestClientException( @@ -193,7 +214,7 @@ public function put(string $path, array $data, array $parameters = []) $path, [], 5, - $e + $e, ); } catch (TransferException $e) { throw new RestException( @@ -201,13 +222,17 @@ public function put(string $path, array $data, array $parameters = []) $path, [], 6, - $e + $e, ); } } /** * Merge default parameters. + * + * @param array $parameters + * + * @return array */ protected function mergeDefaultParameters(array $parameters): array { @@ -218,15 +243,15 @@ protected function mergeDefaultParameters(array $parameters): array $defaultParameters['headers'] = ['Referer' => $request->getUri()]; } - /** @var array|null $out */ + /** @var array|null $out */ $out = array_replace_recursive($defaultParameters, $parameters); if (null === $out) { throw new \RuntimeException( sprintf( 'Error while calling array_replace_recursive in %s. This should not happen.', - __METHOD__ - ) + __METHOD__, + ), ); } @@ -250,14 +275,16 @@ protected function getCurrentRequest(): ?Request /** * Executes request. * - * @return ResponseInterface|array + * @param array $parameters + * + * @return ResponseInterface|array * * @throws TransferException */ private function executeRequest( string $method, string $url, - array $parameters = [] + array $parameters = [], ) { $parameters = $this->mergeDefaultParameters($parameters); @@ -273,7 +300,7 @@ private function executeRequest( $method, $url, $parameters, - $response + $response, ); } catch (RequestException $e) { $this->logRequest( @@ -281,7 +308,7 @@ private function executeRequest( $method, $url, $parameters, - $e->getResponse() + $e->getResponse(), ); throw $e; } catch (TransferException $e) { @@ -295,7 +322,7 @@ private function executeRequest( $requestIsJson = false; $responseContentType = - $headers['Content-Type'] ?? $headers['content-type'] ?? null; + $headers['Content-Type'] ?? ($headers['content-type'] ?? null); if ($responseContentType) { foreach ($jsonContentTypeList as $contentType) { if ( @@ -308,7 +335,7 @@ private function executeRequest( } if ($requestIsJson) { - /** @var array $decodedJson */ + /** @var array $decodedJson */ $decodedJson = json_decode((string) $response->getBody(), true); return $decodedJson; @@ -317,12 +344,15 @@ private function executeRequest( } } + /** + * @param array $parameters + */ private function logRequest( ?float $startTime, string $method, string $url, array $parameters, - ?ResponseInterface $response = null + ResponseInterface $response = null, ): void { if ($this->isHistoryLogged()) { $queryTime = microtime(true) - $startTime; @@ -336,7 +366,7 @@ private function logRequest( ? json_decode((string) $response->getBody(), true) : null, 'queryTime' => $queryTime, - 'backtrace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), + 'backtrace' => debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS), ]; } } diff --git a/src/SdkClient.php b/src/SdkClient.php index ee521f6..b276db7 100644 --- a/src/SdkClient.php +++ b/src/SdkClient.php @@ -13,9 +13,6 @@ use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessor; -/** - * Sdk Client - */ class SdkClient { /** @@ -53,7 +50,7 @@ class SdkClient private $modelHydrator; /** - * @var array + * @var array> */ private $repositoryList = []; @@ -79,8 +76,8 @@ class SdkClient public function __construct( RestClient $restClient, Mapping $mapping, - ?UnitOfWork $unitOfWork = null, - ?Serializer $serializer = null + UnitOfWork $unitOfWork = null, + Serializer $serializer = null, ) { $this->restClient = $restClient; $this->mapping = $mapping; @@ -99,7 +96,7 @@ public function __construct( public function setCacheItemPool( CacheItemPoolInterface $cacheItemPool, - string $cachePrefix = '' + string $cachePrefix = '', ): self { $this->cacheItemPool = $cacheItemPool; $this->cachePrefix = $cachePrefix; @@ -117,6 +114,9 @@ public function getCachePrefix(): string return $this->cachePrefix; } + /** + * @return EntityRepository + */ public function getRepository(string $modelName): EntityRepository { // get repository by key @@ -131,11 +131,18 @@ public function getRepository(string $modelName): EntityRepository if (!isset($this->repositoryList[$modelName])) { $repositoryName = $metadata->getRepositoryName(); + if (!is_a($repositoryName, EntityRepository::class, true)) { + throw new \RuntimeException( + "Repository class {$repositoryName} must extend " . + EntityRepository::class, + ); + } + $this->repositoryList[$modelName] = new $repositoryName( $this, $this->restClient, $this->unitOfWork, - $modelName + $modelName, ); } @@ -162,13 +169,18 @@ public function getModelHydrator(): ModelHydrator return $this->modelHydrator; } + /** + * @return GhostObjectInterface + */ public function createProxy(string $id): GhostObjectInterface { $key = $this->mapping->getKeyFromId($id); $classMetadata = $this->mapping->getClassMetadataByKey($key); if (null === $classMetadata) { - throw new \RuntimeException("Unable to get classMetadata for key {$key}. This should not happen."); + throw new \RuntimeException( + "Unable to get classMetadata for key {$key}. This should not happen.", + ); } /** @var class-string $modelName */ @@ -192,19 +204,19 @@ public function createProxy(string $id): GhostObjectInterface string $method, array $parameters, \Closure|null &$initializer, - array $properties + array $properties, ) use ($sdk, $classMetadata, $id, $proxyModelName) { $isAllowedMethod = - 'jsonSerialize' === $method || - '__set' === $method || - ('__isset' === $method && 'id' === $parameters['name']); + 'jsonSerialize' === $method + || '__set' === $method + || ('__isset' === $method && 'id' === $parameters['name']); if (!$isAllowedMethod) { $initializer = null; // disable initialization // load data and modify the object here if ($id) { $repository = $sdk->getRepository( - $classMetadata->getModelName() + $classMetadata->getModelName(), ); $model = $repository->find($id); @@ -214,7 +226,7 @@ public function createProxy(string $id): GhostObjectInterface foreach ($attributeList as $attribute) { $value = $this->getPropertyAccessor()->getValue( $model, - $attribute->getAttributeName() + $attribute->getAttributeName(), ); $properties[ "\0" . @@ -238,7 +250,7 @@ public function createProxy(string $id): GhostObjectInterface // set the id of the object $idReflexion = new \ReflectionProperty( $modelName, - $classMetadata->getIdentifierAttribute()->getAttributeName() + $classMetadata->getIdentifierAttribute()->getAttributeName(), ); $idReflexion->setAccessible(true); $idReflexion->setValue($instance, $id); diff --git a/src/SdkClientRegistry.php b/src/SdkClientRegistry.php index e1b30a8..883547a 100644 --- a/src/SdkClientRegistry.php +++ b/src/SdkClientRegistry.php @@ -8,8 +8,8 @@ class SdkClientRegistry { - /** var array $sdkClientList */ - private $sdkClientList; + /** @var array */ + private array $sdkClientList; /** * @param array $sdkClientList @@ -25,7 +25,7 @@ public function getSdkClient(string $name): SdkClient if (!$client) { throw new SdkClientNotFoundException( - 'Sdk client not found for name ' . $name + 'Sdk client not found for name ' . $name, ); } @@ -49,7 +49,7 @@ public function getSdkClientForClass(string $entityClassname): SdkClient } throw new SdkClientNotFoundException( - 'Sdk client not found for entity class ' . $entityClassname + 'Sdk client not found for entity class ' . $entityClassname, ); } } diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index c446b2f..297bf8e 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -42,12 +42,12 @@ public function __construct(Mapping $mapping) public function getDirtyData( array $newSerializedModel, array $oldSerializedModel, - ClassMetadata $classMetadata + ClassMetadata $classMetadata, ): array { return $this->getDirtyFields( $newSerializedModel, $oldSerializedModel, - $classMetadata + $classMetadata, ); } @@ -80,7 +80,7 @@ public function clear(string $id): self private function getDirtyFields( array $newSerializedModel, array $oldSerializedModel, - ClassMetadata $classMetadata + ClassMetadata $classMetadata, ): array { $dirtyFields = []; @@ -97,9 +97,9 @@ private function getDirtyFields( if (!$currentRelation) { if ( - (is_array($value) && - !ArrayHelper::arraySame($value, $oldValue ?: [])) || - $value !== $oldValue + (is_array($value) + && !ArrayHelper::arraySame($value, $oldValue ?: [])) + || $value !== $oldValue ) { $dirtyFields[$key] = $value; } @@ -107,7 +107,7 @@ private function getDirtyFields( } $currentClassMetadata = $this->mapping->getClassMetadata( - $currentRelation->getTargetEntity() + $currentRelation->getTargetEntity(), ); $idSerializedKey = $currentClassMetadata->getIdSerializeKey(); @@ -120,7 +120,7 @@ private function getDirtyFields( $recursiveDiff = $this->getDirtyFields( $value, $oldValue, - $currentClassMetadata + $currentClassMetadata, ); if (!empty($recursiveDiff)) { @@ -141,7 +141,7 @@ private function getDirtyFields( $dirtyFields[$key] = $this->addIdentifiers( $value, [], - $idSerializedKey + $idSerializedKey, ); } @@ -150,20 +150,20 @@ private function getDirtyFields( $oldRelationValue = $this->findOldRelation( $relationValue, $oldValue, - $currentClassMetadata + $currentClassMetadata, ); if ($relationValue !== $oldRelationValue) { if ( - is_string($relationValue) || - is_string($oldRelationValue) + is_string($relationValue) + || is_string($oldRelationValue) ) { $dirtyFields[$key][$relationKey] = $relationValue; } else { $recursiveDiff = $this->getDirtyFields( $relationValue, $oldRelationValue, - $currentClassMetadata + $currentClassMetadata, ); if (!empty($recursiveDiff)) { @@ -171,7 +171,7 @@ private function getDirtyFields( $entityId = self::getEntityId( $relationValue, - $idSerializedKey + $idSerializedKey, ); if (null !== $entityId) { $recursiveDiff[ @@ -194,13 +194,12 @@ private function getDirtyFields( /** * add defined identifiers to given model * - * @param array $newSerializedModel * @param ?string $idSerializedKey */ private function addIdentifiers( array $newSerializedModel, array $dirtyFields, - $idSerializedKey = null + $idSerializedKey = null, ): array { foreach ($newSerializedModel as $key => $value) { if ($idSerializedKey && isset($value[$idSerializedKey])) { @@ -221,7 +220,7 @@ private function addIdentifiers( private function findOldRelation( $relationValue, array $oldValue, - ClassMetadata $classMetadata + ClassMetadata $classMetadata, ) { $idSerializedKey = $classMetadata->getIdSerializeKey(); @@ -230,7 +229,7 @@ private function findOldRelation( foreach ($oldValue as $oldRelationValue) { $oldRelationValueId = self::getEntityId( $oldRelationValue, - $idSerializedKey + $idSerializedKey, ); if ($relationValueId === $oldRelationValueId) { @@ -250,7 +249,7 @@ private function findOldRelation( */ private static function getEntityId( $stringOrEntity, - string $idSerializedKey + string $idSerializedKey, ) { if (!is_array($stringOrEntity)) { return $stringOrEntity; diff --git a/yarn.lock b/yarn.lock index 31c4d35..377a992 100644 --- a/yarn.lock +++ b/yarn.lock @@ -39,13 +39,12 @@ "@nodelib/fs.scandir" "2.1.2" fastq "^1.6.0" -"@prettier/plugin-php@^0.19.7": - version "0.19.7" - resolved "https://registry.yarnpkg.com/@prettier/plugin-php/-/plugin-php-0.19.7.tgz#c11b710f319f83c276b6b3f8ab3f70f612302785" - integrity sha512-QOzBs05nwuR92uak7xBHf7RCZCFXml+6Sk3cjTp2ahQlilBtupqlNjitlTXsOfPIAYwlFgLP1oSfyapS6DN00w== +"@prettier/plugin-php@^0.22.2": + version "0.22.2" + resolved "https://registry.yarnpkg.com/@prettier/plugin-php/-/plugin-php-0.22.2.tgz#9a575f7f53ce102d72bb17fa0bd6580f1618bbd9" + integrity sha512-md0+7tNbsP0oy+wIP3KZZc6fzx1k1jtWaMjOy/gM8yU9f2BDYEi+iHOc/UNPihYvPI28zFTbjvlhH4QXQjQwNg== dependencies: - linguist-languages "^7.21.0" - mem "^8.0.0" + linguist-languages "^7.27.0" php-parser "^3.1.5" "@samverschueren/stream-to-observable@^0.3.0": @@ -628,7 +627,7 @@ lines-and-columns@^1.1.6: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA= -linguist-languages@^7.21.0: +linguist-languages@^7.27.0: version "7.27.0" resolved "https://registry.yarnpkg.com/linguist-languages/-/linguist-languages-7.27.0.tgz#bf42d6c62bd04655c8f60ed2f77a84eaeae4023a" integrity sha512-Wzx/22c5Jsv2ag+uKy+ITanGA5hzvBZngrNGDXLTC7ZjGM6FLCYGgomauTkxNJeP9of353OM0pWqngYA180xgw== @@ -725,21 +724,6 @@ log-update@^2.3.0: cli-cursor "^2.0.0" wrap-ansi "^3.0.1" -map-age-cleaner@^0.1.3: - version "0.1.3" - resolved "https://registry.yarnpkg.com/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz#7d583a7306434c055fe474b0f45078e6e1b4b92a" - integrity sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w== - dependencies: - p-defer "^1.0.0" - -mem@^8.0.0: - version "8.1.1" - resolved "https://registry.yarnpkg.com/mem/-/mem-8.1.1.tgz#cf118b357c65ab7b7e0817bdf00c8062297c0122" - integrity sha512-qFCFUDs7U3b8mBDPyz5EToEKoAkgCzqquIgi9nkkR9bixxOVOre+09lbuH7+9Kn2NFpm56M3GUWVbU2hQgdACA== - dependencies: - map-age-cleaner "^0.1.3" - mimic-fn "^3.1.0" - merge-stream@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60" @@ -768,11 +752,6 @@ mimic-fn@^2.1.0: resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b" integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg== -mimic-fn@^3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-3.1.0.tgz#65755145bbf3e36954b949c16450427451d5ca74" - integrity sha512-Ysbi9uYW9hFyfrThdDEQuykN4Ey6BuwPD2kpI5ES/nFTDn/98yxYNLZJcgUAKPT/mcrLLKaGzJR9YVxJrIdASQ== - minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" @@ -851,11 +830,6 @@ opencollective-postinstall@^2.0.2: resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz#5657f1bede69b6e33a45939b061eb53d3c6c3a89" integrity sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw== -p-defer@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c" - integrity sha1-n26xgvbJqozXQwBKfU+WsZaw+ww= - p-finally@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" @@ -967,10 +941,10 @@ please-upgrade-node@^3.1.1, please-upgrade-node@^3.2.0: dependencies: semver-compare "^1.0.0" -prettier@^1.18.2: - version "1.18.2" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" - integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== +prettier@^3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105" + integrity sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew== pump@^3.0.0: version "3.0.0"