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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

83.87
/src/GraphQl/Type/Definition/IterableType.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\GraphQl\Type\Definition;
15

16
use GraphQL\Error\Error;
17
use GraphQL\Language\AST\BooleanValueNode;
18
use GraphQL\Language\AST\FloatValueNode;
19
use GraphQL\Language\AST\IntValueNode;
20
use GraphQL\Language\AST\ListValueNode;
21
use GraphQL\Language\AST\Node;
22
use GraphQL\Language\AST\NullValueNode;
23
use GraphQL\Language\AST\ObjectValueNode;
24
use GraphQL\Language\AST\StringValueNode;
25
use GraphQL\Language\AST\ValueNode;
26
use GraphQL\Type\Definition\ScalarType;
27
use GraphQL\Utils\Utils;
28

29
/**
30
 * Represents an iterable type.
31
 *
32
 * @author Alan Poulain <contact@alanpoulain.eu>
33
 */
34
final class IterableType extends ScalarType implements TypeInterface
35
{
36
    public function __construct()
37
    {
UNCOV
38
        $this->name = 'Iterable';
146✔
UNCOV
39
        $this->description = 'The `Iterable` scalar type represents an array or a Traversable with any kind of data.';
146✔
40

UNCOV
41
        parent::__construct();
146✔
42
    }
43

44
    public function getName(): string
45
    {
UNCOV
46
        return $this->name;
146✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function serialize(mixed $value): iterable
53
    {
54
        if (!is_iterable($value)) {
5✔
55
            throw new Error(\sprintf('`Iterable` cannot represent non iterable value: %s', Utils::printSafe($value)));
×
56
        }
57

58
        return $value;
5✔
59
    }
60

61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function parseValue(mixed $value): iterable
65
    {
66
        if (!is_iterable($value)) {
×
67
            throw new Error(\sprintf('`Iterable` cannot represent non iterable value: %s', Utils::printSafeJson($value)));
×
68
        }
69

70
        return $value;
×
71
    }
72

73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @param ObjectValueNode|ListValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|NullValueNode $valueNode
77
     */
78
    public function parseLiteral(Node $valueNode, ?array $variables = null): float|array|bool|int|string|null
79
    {
80
        if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) {
3✔
81
            return $this->parseIterableLiteral($valueNode);
3✔
82
        }
83

84
        // Intentionally without message, as all information already in wrapped Exception
85
        throw new \Exception();
×
86
    }
87

88
    private function parseIterableLiteral(StringValueNode|BooleanValueNode|IntValueNode|FloatValueNode|ObjectValueNode|ListValueNode|ValueNode $valueNode): float|array|bool|int|string|null
89
    {
90
        switch ($valueNode) {
91
            case $valueNode instanceof StringValueNode:
3✔
92
            case $valueNode instanceof BooleanValueNode:
3✔
93
                return $valueNode->value;
1✔
94
            case $valueNode instanceof IntValueNode:
3✔
95
                return (int) $valueNode->value;
1✔
96
            case $valueNode instanceof FloatValueNode:
3✔
97
                return (float) $valueNode->value;
1✔
98
            case $valueNode instanceof ObjectValueNode:
3✔
99
                $value = [];
1✔
100
                foreach ($valueNode->fields as $field) {
1✔
101
                    $value[$field->name->value] = $this->parseIterableLiteral($field->value);
1✔
102
                }
103

104
                return $value;
1✔
105
            case $valueNode instanceof ListValueNode:
3✔
106
                $list = [];
3✔
107
                foreach ($valueNode->values as $value) {
3✔
108
                    $list[] = $this->parseIterableLiteral($value);
1✔
109
                }
110

111
                return $list;
3✔
112
            default:
113
                return null;
1✔
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

© 2025 Coveralls, Inc