UserId::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Model;
14
15
use BenGorUser\User\Domain\Model\Exception\UserIdInvalidException;
16
use Ramsey\Uuid\Uuid;
17
18
/**
19
 * User id domain class.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 * @author Gorka Laucirica <[email protected]>
23
 */
24
final class UserId
25
{
26
    /**
27
     * The id in a primitive type.
28
     *
29
     * @var string|int
30
     */
31
    private $id;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param string|int|null $anId The id in a primitive type
37
     *
38
     * @throws UserIdInvalidException when the id is not valid
39
     */
40
    public function __construct($anId = null)
41
    {
42
        if ($anId !== null && !is_scalar($anId)) {
43
            throw new UserIdInvalidException();
44
        }
45
        $this->id = null === $anId ? Uuid::uuid4()->toString() : $anId;
0 ignored issues
show
Documentation Bug introduced by
It seems like null === $anId ? \Ramsey...4()->toString() : $anId can also be of type double or boolean. However, the property $id is declared as type string|integer. 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...
46
    }
47
48
    /**
49
     * Gets the id.
50
     *
51
     * @return string|int
52
     */
53
    public function id()
54
    {
55
        return $this->id;
56
    }
57
58
    /**
59
     * Method that checks if the id given is equal to the current.
60
     *
61
     * @param UserId $anId The id
62
     *
63
     * @return bool
64
     */
65
    public function equals(UserId $anId)
66
    {
67
        return $this->id() === $anId->id();
68
    }
69
70
    /**
71
     * Magic method that represents the user id in string format.
72
     *
73
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function __toString()
76
    {
77
        return $this->id();
78
    }
79
}
80