You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44Lines changed: 44 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ This extension provides following features:
17
17
* Provides correct return type for `Doctrine\ORM\EntityManager::find`, `getReference` and `getPartialReference` when `Foo::class` entity class name is provided as the first argument
18
18
* Adds missing `matching` method on `Doctrine\Common\Collections\Collection`. This can be turned off by setting `parameters.doctrine.allCollectionsSelectable` to `false`.
19
19
* Also supports Doctrine ODM.
20
+
* Analysis of discrepancies between entity column types and property field types.
20
21
21
22
## Installation
22
23
@@ -73,3 +74,46 @@ $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
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`:
0 commit comments