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.
Wrong positives can happen with psalm and it may be difficult or impossible to fix sometimes.
You can suppress psalm error using @psalm-api:
So you can use it above your method: