• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overblog / GraphQLBundle / 16147271162

08 Jul 2025 03:15PM UTC coverage: 98.368%. Remained the same
16147271162

Pull #1145

github

web-flow
Merge e7b45c8ce into 92771e1ee
Pull Request #1145: PhpStan for AliasedInterface.php

4339 of 4411 relevant lines covered (98.37%)

39.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/Definition/Type/PhpEnumType.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Overblog\GraphQLBundle\Definition\Type;
6

7
use Exception;
8
use GraphQL\Error\Error;
9
use GraphQL\Error\SerializationError;
10
use GraphQL\Language\AST\EnumTypeDefinitionNode;
11
use GraphQL\Language\AST\EnumTypeExtensionNode;
12
use GraphQL\Language\AST\EnumValueNode;
13
use GraphQL\Language\AST\Node;
14
use GraphQL\Type\Definition\EnumType;
15
use GraphQL\Utils\Utils;
16
use ReflectionEnum;
17
use UnitEnum;
18

19
/**
20
 * @phpstan-import-type EnumValues from EnumType
21
 *
22
 * @phpstan-type PhpEnumTypeConfig array{
23
 *   name?: string|null,
24
 *   description?: string|null,
25
 *   enumClass?: class-string<\UnitEnum>|null,
26
 *   values?: EnumValues|callable(): EnumValues,
27
 *   astNode?: EnumTypeDefinitionNode|null,
28
 *   extensionASTNodes?: array<int, EnumTypeExtensionNode>|null
29
 * }
30
 */
31
class PhpEnumType extends EnumType
32
{
33
    /** @var class-string<UnitEnum>|null */
34
    protected ?string $enumClass = null;
35

36
    /**
37
     * @phpstan-param PhpEnumTypeConfig $config
38
     */
39
    public function __construct(array $config)
40
    {
41
        if (isset($config['enumClass'])) {
31✔
42
            $this->enumClass = $config['enumClass'];
23✔
43
            if (!enum_exists($this->enumClass)) {
23✔
44
                throw new Error(sprintf('Enum class "%s" does not exist.', $this->enumClass));
1✔
45
            }
46
            unset($config['enumClass']);
22✔
47
        }
48

49
        if (!isset($config['values'])) {
30✔
50
            $config['values'] = [];
6✔
51
        }
52

53
        parent::__construct($config);
30✔
54
        if ($this->enumClass) {
30✔
55
            $configValues = $this->config['values'] ?? [];
22✔
56
            if (is_callable($configValues)) {
22✔
57
                $configValues = $configValues();
×
58
            }
59
            $reflection = new ReflectionEnum($this->enumClass);
22✔
60

61
            $enumDefinitions = [];
22✔
62
            foreach ($reflection->getCases() as $case) {
22✔
63
                $enumDefinitions[$case->getName()] = ['value' => $case->getName()];
22✔
64
            }
65

66
            foreach ($configValues as $name => $config) {
22✔
67
                if (!isset($enumDefinitions[$name])) {
16✔
68
                    throw new Error("Enum value {$name} is not defined in {$this->enumClass}");
1✔
69
                }
70
                $enumDefinitions[$name]['description'] = $config['description'] ?? null;
15✔
71
                $enumDefinitions[$name]['deprecationReason'] = $config['deprecationReason'] ?? null;
15✔
72
            }
73

74
            $this->config['values'] = $enumDefinitions;
21✔
75
        }
76
    }
29✔
77

78
    public function isEnumPhp(): bool
79
    {
80
        return null !== $this->enumClass;
4✔
81
    }
82

83
    public function parseValue($value): mixed
84
    {
85
        if ($this->enumClass) {
3✔
86
            try {
87
                return (new ReflectionEnum($this->enumClass))->getCase($value)->getValue();
3✔
88
            } catch (Exception $e) {
1✔
89
                throw new Error("Cannot represent enum of class {$this->enumClass} from value {$value}: ".$e->getMessage());
1✔
90
            }
91
        }
92

93
        return parent::parseValue($value);
×
94
    }
95

96
    public function parseLiteral(Node $valueNode, ?array $variables = null): mixed
97
    {
98
        if ($this->enumClass) {
3✔
99
            if (!$valueNode instanceof EnumValueNode) {
3✔
100
                throw new Error("Cannot represent enum of class {$this->enumClass} from node: {$valueNode->__toString()} is not an enum value");
1✔
101
            }
102
            try {
103
                return (new ReflectionEnum($this->enumClass))->getCase($valueNode->value)->getValue();
2✔
104
            } catch (Exception $e) {
1✔
105
                throw new Error("Cannot represent enum of class {$this->enumClass} from literal {$valueNode->value}: ".$e->getMessage());
1✔
106
            }
107
        }
108

109
        return parent::parseLiteral($valueNode, $variables);
×
110
    }
111

112
    public function serialize($value): mixed
113
    {
114
        if ($this->enumClass) {
6✔
115
            if (!$value instanceof $this->enumClass) {
3✔
116
                $valueStr = Utils::printSafe($value);
1✔
117
                throw new SerializationError("Cannot serialize value {$valueStr} as it must be an instance of enum {$this->enumClass}.");
1✔
118
            }
119

120
            return $value->name;
2✔
121
        }
122

123
        return parent::serialize($value);
3✔
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc