Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add @return non-empty-string annotation to AbstractUid methods for better user identifier support #59076

Copy link
Copy link
Closed
@niravpateljoin

Description

@niravpateljoin
Issue body actions

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:

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

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

  3. Better Tooling and IDE Support: Adding specific return type annotations improves IDE autocomplete suggestions, refactoring support, and error detection.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.