Passed
Push — master ( 253018...166e4e )
by Davis
38s
created

AbstractEntity::setId()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 7/30/17
6
 * Time: 1:27 PM
7
 */
8
9
namespace DavisPeixoto\BlogCore\Entity;
10
11
12
use Ramsey\Uuid\Exception\InvalidUuidStringException;
13
use Ramsey\Uuid\Uuid;
14
use Ramsey\Uuid\UuidInterface;
15
use stdClass;
16
17
class AbstractEntity extends stdClass
18
{
19
    /**
20
     * @var UuidInterface
21
     */
22
    protected $id;
23
24
    /**
25
     * @return string
26
     */
27 9
    public function getId(): string
28
    {
29 9
        return $this->id->toString();
30
    }
31
32
    /**
33
     * @param string|null $id
34
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be AbstractEntity|null?

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...
35
     * @throws InvalidUuidStringException
36
     */
37 74
    public function setId(string $id = null)
38
    {
39 74
        $uuid = null;
0 ignored issues
show
Unused Code introduced by
$uuid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40 74
        if ($id === null || $id === '') {
41 47
            $uuid = Uuid::uuid4();
42
        } else {
43 61
            if (Uuid::isValid($id)) {
44 60
                $uuid = Uuid::fromString($id);
45
            } else {
46 1
                throw new InvalidUuidStringException('Invalid string');
47
            }
48
        }
49
50 73
        if ($uuid->getVersion() !== Uuid::uuid4()->getVersion()) {
51 1
            throw new InvalidUuidStringException('Not supported version: '.$uuid->getVersion());
52
        }
53
54 72
        $this->id = $uuid;
55 72
    }
56
}
57