phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Invalid nullable property check for simple_array mapping type

Open v-noskov opened this issue 7 months ago • 1 comments

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>.

v-noskov avatar Jul 02 '25 20:07 v-noskov

I see two possible ways to address the issue:

  1. 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.

  2. 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.

v-noskov avatar Jul 02 '25 21:07 v-noskov