PossiblyUnusedMethod: Cannot find any calls to method in Repository

227 Views Asked by At

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.

1

There are 1 best solutions below

0
Dylan KAS On

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