Completed
Pull Request — master (#23)
by Brent
03:56
created

HasEnums   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 0 25 3
A getAttribute() 0 30 4
1
<?php
2
3
namespace Spatie\Enum;
4
5
use Spatie\Enum\Exceptions\InvalidEnumError;
6
use Spatie\Enum\Exceptions\InvalidValueException;
7
8
trait HasEnums
9
{
10
    /**
11
     * @param $key
12
     * @param \Spatie\Enum\Enum $enumObject
13
     *
14
     * @return mixed
15
     */
16
    public function setAttribute($key, $enumObject)
17
    {
18
        if (! isset($this->enums[$key])) {
0 ignored issues
show
Bug introduced by
The property enums 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...
19
            return parent::setAttribute($key, $enumObject);
20
        }
21
22
        $enumClass = $this->enums[$key];
23
24
        if (! is_a($enumObject, $enumClass)) {
25
            throw InvalidEnumError::make(
26
                static::class,
27
                $key,
28
                $enumClass,
29
                get_class($enumObject)
30
            );
31
        }
32
33
        $enumValue = $enumObject->getValue();
34
35
        $mappedValue = $enumClass::$map[$enumValue] ?? null;
36
37
        $this->attributes[$key] = $mappedValue ?? $enumValue;
0 ignored issues
show
Bug introduced by
The property attributes 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...
38
39
        return $this;
40
    }
41
42
    public function getAttribute($key)
43
    {
44
        if (! isset($this->enums[$key])) {
45
            return parent::getAttribute($key);
46
        }
47
48
        $enumClass = $this->enums[$key];
49
50
        $storedEnumValue = $this->attributes[$key] ?? null;
51
52
        try {
53
            $enumObject = forward_static_call_array(
54
                $enumClass . '::make',
55
                [$storedEnumValue]
56
            );
57
        } catch (InvalidValueException $exception) {
58
            $mappedEnumValue = array_search($storedEnumValue, $enumClass::$map ?? []);
59
60
            if (! $mappedEnumValue) {
61
                throw new InvalidValueException($storedEnumValue, $enumClass);
62
            }
63
64
            $enumObject = forward_static_call_array(
65
                $enumClass . '::make',
66
                [$mappedEnumValue]
67
            );
68
        }
69
70
        return $enumObject;
71
    }
72
}
73