Suggestion: Add new Types for non-empty-string and positive-int #7449

Open
opened 2026-01-22 15:51:48 +01:00 by admin · 2 comments
Owner

Originally created by @ThomasLandauer on GitHub (Dec 4, 2024).

Feature Request

What

Implement something like Types:NON_EMPTY_STRING and Types::POSITIVE_INT.

Why

Since Symfony 7.2, UserInterface::getUserIdentifier() is annotated with @return non-empty-string.

So if you're fetching that user identifier from the database, PHPStan will report something like:

Property App\Entity\User::$... type mapping mismatch: database can contain string but property expects non-empty-string.

So instead of telling everybody to just @phpstan-ignore it (e.g. at https://github.com/phpstan/phpstan-doctrine/issues/204), I'm wondering if it would be possible to fix the root cause :-)

Originally created by @ThomasLandauer on GitHub (Dec 4, 2024). ### Feature Request #### What Implement something like `Types:NON_EMPTY_STRING` and `Types::POSITIVE_INT`. #### Why Since Symfony 7.2, [`UserInterface::getUserIdentifier()`](https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Security/Core/User/UserInterface.php#L62) is annotated with `@return non-empty-string`. So if you're fetching that user identifier from the database, PHPStan will report something like: > Property App\Entity\User::$... type mapping mismatch: database can contain string but property expects non-empty-string. So instead of telling everybody to just `@phpstan-ignore` it (e.g. at https://github.com/phpstan/phpstan-doctrine/issues/204), I'm wondering if it would be possible to fix the root cause :-)
Author
Owner

@curry684 commented on GitHub (Dec 26, 2024):

The root cause is not with Doctrine, as it does not take any responsibility for content validation whatsoever. As correctly pointed out in the linked issue you can always break it by directly editing the database anyway.

The true root cause is setting an entity field to non-empty-string. Your validation layers should ensure it does not get invalid content, not Doctrine or PHPStan.

@curry684 commented on GitHub (Dec 26, 2024): The root cause is not with Doctrine, as it does not take any responsibility for content validation whatsoever. As correctly pointed out in the linked issue you can always break it by directly editing the database anyway. The true root cause is setting an entity field to non-empty-string. Your validation layers should ensure it does not get invalid content, not Doctrine or PHPStan.
Author
Owner

@dcorroyer commented on GitHub (Jul 21, 2025):

I've just used an assert to fix my PHPStan error - maybe this could help you too.

public function getUserIdentifier(): string
  {
      assert($this->email !== '');
      return $this->email;
  }
@dcorroyer commented on GitHub (Jul 21, 2025): I've just used an assert to fix my PHPStan error - maybe this could help you too. ``` public function getUserIdentifier(): string { assert($this->email !== ''); return $this->email; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7449