Skip to content

Commit c35261f

Browse files
AlexandruGGondrejmirtes
authored andcommitted
Add Carbon descriptors via ReflectionDescriptor
1 parent 24f7283 commit c35261f

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"doctrine/mongodb-odm": "^1.3 || ^2.1",
2525
"doctrine/orm": "^2.9.1",
2626
"doctrine/persistence": "^1.1 || ^2.0",
27+
"nesbot/carbon": "^2.49",
2728
"php-parallel-lint/php-parallel-lint": "^1.2",
2829
"phpstan/phpstan-phpunit": "^0.12.16",
2930
"phpstan/phpstan-strict-rules": "^0.12.5",

extension.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ services:
295295
tags: [phpstan.doctrine.typeDescriptor]
296296

297297
# 3rd party Type descriptors
298+
-
299+
factory: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('Carbon\Doctrine\CarbonImmutableType')
300+
tags: [phpstan.doctrine.typeDescriptor]
301+
-
302+
factory: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('Carbon\Doctrine\CarbonType')
303+
tags: [phpstan.doctrine.typeDescriptor]
298304
-
299305
class: PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor
300306
tags: [phpstan.doctrine.typeDescriptor]

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPStan\Rules\Doctrine\ORM;
44

5+
use Carbon\Doctrine\CarbonImmutableType;
6+
use Carbon\Doctrine\CarbonType;
57
use Doctrine\DBAL\Types\Type;
68
use Iterator;
79
use PHPStan\Rules\Rule;
@@ -38,22 +40,30 @@ protected function getRule(): Rule
3840
if (!Type::hasType(UuidType::NAME)) {
3941
Type::addType(UuidType::NAME, UuidType::class);
4042
}
43+
if (!Type::hasType('carbon')) {
44+
Type::addType('carbon', \Carbon\Doctrine\CarbonType::class);
45+
}
46+
if (!Type::hasType('carbon_immutable')) {
47+
Type::addType('carbon_immutable', \Carbon\Doctrine\CarbonImmutableType::class);
48+
}
4149

4250
return new EntityColumnRule(
4351
new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null),
4452
new DescriptorRegistry([
53+
new ArrayType(),
4554
new BigIntType(),
46-
new StringType(),
47-
new DateTimeType(),
48-
new DateTimeImmutableType(),
4955
new BinaryType(),
56+
new DateTimeImmutableType(),
57+
new DateTimeType(),
58+
new DateType(),
59+
new DecimalType(),
5060
new IntegerType(),
61+
new StringType(),
62+
new UuidTypeDescriptor(UuidType::class),
63+
new ReflectionDescriptor(CarbonImmutableType::class, $this->createBroker()),
64+
new ReflectionDescriptor(CarbonType::class, $this->createBroker()),
5165
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
5266
new ReflectionDescriptor(CustomNumericType::class, $this->createBroker()),
53-
new DateType(),
54-
new UuidTypeDescriptor(UuidType::class),
55-
new ArrayType(),
56-
new DecimalType(),
5767
]),
5868
true
5969
);
@@ -106,6 +116,14 @@ public function testRule(): void
106116
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$numericString type mapping mismatch: database can contain string but property expects string&numeric.',
107117
126,
108118
],
119+
[
120+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidCarbon type mapping mismatch: database can contain Carbon\Carbon but property expects Carbon\CarbonImmutable.',
121+
132,
122+
],
123+
[
124+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidCarbonImmutable type mapping mismatch: database can contain Carbon\CarbonImmutable but property expects Carbon\Carbon.',
125+
138,
126+
],
109127
]);
110128
}
111129

tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,27 @@ class MyBrokenEntity extends MyBrokenSuperclass
125125
*/
126126
private $numericString;
127127

128+
/**
129+
* @ORM\Column(type="carbon")
130+
* @var \Carbon\CarbonImmutable
131+
*/
132+
private $invalidCarbon;
133+
134+
/**
135+
* @ORM\Column(type="carbon_immutable")
136+
* @var \Carbon\Carbon
137+
*/
138+
private $invalidCarbonImmutable;
139+
140+
/**
141+
* @ORM\Column(type="carbon")
142+
* @var \Carbon\Carbon
143+
*/
144+
private $validCarbon;
145+
146+
/**
147+
* @ORM\Column(type="carbon_immutable")
148+
* @var \Carbon\CarbonImmutable
149+
*/
150+
private $validCarbonImmutable;
128151
}

0 commit comments

Comments
 (0)