Completed
Push — master ( b1b2ab...22faf1 )
by Benjamin
01:10
created

HasUuid::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bdelespierre\HasUuid;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Str;
7
use Webpatser\Uuid\Uuid;
8
9
trait HasUuid
10
{
11
    public function __construct(array $attributes = [])
12
    {
13
        parent::__construct($attributes);
14
15
        $this->setKeyType('string')->setIncrementing(false);
0 ignored issues
show
Bug introduced by
It seems like setKeyType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
16
    }
17
18
    protected static function bootHasUuid()
19
    {
20
        static::creating(function ($model) {
21
            if (! $model->getAttribute($model->getKeyName())) {
22
                $model->setAttribute(
23
                    $model->getKeyName(),
24
                    $model->getUuid()
25
                );
26
            }
27
        });
28
    }
29
30
    public function resolveRouteBinding($value, $field = null)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        if (! is_string($value) || ! Uuid::validate($value)) {
33
            return null;
34
        }
35
36
        return parent::resolveRouteBinding($value);
37
    }
38
39
    public function getUuid(): string
40
    {
41
        return (string) Uuid::generate(
42
            $this->getUuidVersion(),
43
            $this->getUuidNode(),
44
            $this->getUuidNamespace()
45
        );
46
    }
47
48
    public function getUuidVersion(): int
49
    {
50
        return $this->uuidVersion ?? 4;
0 ignored issues
show
Bug introduced by
The property uuidVersion does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
    }
52
53
    public function getUuidNode(): ?string
54
    {
55
        if (! isset($this->uuidNode)) {
56
            return null;
57
        }
58
59
        return $this->interpolate($this->uuidNode);
0 ignored issues
show
Bug introduced by
The property uuidNode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
    }
61
62
    public function getUuidNamespace(): ?string
63
    {
64
        // when no explicit namespace is provided,
65
        // the namespace is calculated from the
66
        // app key.
67
        if (! isset($this->uuidNamespace)) {
68
            $key = Config::get('app.key');
69
70
            $key = Str::startsWith($key, 'base64:')
71
                ? self::hash($key)
72
                : self::strToHex($key);
73
74
            // Webpatser/UUID needs 16 bytes for namespace
75
            // which is *slightly* unsafe because Laravel
76
            // generates 32 bytes keys...
77
            return substr($key, 32);
78
        }
79
80
        return $this->interpolate($this->uuidNamespace);
0 ignored issues
show
Bug introduced by
The property uuidNamespace does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
    }
82
83
    protected function interpolate(string $value): ?string
84
    {
85
        if (Str::startsWith($value, ':')) {
86
            $value = $this->getAttribute(
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
                Str::after($value, ':')
88
            );
89
        }
90
91
        return $value;
92
    }
93
94
    protected static function hash(string $key): string
95
    {
96
        return bin2hex(base64_decode(Str::after($key, 'base64:')));
97
    }
98
99
    protected static function strToHex(string $string): string
100
    {
101
        for ($i = 0, $hex = ''; $i < strlen($string); $i++) {
102
            $hex .= substr('0'.dechex(ord($string[$i])), -2);
103
        }
104
105
        return strtoupper($hex);
106
    }
107
}
108