Passed
Push — master ( 2cda12...3e0ae5 )
by Melech
02:04 queued 37s
created

Enum::fromValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 1
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Type\BuiltIn\Enum\Trait;
15
16
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use UnitEnum;
0 ignored issues
show
Bug introduced by
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Valkyrja\Type\Throwable\Exception\InvalidArgumentException;
19
use Valkyrja\Type\Throwable\Exception\RuntimeException;
20
21
use function is_int;
22
use function is_string;
23
24
/**
25
 * Trait Enum.
26
 *
27
 * @author Melech Mizrachi
28
 */
29
trait Enum
30
{
31
    /**
32
     * Get a new Type given a value.
33
     */
34
    public static function fromValue(mixed $value): static
35
    {
36
        if ($value instanceof static) {
37
            return $value;
38
        }
39
40
        /** @var class-string<UnitEnum>|class-string<BackedEnum> $class */
41
        $class = static::class;
42
43
        if (! is_string($value) && ! is_int($value)) {
44
            throw new InvalidArgumentException("Invalid value provided for enum $class");
45
        }
46
47
        // Need to check BackedEnum first because all Enums are UnitEnum
48
        if (is_a($class, BackedEnum::class, true)) {
49
            /** @var static $case Get Psalm working and understanding that the static is what gets returned here */
50
            $case = $class::from($value);
51
52
            return $case;
53
        }
54
55
        // Fallback to iterating over all the cases and find a match
56
        foreach ($class::cases() as $case) {
57
            if ($case->name === $value) {
58
                /** @var static $case Get Psalm working and understanding that the static is what gets returned here */
59
                return $case;
60
            }
61
        }
62
63
        throw new InvalidArgumentException("Invalid value provided for enum $class");
64
    }
65
66
    /**
67
     * @inheritDoc
68
     *
69
     * @psalm-suppress ImplementedReturnTypeMismatch The inherited return type 'static' for Valkyrja\Type\Type::asValue is different to the implemented return type for Valkyrja\Type\Types\Enum::asvalue 'Valkyrja\Enum&static'
70
     */
71
    public function asValue(): static
72
    {
73
        return $this;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function asFlatValue(): string|int
80
    {
81
        // Need to check BackedEnum first because all Enums are UnitEnum
82
        if ($this instanceof BackedEnum) {
83
            return $this->value;
84
        }
85
86
        // Fallback to UnitEnum name property
87
        /** @var string $name */
88
        $name = $this->name;
89
90
        return $name;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function modify(callable $closure): static
0 ignored issues
show
Unused Code introduced by
The parameter $closure is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

96
    public function modify(/** @scrutinizer ignore-unused */ callable $closure): static

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

Loading history...
97
    {
98
        throw new RuntimeException('Cannot modify an enum.');
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function jsonSerialize(): string|int
105
    {
106
        return $this->asFlatValue();
107
    }
108
}
109