• 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

31.71
/src/Metadata/Extractor/ResourceExtractorTrait.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\Metadata\Extractor;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use Symfony\Component\Config\Util\XmlUtils;
18

19
/**
20
 * Utils for ResourceExtractors.
21
 *
22
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
23
 */
24
trait ResourceExtractorTrait
25
{
26
    private function buildArrayValue(\SimpleXMLElement|array|null $resource, string $key, mixed $default = null): ?array
27
    {
UNCOV
28
        if (\is_object($resource) && $resource instanceof \SimpleXMLElement) {
69✔
UNCOV
29
            if (!isset($resource->{$key.'s'}->{$key})) {
69✔
UNCOV
30
                return $default;
69✔
31
            }
32

33
            return (array) $resource->{$key.'s'}->{$key};
×
34
        }
35

UNCOV
36
        if (empty($resource[$key])) {
69✔
UNCOV
37
            return $default;
69✔
38
        }
39

UNCOV
40
        if (!\is_array($resource[$key])) {
69✔
41
            throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be an array, %s given', $key, \gettype($resource[$key])));
×
42
        }
43

UNCOV
44
        return $resource[$key];
69✔
45
    }
46

47
    /**
48
     * Transforms an attribute's value in a PHP value.
49
     */
50
    private function phpize(\SimpleXMLElement|array|null $resource, string $key, string $type, mixed $default = null): array|bool|int|string|null
51
    {
UNCOV
52
        if (!isset($resource[$key])) {
69✔
UNCOV
53
            return $default;
69✔
54
        }
55

56
        switch ($type) {
UNCOV
57
            case 'bool|string':
69✔
UNCOV
58
                return \is_bool($resource[$key]) || \in_array((string) $resource[$key], ['1', '0', 'true', 'false'], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string');
69✔
UNCOV
59
            case 'string':
69✔
UNCOV
60
                return (string) $resource[$key];
69✔
61
            case 'integer':
×
62
                return (int) $resource[$key];
×
63
            case 'bool':
×
64
                if (\is_object($resource) && $resource instanceof \SimpleXMLElement) {
×
65
                    return (bool) XmlUtils::phpize($resource[$key]);
×
66
                }
67

68
                return \in_array($resource[$key], ['1', 'true', 1, true], false);
×
69
        }
70

71
        throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key])));
×
72
    }
73

74
    private function buildArgs(\SimpleXMLElement $resource): ?array
75
    {
76
        if (!isset($resource->args->arg)) {
×
77
            return null;
×
78
        }
79

80
        $data = [];
×
81
        foreach ($resource->args->arg as $arg) {
×
82
            $data[(string) $arg['id']] = $this->buildValues($arg->values);
×
83
        }
84

85
        return $data;
×
86
    }
87

88
    private function buildExtraArgs(\SimpleXMLElement $resource): ?array
89
    {
90
        if (!isset($resource->extraArgs->arg)) {
×
91
            return null;
×
92
        }
93

94
        $data = [];
×
95
        foreach ($resource->extraArgs->arg as $arg) {
×
96
            $data[(string) $arg['id']] = $this->buildValues($arg->values);
×
97
        }
98

99
        return $data;
×
100
    }
101

102
    private function buildValues(\SimpleXMLElement $resource): array
103
    {
104
        $data = [];
×
105
        foreach ($resource->value as $value) {
×
106
            if (null !== $value->attributes()->name) {
×
107
                $data[(string) $value->attributes()->name] = isset($value->values) ? $this->buildValues($value->values) : XmlUtils::phpize($value->__toString());
×
108
                continue;
×
109
            }
110

111
            $data[] = isset($value->values) ? $this->buildValues($value->values) : XmlUtils::phpize($value->__toString());
×
112
        }
113

114
        return $data;
×
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