Invalid nullable property check for simple_array mapping type
Doctrine DBAL always converts the database value to a PHP array, even when the field contains null. SimpleArrayType mapper converts the null value from DB to an empty PHP array. However, when I put a not-nullable type hint for the simple_array mapping from the nullable DB field:
/**
* @var list<string>
* @ORM\Column(type="simple_array", nullable=true)
*/
private $simpleArrayFromNullableField;
PHPStan check produces an error:
Property PHPStan\Rules\Doctrine\ORM\MyEntity::$simpleArrayFromNullableField type mapping mismatch: database can contain list<string>|null but property expects list<string>.
I see two possible ways to address the issue:
-
Always require a non-nullable property when using simple_array mapping. This approach aligns the property type with the behaviour of the SimpleArrayType::convertToPHPValue() method, which always returns an array. It ensures type consistency by disallowing null at the PHP level.
-
Ignore property nullability when mapping a nullable database field with simple_array. This approach gives developers the flexibility to define the property as nullable if they intend to store null. The SimpleArrayType::convertToDatabaseValue() method will handle the null value correctly during persistence.
If maintainers agree with the issue and will choose the desired way, I can try to provide the fix.