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

api-platform / core / 16053734897

03 Jul 2025 02:52PM UTC coverage: 22.132% (-0.4%) from 22.566%
16053734897

Pull #7274

github

web-flow
Merge 459b4021a into 828b62a4e
Pull Request #7274: fix(symfony): restore graphql_playground option

3 of 3 new or added lines in 1 file covered. (100.0%)

3831 existing lines in 106 files now uncovered.

11560 of 52232 relevant lines covered (22.13%)

23.34 hits per line

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

78.69
/src/Metadata/Extractor/XmlPropertyExtractor.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
 * Extracts an array of metadata from a list of XML files.
21
 *
22
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
23
 */
24
final class XmlPropertyExtractor extends AbstractPropertyExtractor
25
{
26
    public const SCHEMA = __DIR__.'/schema/properties.xsd';
27

28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function extractPath(string $path): void
32
    {
33
        try {
34
            /** @var \SimpleXMLElement $xml */
35
            $xml = simplexml_import_dom(XmlUtils::loadFile($path, self::SCHEMA));
182✔
36
        } catch (\InvalidArgumentException $e) {
182✔
37
            // Ensure it's not a resource
38
            try {
39
                simplexml_import_dom(XmlUtils::loadFile($path, XmlResourceExtractor::SCHEMA));
182✔
40
            } catch (\InvalidArgumentException) {
×
41
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
42
            }
43

44
            // It's a resource: ignore error
45
            return;
182✔
46
        }
47

48
        foreach ($xml->property as $property) {
182✔
49
            $this->properties[$this->resolve((string) $property['resource'])][(string) $property['name']] = [
182✔
50
                'description' => $this->phpize($property, 'description', 'string'),
182✔
51
                'readable' => $this->phpize($property, 'readable', 'bool'),
182✔
52
                'writable' => $this->phpize($property, 'writable', 'bool'),
182✔
53
                'readableLink' => $this->phpize($property, 'readableLink', 'bool'),
182✔
54
                'writableLink' => $this->phpize($property, 'writableLink', 'bool'),
182✔
55
                'required' => $this->phpize($property, 'required', 'bool'),
182✔
56
                'identifier' => $this->phpize($property, 'identifier', 'bool'),
182✔
57
                'default' => $this->phpize($property, 'default', 'string'),
182✔
58
                'example' => $this->phpize($property, 'example', 'string'),
182✔
59
                'deprecationReason' => $this->phpize($property, 'deprecationReason', 'string'),
182✔
60
                'fetchable' => $this->phpize($property, 'fetchable', 'bool'),
182✔
61
                'fetchEager' => $this->phpize($property, 'fetchEager', 'bool'),
182✔
62
                'jsonldContext' => isset($property->jsonldContext->values) ? $this->buildValues($property->jsonldContext->values) : null,
182✔
63
                'openapiContext' => isset($property->openapiContext->values) ? $this->buildValues($property->openapiContext->values) : null,
182✔
64
                'jsonSchemaContext' => isset($property->jsonSchemaContext->values) ? $this->buildValues($property->jsonSchemaContext->values) : null,
182✔
65
                'push' => $this->phpize($property, 'push', 'bool'),
182✔
66
                'security' => $this->phpize($property, 'security', 'string'),
182✔
67
                'securityPostDenormalize' => $this->phpize($property, 'securityPostDenormalize', 'string'),
182✔
68
                'types' => $this->buildArrayValue($property, 'type'),
182✔
69
                'builtinTypes' => $this->buildArrayValue($property, 'builtinType'),
182✔
70
                'schema' => isset($property->schema->values) ? $this->buildValues($property->schema->values) : null,
182✔
71
                'initializable' => $this->phpize($property, 'initializable', 'bool'),
182✔
72
                'extraProperties' => $this->buildExtraProperties($property, 'extraProperties'),
182✔
73
                'iris' => $this->buildArrayValue($property, 'iri'),
182✔
74
                'genId' => $this->phpize($property, 'genId', 'bool'),
182✔
75
                'uriTemplate' => $this->phpize($property, 'uriTemplate', 'string'),
182✔
76
                'property' => $this->phpize($property, 'property', 'string'),
182✔
77
                'nativeType' => $this->phpize($property, 'nativeType', 'string'),
182✔
78
            ];
182✔
79
        }
80
    }
81

82
    private function buildExtraProperties(\SimpleXMLElement $resource, ?string $key = null): ?array
83
    {
84
        if (null !== $key) {
182✔
85
            if (!isset($resource->{$key})) {
182✔
86
                return null;
182✔
87
            }
88

UNCOV
89
            $resource = $resource->{$key};
×
90
        }
91

UNCOV
92
        return $this->buildValues($resource->values);
×
93
    }
94

95
    /**
96
     * @return string[]
97
     */
98
    private function buildValues(\SimpleXMLElement $resource): array
99
    {
100
        $data = [];
×
101
        foreach ($resource->value as $value) {
×
102
            if (null !== $value->attributes()->name) {
×
103
                $data[(string) $value->attributes()->name] = isset($value->values) ? $this->buildValues($value->values) : (string) $value;
×
UNCOV
104
                continue;
×
105
            }
106

UNCOV
107
            $data[] = isset($value->values) ? $this->buildValues($value->values) : (string) $value;
×
108
        }
109

UNCOV
110
        return $data;
×
111
    }
112

113
    private function buildArrayValue(?\SimpleXMLElement $resource, string $key): ?array
114
    {
115
        if (!isset($resource->{$key.'s'}->{$key})) {
182✔
116
            return null;
182✔
117
        }
118

119
        return (array) $resource->{$key.'s'}->{$key};
182✔
120
    }
121

122
    /**
123
     * Transforms an XML attribute's value in a PHP value.
124
     */
125
    private function phpize(\SimpleXMLElement $resource, string $key, string $type, mixed $default = null): array|bool|int|string|null
126
    {
127
        if (!isset($resource[$key])) {
182✔
128
            return $default;
182✔
129
        }
130

131
        return match ($type) {
182✔
UNCOV
132
            'bool|string' => \in_array((string) $resource[$key], ['1', '0', 'true', 'false'], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string'),
×
133
            'string' => (string) $resource[$key],
182✔
UNCOV
134
            'integer' => (int) $resource[$key],
×
135
            'bool' => (bool) XmlUtils::phpize($resource[$key]),
182✔
136
            default => null,
182✔
137
        };
182✔
138
    }
139
}
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