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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

77.78
/src/Metadata/Extractor/YamlPropertyExtractor.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\Yaml\Exception\ParseException;
18
use Symfony\Component\Yaml\Yaml;
19

20
/**
21
 * Extracts an array of metadata from a list of YAML files.
22
 *
23
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
24
 */
25
final class YamlPropertyExtractor extends AbstractPropertyExtractor
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function extractPath(string $path): void
31
    {
32
        try {
33
            $propertiesYaml = Yaml::parse((string) file_get_contents($path), Yaml::PARSE_CONSTANT);
200✔
34
        } catch (ParseException $e) {
×
35
            $e->setParsedFile($path);
×
36

37
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
38
        }
39

40
        if (null === $propertiesYaml = $propertiesYaml['properties'] ?? $propertiesYaml) {
200✔
41
            return;
×
42
        }
43

44
        if (!\is_array($propertiesYaml)) {
200✔
45
            throw new InvalidArgumentException(\sprintf('"properties" setting is expected to be null or an array, %s given in "%s".', \gettype($propertiesYaml), $path));
×
46
        }
47

48
        $this->buildProperties($propertiesYaml);
200✔
49
    }
50

51
    private function buildProperties(array $resourcesYaml): void
52
    {
53
        foreach ($resourcesYaml as $resourceName => $resourceYaml) {
200✔
54
            if (null === $resourceYaml) {
200✔
55
                continue;
×
56
            }
57

58
            $resourceName = $this->resolve($resourceName);
200✔
59

60
            foreach ($resourceYaml as $propertyName => $propertyValues) {
200✔
61
                if (null === $propertyValues) {
200✔
62
                    $this->properties[$resourceName][$propertyName] = null;
×
63
                    continue;
×
64
                }
65

66
                if (!\is_array($propertyValues)) {
200✔
67
                    throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be null or an array, %s given.', $propertyName, \gettype($propertyValues)));
×
68
                }
69

70
                $this->properties[$resourceName][$propertyName] = [
200✔
71
                    'description' => $this->phpize($propertyValues, 'description', 'string'),
200✔
72
                    'readable' => $this->phpize($propertyValues, 'readable', 'bool'),
200✔
73
                    'writable' => $this->phpize($propertyValues, 'writable', 'bool'),
200✔
74
                    'readableLink' => $this->phpize($propertyValues, 'readableLink', 'bool'),
200✔
75
                    'writableLink' => $this->phpize($propertyValues, 'writableLink', 'bool'),
200✔
76
                    'required' => $this->phpize($propertyValues, 'required', 'bool'),
200✔
77
                    'identifier' => $this->phpize($propertyValues, 'identifier', 'bool'),
200✔
78
                    'deprecationReason' => $this->phpize($propertyValues, 'deprecationReason', 'string'),
200✔
79
                    'fetchable' => $this->phpize($propertyValues, 'fetchable', 'bool'),
200✔
80
                    'fetchEager' => $this->phpize($propertyValues, 'fetchEager', 'bool'),
200✔
81
                    'push' => $this->phpize($propertyValues, 'push', 'bool'),
200✔
82
                    'security' => $this->phpize($propertyValues, 'security', 'string'),
200✔
83
                    'securityPostDenormalize' => $this->phpize($propertyValues, 'securityPostDenormalize', 'string'),
200✔
84
                    'initializable' => $this->phpize($propertyValues, 'initializable', 'bool'),
200✔
85
                    'iris' => $this->buildAttribute($propertyValues, 'iris'),
200✔
86
                    'jsonldContext' => $this->buildAttribute($propertyValues, 'jsonldContext'),
200✔
87
                    'openapiContext' => $this->buildAttribute($propertyValues, 'openapiContext'),
200✔
88
                    'jsonSchemaContext' => $this->buildAttribute($propertyValues, 'jsonSchemaContext'),
200✔
89
                    'types' => $this->buildAttribute($propertyValues, 'types'),
200✔
90
                    'extraProperties' => $this->buildAttribute($propertyValues, 'extraProperties'),
200✔
91
                    'default' => $propertyValues['default'] ?? null,
200✔
92
                    'example' => $propertyValues['example'] ?? null,
200✔
93
                    'builtinTypes' => $this->buildAttribute($propertyValues, 'builtinTypes'),
200✔
94
                    'schema' => $this->buildAttribute($propertyValues, 'schema'),
200✔
95
                    'genId' => $this->phpize($propertyValues, 'genId', 'bool'),
200✔
96
                    'uriTemplate' => $this->phpize($propertyValues, 'uriTemplate', 'string'),
200✔
97
                    'property' => $this->phpize($propertyValues, 'property', 'string'),
200✔
98
                    'nativeType' => $this->phpize($propertyValues, 'nativeType', 'string'),
200✔
99
                ];
200✔
100
            }
101
        }
102
    }
103

104
    private function buildAttribute(array $resource, string $key): ?array
105
    {
106
        if (empty($resource[$key])) {
200✔
107
            return null;
200✔
108
        }
109

110
        if (!\is_array($resource[$key])) {
×
111
            throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be an array, %s given', $key, \gettype($resource[$key])));
×
112
        }
113

114
        return $resource[$key];
×
115
    }
116

117
    /**
118
     * Transforms an XML attribute's value in a PHP value.
119
     */
120
    private function phpize(?array $resource, string $key, string $type, mixed $default = null): array|bool|int|string|null
121
    {
122
        if (!isset($resource[$key])) {
200✔
123
            return $default;
200✔
124
        }
125

126
        return match ($type) {
200✔
127
            'bool|string' => \in_array($resource[$key], ['1', '0', 1, 0, 'true', 'false', true, false], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string'),
×
128
            'string' => (string) $resource[$key],
200✔
129
            'integer' => (int) $resource[$key],
×
130
            'bool' => \in_array($resource[$key], ['1', 'true', 1, true], false),
200✔
131
            default => throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key]))),
200✔
132
        };
200✔
133
    }
134
}
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