Skip to content

Commit 58c3c8a

Browse files
lookymanondrejmirtes
authored andcommitted
Documentation for type descriptors
1 parent 4540cdf commit 58c3c8a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This extension provides following features:
1717
* Provides correct return type for `Doctrine\ORM\EntityManager::find`, `getReference` and `getPartialReference` when `Foo::class` entity class name is provided as the first argument
1818
* Adds missing `matching` method on `Doctrine\Common\Collections\Collection`. This can be turned off by setting `parameters.doctrine.allCollectionsSelectable` to `false`.
1919
* Also supports Doctrine ODM.
20+
* Analysis of discrepancies between entity column types and property field types.
2021

2122
## Installation
2223

@@ -73,3 +74,46 @@ $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
7374
$kernel->boot();
7475
return $kernel->getContainer()->get('doctrine')->getManager();
7576
```
77+
78+
## Custom types
79+
80+
If your application uses custom Doctrine types, you can write your own type descriptors to analyse them properly.
81+
Type descriptors implement the interface `PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor` which looks like this:
82+
83+
```php
84+
public function getType(): string;
85+
86+
public function getWritableToPropertyType(): Type;
87+
88+
public function getWritableToDatabaseType(): Type;
89+
```
90+
91+
* The `getType()` method simply returns the name of the custom type.
92+
* 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.
93+
* 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.
94+
95+
Generally, at least for most of Doctrine's native types, these last two methods will return the same type, but it is not always the case. One example would be the `datetime` type, which allows you to set any `\DateTimeInterface` into to property field, but will always contain the `\DateTime` type when loaded from the database.
96+
97+
### Nullable types
98+
99+
Type descriptors don't have to deal with nullable types, as these are transparently added/removed from the descriptor's types as needed. Therefore you don't have to return the union type of your custom type and `NullType` from the descriptor's methods, even if your custom type allows `null`.
100+
101+
### ReflectionDescriptor
102+
103+
If your custom type's `convertToPHPValue()` and `convertToDatabaseValue()` methods have proper typehints, you don't have to write your own descriptor for it. The `PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor` can analyse the typehints and do the rest for you.
104+
105+
### Registering type descriptors
106+
107+
When you write a custom type descriptor, you have to let PHPStan know about it. Add something like this into your `phpstan.neon`:
108+
109+
```neon
110+
services:
111+
-
112+
class: MyCustomTypeDescriptor
113+
tags: [phpstan.doctrine.typeDescriptor]
114+
115+
# in case you are using the ReflectionDescriptor
116+
-
117+
class: PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor('my_custom_type_name')
118+
tags: [phpstan.doctrine.typeDescriptor]
119+
```

0 commit comments

Comments
 (0)