• 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

77.42
/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 {
UNCOV
33
            $propertiesYaml = Yaml::parse((string) file_get_contents($path), Yaml::PARSE_CONSTANT);
241✔
34
        } catch (ParseException $e) {
×
35
            $e->setParsedFile($path);
×
36

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

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

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

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

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

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

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

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

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

103
    private function buildAttribute(array $resource, string $key, mixed $default = null)
104
    {
UNCOV
105
        if (empty($resource[$key])) {
241✔
UNCOV
106
            return $default;
241✔
107
        }
108

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

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

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

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