From 1e3c1ea654134ee479109bb6ffed5c286cae73c9 Mon Sep 17 00:00:00 2001 From: AlexandruGG Date: Wed, 9 Jun 2021 18:53:42 +0100 Subject: [PATCH] Add Carbon descriptors via ReflectionDescriptor --- composer.json | 1 + extension.neon | 6 ++++ .../Doctrine/ORM/EntityColumnRuleTest.php | 32 +++++++++++++++---- .../Doctrine/ORM/data/MyBrokenEntity.php | 23 +++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 1f761951..e819631b 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "doctrine/mongodb-odm": "^1.3 || ^2.1", "doctrine/orm": "^2.9.1", "doctrine/persistence": "^1.1 || ^2.0", + "nesbot/carbon": "^2.49", "php-parallel-lint/php-parallel-lint": "^1.2", "phpstan/phpstan-phpunit": "^0.12.16", "phpstan/phpstan-strict-rules": "^0.12.5", diff --git a/extension.neon b/extension.neon index 14d26486..6102bec4 100644 --- a/extension.neon +++ b/extension.neon @@ -295,6 +295,12 @@ services: tags: [phpstan.doctrine.typeDescriptor] # 3rd party Type descriptors + - + factory: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('Carbon\Doctrine\CarbonImmutableType') + tags: [phpstan.doctrine.typeDescriptor] + - + factory: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('Carbon\Doctrine\CarbonType') + tags: [phpstan.doctrine.typeDescriptor] - class: PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor tags: [phpstan.doctrine.typeDescriptor] diff --git a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php index fb8aaaa0..472ae21f 100644 --- a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php +++ b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php @@ -2,6 +2,8 @@ namespace PHPStan\Rules\Doctrine\ORM; +use Carbon\Doctrine\CarbonImmutableType; +use Carbon\Doctrine\CarbonType; use Doctrine\DBAL\Types\Type; use Iterator; use PHPStan\Rules\Rule; @@ -38,22 +40,30 @@ protected function getRule(): Rule if (!Type::hasType(UuidType::NAME)) { Type::addType(UuidType::NAME, UuidType::class); } + if (!Type::hasType('carbon')) { + Type::addType('carbon', \Carbon\Doctrine\CarbonType::class); + } + if (!Type::hasType('carbon_immutable')) { + Type::addType('carbon_immutable', \Carbon\Doctrine\CarbonImmutableType::class); + } return new EntityColumnRule( new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null), new DescriptorRegistry([ + new ArrayType(), new BigIntType(), - new StringType(), - new DateTimeType(), - new DateTimeImmutableType(), new BinaryType(), + new DateTimeImmutableType(), + new DateTimeType(), + new DateType(), + new DecimalType(), new IntegerType(), + new StringType(), + new UuidTypeDescriptor(UuidType::class), + new ReflectionDescriptor(CarbonImmutableType::class, $this->createBroker()), + new ReflectionDescriptor(CarbonType::class, $this->createBroker()), new ReflectionDescriptor(CustomType::class, $this->createBroker()), new ReflectionDescriptor(CustomNumericType::class, $this->createBroker()), - new DateType(), - new UuidTypeDescriptor(UuidType::class), - new ArrayType(), - new DecimalType(), ]), true ); @@ -106,6 +116,14 @@ public function testRule(): void 'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$numericString type mapping mismatch: database can contain string but property expects string&numeric.', 126, ], + [ + 'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidCarbon type mapping mismatch: database can contain Carbon\Carbon but property expects Carbon\CarbonImmutable.', + 132, + ], + [ + 'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidCarbonImmutable type mapping mismatch: database can contain Carbon\CarbonImmutable but property expects Carbon\Carbon.', + 138, + ], ]); } diff --git a/tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php b/tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php index adc2c2e4..13fe5b6c 100644 --- a/tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php +++ b/tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php @@ -125,4 +125,27 @@ class MyBrokenEntity extends MyBrokenSuperclass */ private $numericString; + /** + * @ORM\Column(type="carbon") + * @var \Carbon\CarbonImmutable + */ + private $invalidCarbon; + + /** + * @ORM\Column(type="carbon_immutable") + * @var \Carbon\Carbon + */ + private $invalidCarbonImmutable; + + /** + * @ORM\Column(type="carbon") + * @var \Carbon\Carbon + */ + private $validCarbon; + + /** + * @ORM\Column(type="carbon_immutable") + * @var \Carbon\CarbonImmutable + */ + private $validCarbonImmutable; }