Uuid::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Protoku\ValueObjects;
4
5
use Ramsey\Uuid\Uuid as UuidRamsey;
6
7
class Uuid
8
{
9
    /**
10
     * @var string
11
     */
12
    private $value;
13
14
    /**
15
     * Uuid constructor.
16
     *
17
     * @param string $value
18
     */
19
    protected function __construct(string $value = null)
20
    {
21
        $this->value = is_null($value) ? self::generate() : $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($value) ? self::generate() : $value can also be of type this<Protoku\ValueObjects\Uuid>. However, the property $value is declared as type string. 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...
22
    }
23
24
    /**
25
     * @return static
26
     */
27
    public static function generate(): string
28
    {
29
        return new static(UuidRamsey::uuid4()->toString());
30
    }
31
32
    /**
33
     * @param string $string
34
     *
35
     * @return string
36
     */
37
    public static function fromString(string $string): string
38
    {
39
        return new static($string);
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getValue(): string
46
    {
47
        return $this->value;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function __toString()
54
    {
55
        return $this->getValue();
56
    }
57
}
58