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

overblog / GraphQLBundle / 21429601207

28 Jan 2026 07:44AM UTC coverage: 98.52% (+0.2%) from 98.346%
21429601207

Pull #1233

github

web-flow
Merge f872e486b into 8c23b87f2
Pull Request #1233: migrate to phpunit 10.5

4528 of 4596 relevant lines covered (98.52%)

38.7 hits per line

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

96.67
/src/Definition/Type/CustomScalarType.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Overblog\GraphQLBundle\Definition\Type;
6

7
use GraphQL\Error\InvariantViolation;
8
use GraphQL\Language\AST\Node;
9
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
10
use GraphQL\Language\AST\ScalarTypeExtensionNode;
11
use GraphQL\Type\Definition\CustomScalarType as BaseCustomScalarType;
12
use GraphQL\Type\Definition\ScalarType;
13
use GraphQL\Utils\Utils;
14

15
use function call_user_func;
16
use function is_callable;
17
use function sprintf;
18
use function uniqid;
19

20
/**
21
 * @phpstan-type CustomScalarConfig array{
22
 *   name?: string|null,
23
 *   description?: string|null,
24
 *   serialize: callable(mixed): mixed,
25
 *   parseValue?: callable(mixed): mixed,
26
 *   parseLiteral?: callable(Node $valueNode, array|null $variables): mixed,
27
 *   astNode?: ScalarTypeDefinitionNode|null,
28
 *   extensionASTNodes?: array<ScalarTypeExtensionNode>|null,
29
 *   scalarType?: ScalarType|callable(): ScalarType|null,
30
 * }
31
 */
32
class CustomScalarType extends BaseCustomScalarType
33
{
34
    /** @phpstan-var CustomScalarConfig */
35
    public array $config;
36

37
    /**
38
     * @phpstan-param CustomScalarConfig $config
39
     */
40
    public function __construct(array $config)
41
    {
42
        $config['name'] ??= uniqid('CustomScalar', true);
23✔
43

44
        parent::__construct($config);
23✔
45
    }
46

47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function serialize($value): mixed
51
    {
52
        return $this->call('serialize', $value);
8✔
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function parseValue($value): mixed
59
    {
60
        return $this->call('parseValue', $value);
7✔
61
    }
62

63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function parseLiteral(/* GraphQL\Language\AST\ValueNode */ $valueNode, ?array $variables = null): mixed
67
    {
68
        return $this->call('parseLiteral', $valueNode);
5✔
69
    }
70

71
    /**
72
     * @param mixed $value
73
     *
74
     * @return mixed
75
     */
76
    private function call(string $type, $value)
77
    {
78
        if (!isset($this->config['scalarType'])) {
12✔
79
            return parent::$type($value);
3✔
80
        }
81

82
        $scalarType = match (true) {
9✔
83
            $this->config['scalarType'] instanceof ScalarType => $this->config['scalarType'],
9✔
84
            is_callable($this->config['scalarType']) => $this->config['scalarType'](),
4✔
85
            default => $this->config['scalarType'],
×
86
        };
9✔
87

88
        return call_user_func([$scalarType, $type], $value); // @phpstan-ignore-line
9✔
89
    }
90

91
    public function assertValid(): void
92
    {
93
        if (!isset($this->config['scalarType'])) {
19✔
94
            parent::assertValid();
2✔
95

96
            return;
2✔
97
        }
98

99
        $scalarType = match (true) {
17✔
100
            $this->config['scalarType'] instanceof ScalarType => $this->config['scalarType'],
17✔
101
            is_callable($this->config['scalarType']) => $this->config['scalarType'](),
10✔
102
            default => $this->config['scalarType'],
2✔
103
        };
17✔
104

105
        if (!$scalarType instanceof ScalarType) {
17✔
106
            throw new InvariantViolation(
4✔
107
                sprintf(
4✔
108
                    '%s must provide a valid "scalarType" instance of %s but got: %s',
4✔
109
                    $this->name,
4✔
110
                    ScalarType::class,
4✔
111
                    Utils::printSafe($scalarType)
4✔
112
                )
4✔
113
            );
4✔
114
        }
115
    }
116
}
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