Description
Symfony version(s) affected
7.2
Description
In Symfony 7.2, the UserInterface was updated to include a @return non-empty-string
annotation for the getUserIdentifier() method. To maintain consistency and improve support for UUIDs as user identifiers, it would be beneficial to add similar return type annotations to relevant methods in the AbstractUid class.
How to reproduce
add this to any entity you want that implements UserInterface
#[ORM\Column(type: 'uuid', unique: true, nullable: false)]
private Uuid $apiPublicKey;
public function getUserIdentifier(): string
{
return $this->apiPublicKey->toString();
}
if you are using code quality tools like psalm, then you will surely get below error
the declared return type 'non-empty-string' for App\Entity\YOUR_CLASS::getUserIdentifier is more specific than the inferred return type 'string' (see https://psalm.dev/070)
public function getUserIdentifier(): string
the type 'string' is more general than the declared return type 'non-empty-string' for App\Entity\YOUR_CLASS::getUserIdentifier (see https://psalm.dev/129)
return $this->apiPublicKey->toString();
Possible Solution
adding the @return non-empty-string
annotation to the following methods in the AbstractUid class:
- toBinary()
- toBase58()
- toBase32()
- toRfc4122()
- toHex()
- hash()
- toString()
- jsonSerialize()
By adding these annotations, we align the AbstractUid class with the new expectations in Symfony 7.2 and ensure consistency when using UUIDs as user identifiers.
Additional Context
In Symfony 7.2, the UserInterface was updated to include a @return non-empty-string
annotation for the getUserIdentifier() method. Since UUIDs are frequently used as user identifiers, adding the same level of type safety to the methods in AbstractUid (which are used to generate various representations of UUIDs) is important for several reasons:
-
Consistency: Aligning with the new Symfony 7.2 update ensures that the handling of user identifiers is consistent across the framework, especially when UUIDs are used.
-
Improved Static Analysis: The
@return non-empty-string
annotation improves static analysis tools like Psalm and PHPStan, making the return type explicit and allowing for better detection of potential issues. -
Better Tooling and IDE Support: Adding specific return type annotations improves IDE autocomplete suggestions, refactoring support, and error detection.