Failed Conditions
Push — master ( 0ef5da...6c8847 )
by Adrien
11:04 queued 07:56
created

PhpEnumType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 12
c 1
b 0
f 0
dl 0
loc 41
ccs 13
cts 14
cp 0.9286
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHPValue() 0 11 4
A getPossibleValues() 0 3 1
A convertToDatabaseValue() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use BackedEnum;
1 ignored issue
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...
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use GraphQL\Utils\Utils;
10
use InvalidArgumentException;
11
12
/**
13
 * Enum based on native PHP backed enum.
14
 */
15
abstract class PhpEnumType extends EnumType
16
{
17
    /**
18
     * Returns the FQCN of the native PHP enum.
19
     *
20
     * @return class-string<BackedEnum>
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string<BackedEnum> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<BackedEnum>.
Loading history...
21
     */
22
    abstract protected function getEnumType(): string;
23
24 2
    protected function getPossibleValues(): array
25
    {
26 2
        return array_map(fn (BackedEnum $str) => $str->value, $this->getEnumType()::cases());
27
    }
28
29
    /**
30
     * @param ?string $value
31
     */
32 3
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?BackedEnum
33
    {
34 3
        if ($value === null || '' === $value) {
35 1
            return null;
36
        }
37
38 3
        if (!is_string($value)) {
39 1
            throw new InvalidArgumentException("Invalid '" . Utils::printSafe($value) . "' value fetched from database for enum " . $this->getName());
40
        }
41
42 2
        return $this->getEnumType()::from($value);
43
    }
44
45 4
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
46
    {
47 4
        if ($value === null) {
48 1
            return null;
49
        }
50
51 3
        if (!is_object($value) || !is_a($value, $this->getEnumType())) {
52 3
            throw new InvalidArgumentException("Invalid '" . Utils::printSafe($value) . "' value to be stored in database for enum " . $this->getName());
53
        }
54
55
        return $value->value;
56
    }
57
}
58