Skip to content

Commit 59c6543

Browse files
committed
Refactored descriptors so they work with Doctrine\DBAL\Types\Type::overrideType()
1 parent cda8b82 commit 59c6543

32 files changed

+62
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getWritableToPropertyType(): Type;
8888
public function getWritableToDatabaseType(): Type;
8989
```
9090

91-
* The `getType()` method simply returns the name of the custom type.
91+
* The `getType()` method simply returns the class name of the custom type.
9292
* The `getWritableToPropertyType()` method returns the PHPStan type that the custom type will write into the entity's property field. Basically it is the return type of the custom type's `convertToPHPValue()` method.
9393
* The `getWritableToDatabaseType()` method returns the PHPStan type that can be written from the entity's property field into the custom type. Again, basically it's the allowed type for the custom type's `convertToDatabaseValue()`'s first argument.
9494

@@ -114,6 +114,6 @@ services:
114114
115115
# in case you are using the ReflectionDescriptor
116116
-
117-
class: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('my_custom_type_name')
117+
class: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('MyApp\MyCustomTypeName')
118118
tags: [phpstan.doctrine.typeDescriptor]
119119
```

src/Type/Doctrine/DescriptorRegistry.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace PHPStan\Type\Doctrine;
44

5+
use Doctrine\DBAL\Types\Type;
56
use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor;
67

78
class DescriptorRegistry
89
{
910

10-
/** @var array<string, \PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor> */
11+
/** @var array<class-string<\Doctrine\DBAL\Types\Type>, \PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor> */
1112
private $descriptors = [];
1213

1314
/**
@@ -22,10 +23,17 @@ public function __construct(array $descriptors)
2223

2324
public function get(string $type): DoctrineTypeDescriptor
2425
{
25-
if (!isset($this->descriptors[$type])) {
26+
$typesMap = Type::getTypesMap();
27+
if (!isset($typesMap[$type])) {
2628
throw new \PHPStan\Type\Doctrine\DescriptorNotRegisteredException();
2729
}
28-
return $this->descriptors[$type];
30+
31+
/** @var class-string<\Doctrine\DBAL\Types\Type> $typeClass */
32+
$typeClass = $typesMap[$type];
33+
if (!isset($this->descriptors[$typeClass])) {
34+
throw new \PHPStan\Type\Doctrine\DescriptorNotRegisteredException();
35+
}
36+
return $this->descriptors[$typeClass];
2937
}
3038

3139
}

src/Type/Doctrine/Descriptors/ArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ArrayType implements DoctrineTypeDescriptor
1010

1111
public function getType(): string
1212
{
13-
return 'array';
13+
return \Doctrine\DBAL\Types\ArrayType::class;
1414
}
1515

1616
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/BigIntType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class BigIntType implements DoctrineTypeDescriptor
1010

1111
public function getType(): string
1212
{
13-
return 'bigint';
13+
return \Doctrine\DBAL\Types\BigIntType::class;
1414
}
1515

1616
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/BinaryType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BinaryType implements DoctrineTypeDescriptor
1111

1212
public function getType(): string
1313
{
14-
return 'binary';
14+
return \Doctrine\DBAL\Types\BinaryType::class;
1515
}
1616

1717
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/BlobType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BlobType implements DoctrineTypeDescriptor
1111

1212
public function getType(): string
1313
{
14-
return 'blob';
14+
return \Doctrine\DBAL\Types\BlobType::class;
1515
}
1616

1717
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/BooleanType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BooleanType implements DoctrineTypeDescriptor
99

1010
public function getType(): string
1111
{
12-
return 'boolean';
12+
return \Doctrine\DBAL\Types\BooleanType::class;
1313
}
1414

1515
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/DateImmutableType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DateImmutableType implements DoctrineTypeDescriptor
1111

1212
public function getType(): string
1313
{
14-
return 'date_immutable';
14+
return \Doctrine\DBAL\Types\DateTimeImmutableType::class;
1515
}
1616

1717
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/DateIntervalType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DateIntervalType implements DoctrineTypeDescriptor
1111

1212
public function getType(): string
1313
{
14-
return 'dateinterval';
14+
return \Doctrine\DBAL\Types\DateIntervalType::class;
1515
}
1616

1717
public function getWritableToPropertyType(): Type

src/Type/Doctrine/Descriptors/DateTimeImmutableType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DateTimeImmutableType implements DoctrineTypeDescriptor
1111

1212
public function getType(): string
1313
{
14-
return 'datetime_immutable';
14+
return \Doctrine\DBAL\Types\DateTimeImmutableType::class;
1515
}
1616

1717
public function getWritableToPropertyType(): Type

0 commit comments

Comments
 (0)