0

I'm working on a Symfony project and I'm starting using Psalm. Almost everything is fine, as I keep getting an error that I don't understand:

ERROR: PossiblyUnusedMethod - src/Repository/PlaylistRepository.php:116:21 - Cannot find any calls to method App\Repository\PlaylistRepository::getPlaylistsFor (see https://psalm.dev/087)
    public function getPlaylistsFor( ?User $user = null ) : array

This function is declared as PossiblyUnusedMethod but I am calling it, as you can see in this code:

// src/Controller/PlaylistController.php
#[ Route( '/playlists', name: 'playlists-home' ) ]
public function playlists(
    EntityManagerInterface $em,
    PlaylistRepository $playlistRepository,
) : RedirectResponse|Response
{
    $params = [
        'controller_name' => self::class,
        'playlists' => $playlistRepository->getPlaylistsForUserAuthed(),
    ];
    return $this->render( 'playlists/home.html.twig', $params );
}
// src/Repository/PlaylistRepository.php
public function getPlaylistsForUserAuthed() : array
{
    $em = SpotifyTools::getEntityManager();
    /** @var UserRepository $userRepository */
    $userRepository = $em->getRepository( User::class );

    return $this->getPlaylistsFor( $userRepository->getUserAuthed() );
}

Can someone help me understand this please?

Solution

As this function is only used in the repo, the error is cleared by changing the visibility of the method from public to private/protected.

Xem
  • 23
  • 1
  • 4
  • I'm not as familiar with psalm as perhaps I should be but just for kicks try changing the visibility of getPlaylistsFor from public to private. Psalm might be complaining about how nothing outside of the repository is calling the method. – Cerad Aug 28 '23 at 18:16
  • Do you have it in a public repo somewhere? It would be interesting to look into this (potential) bug – weirdan Aug 29 '23 at 09:09
  • Thanks, but @Cerad found the problem, by changing the visibility from public to private, the error disappeared. Thanks to both of you! – Xem Aug 29 '23 at 13:24

1 Answers1

0

Wrong positives can happen with psalm and it may be difficult or impossible to fix sometimes.

You can suppress psalm error using @psalm-api:

@api, @psalm-api

Used to tell Psalm that a class or method is used, even if no references to it can be found. Unused issues will be suppressed. For example, in frameworks, controllers are often invoked "magically" without any explicit references to them in your code. You should mark these classes with @psalm-api.

So you can use it above your method:

/**
* @psalm-api
*/
public function getPlaylistsFor( ?User $user = null ) : array
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33