Completed
Push — master ( cd6fae...bb592d )
by Brent
01:11
created

HasBinaryUuid::newQueryForRestoration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BinaryUuid;
4
5
use Ramsey\Uuid\Uuid;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
9
trait HasBinaryUuid
10
{
11
    protected static function bootHasBinaryUuid()
12
    {
13
        static::creating(function (Model $model) {
14
            if ($model->{$model->getKeyName()}) {
15
                return;
16
            }
17
18
            $model->{$model->getKeyName()} = static::encodeUuid(Uuid::uuid1());
0 ignored issues
show
Documentation introduced by
\Ramsey\Uuid\Uuid::uuid1() is of type object<Ramsey\Uuid\UuidInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
19
        });
20
    }
21
22
    public static function scopeWithUuid(Builder $builder, $uuid): Builder
23
    {
24
        if (is_array($uuid)) {
25
            return $builder->whereIn('uuid', array_map(function (string $modelUuid) {
26
                return static::encodeUuid($modelUuid);
27
            }, $uuid));
28
        }
29
30
        return $builder->where('uuid', static::encodeUuid($uuid));
31
    }
32
33
    public static function encodeUuid(string $uuid): string
34
    {
35
        $uuid = str_replace('-', '', (string) $uuid);
36
37
        return
38
            substr(hex2bin($uuid), 6, 2)
39
            .substr(hex2bin($uuid), 4, 2)
40
            .substr(hex2bin($uuid), 0, 4)
41
            .substr(hex2bin($uuid), 8, 8);
42
    }
43
44
    public static function decodeUuid(string $binaryUuid): string
45
    {
46
        $uuidWithoutDashes = bin2hex(
47
            substr($binaryUuid, 4, 4)
48
            .substr($binaryUuid, 2, 2)
49
            .substr($binaryUuid, 0, 2)
50
            .substr($binaryUuid, 8, 8)
51
        );
52
53
        $uuidWithDashes = collect([8, 13, 18, 23])->reduce(function ($uuid, $position) {
54
            return substr_replace($uuid, '-', $position, 0);
55
        }, $uuidWithoutDashes);
56
57
        return $uuidWithDashes;
58
    }
59
60
    public function getUuidTextAttribute(): string
61
    {
62
        return static::decodeUuid($this->{$this->getKeyName()});
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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...
63
    }
64
65
    public function setUuidTextAttribute(string $uuid)
66
    {
67
        $this->{$this->getKeyName()} = static::encodeUuid($uuid);
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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...
68
    }
69
70
    public function getQueueableId()
71
    {
72
        return base64_encode($this->uuid);
0 ignored issues
show
Bug introduced by
The property uuid 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...
73
    }
74
75
    public function newQueryForRestoration($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
76
    {
77
        return $this->newQueryWithoutScopes()->whereKey(base64_decode($this->uuid));
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScopes() 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...
78
    }
79
}
80