Completed
Branch develop (56ef4a)
by Mohamed
04:14
created

LoggedUser::setLoggedUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Extensions\Auth;
13
14
use Tinyissue\Model\User;
15
16
/**
17
 * @author Mohamed Alsharaf <[email protected]>
18
 */
19
trait LoggedUser
20
{
21
    /**
22
     * @var User
23
     */
24
    protected $loggedUser;
25
26
    /**
27
     * Return instance of the logged user.
28
     *
29
     * @param static $user
0 ignored issues
show
introduced by
The type LoggedUser for parameter $user is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
30
     *
31
     * @return $this
32
     */
33
    public function setLoggedUser($user)
34
    {
35
        $this->loggedUser = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type object<Tinyissue\Extensions\Auth\LoggedUser> is incompatible with the declared type object<Tinyissue\Model\User> of property $loggedUser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
37
        return $this;
38
    }
39
40
    /**
41
     * Return instance of the logged user.
42
     *
43
     * @return User
44
     */
45
    public function getLoggedUser()
46
    {
47
        if (null === $this->loggedUser) {
48
            $this->loggedUser = auth()->user();
0 ignored issues
show
Documentation Bug introduced by
It seems like auth()->user() can also be of type object<Illuminate\Contracts\Auth\Authenticatable>. However, the property $loggedUser is declared as type object<Tinyissue\Model\User>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
        }
50
51
        if (!$this->loggedUser instanceof User) {
52
            throw new \DomainException('Unable to find a valid instance of logged user.');
53
        }
54
55
        return $this->loggedUser;
56
    }
57
}
58