Issues (39)

src/User/UserService.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use Yiisoft\Access\AccessCheckerInterface;
8
use Yiisoft\User\CurrentUser;
9
10
final class UserService
11
{
12
    private CurrentUser $currentUser;
13
    private UserRepository $repository;
14
    private AccessCheckerInterface $accessChecker;
15
16
    public function __construct(CurrentUser $currentUser, UserRepository $repository, AccessCheckerInterface $accessChecker)
17
    {
18
        $this->currentUser = $currentUser;
19
        $this->repository = $repository;
20
        $this->accessChecker = $accessChecker;
21
    }
22
23
    public function getUser(): ?User
24
    {
25
        return $this->repository->findIdentity($this->currentUser->getId());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->repository...->currentUser->getId()) could return the type Yiisoft\Auth\IdentityInterface which includes types incompatible with the type-hinted return App\User\User|null. Consider adding an additional type-check to rule them out.
Loading history...
It seems like $this->currentUser->getId() can also be of type null; however, parameter $id of App\User\UserRepository::findIdentity() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        return $this->repository->findIdentity(/** @scrutinizer ignore-type */ $this->currentUser->getId());
Loading history...
26
    }
27
28
    public function hasPermission(string $permission): bool
29
    {
30
        $userId = $this->currentUser->getId();
31
        return null !== $userId && $this->accessChecker->userHasPermission($userId, $permission);
32
    }
33
}
34