Completed
Push — master ( efafaa...1ce8a3 )
by Avtandil
05:26
created

UsesUuidAsPrimary::bootUsesUuidAsPrimary()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 0
dl 0
loc 20
ccs 0
cts 14
cp 0
crap 30
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Eloquent;
6
7
use Ramsey\Uuid\Uuid;
8
9
use function preg_match;
10
11
/**
12
 * @mixin \Illuminate\Database\Eloquent\Model
13
 */
14
trait UsesUuidAsPrimary
15
{
16
    public function isUuidBinary(string $value): bool
17
    {
18
        return isset($value[0]) && ! preg_match('//u', $value);
19
    }
20
21
    public function getIncrementing(): bool
22
    {
23
        return false;
24
    }
25
26
    public function getKeyType(): string
27
    {
28
        return 'string';
29
    }
30
31
    protected static function bootUsesUuidAsPrimary(): void
32
    {
33
        static::creating(static function (UuidAsPrimaryContract $model): void {
34
            $keyName = $model->getKeyName();
35
36
            if (! empty($model->{$keyName})) {
37
                // Do some validation
38
                $model->isUuidBinary($model->{$keyName})
39
                    ? Uuid::fromBytes($model->{$keyName})
40
                    : Uuid::fromString($model->{$keyName});
41
            } elseif (! empty($model->attributes[$keyName])) {
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface Longman\LaravelLodash\El...t\UuidAsPrimaryContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
42
                // Do some validation
43
                $model->isUuidBinary($model->attributes[$keyName])
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface Longman\LaravelLodash\El...t\UuidAsPrimaryContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
44
                    ? Uuid::fromBytes($model->attributes[$keyName])
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface Longman\LaravelLodash\El...t\UuidAsPrimaryContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
45
                    : Uuid::fromString($model->attributes[$keyName]);
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface Longman\LaravelLodash\El...t\UuidAsPrimaryContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
46
            } else {
47
                $model->{$keyName} = Uuid::uuid1()->getBytes();
48
            }
49
        });
50
    }
51
}
52