• 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

45.0
/src/Metadata/Property/Factory/ExtractorPropertyMetadataFactory.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\Property\Factory;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\Extractor\PropertyExtractorInterface;
21
use PHPStan\PhpDocParser\Parser\PhpDocParser;
22
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
23
use Symfony\Component\PropertyInfo\Type as LegacyType;
24
use Symfony\Component\TypeInfo\Type;
25
use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver;
26

27
/**
28
 * Creates properties's metadata using an extractor.
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
32
 */
33
final class ExtractorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
34
{
35
    public function __construct(private readonly PropertyExtractorInterface $extractor, private readonly ?PropertyMetadataFactoryInterface $decorated = null)
36
    {
37
    }
724✔
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
43
    {
44
        $parentPropertyMetadata = null;
194✔
45
        if ($this->decorated) {
194✔
46
            try {
47
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
194✔
48
            } catch (PropertyNotFoundException) {
×
49
                // Ignore not found exception from decorated factories
50
            }
51
        }
52

53
        if (
54
            !property_exists($resourceClass, $property) && !interface_exists($resourceClass)
194✔
55
            || null === ($propertyMetadata = $this->extractor->getProperties()[$resourceClass][$property] ?? null)
194✔
56
        ) {
57
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
194✔
58
        }
59

60
        if ($parentPropertyMetadata) {
4✔
61
            return $this->handleUserDefinedSchema($this->update($parentPropertyMetadata, $propertyMetadata));
4✔
62
        }
63

64
        $apiProperty = new ApiProperty();
×
65

66
        foreach ($propertyMetadata as $key => $value) {
×
67
            if ('builtinTypes' === $key && null !== $value) {
×
68
                if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
69
                    continue;
×
70
                }
71

72
                $apiProperty = $apiProperty->withBuiltinTypes(array_map(static fn (string $builtinType): LegacyType => new LegacyType($builtinType), $value));
×
73

74
                continue;
×
75
            }
76

77
            if ('nativeType' === $key && null !== $value) {
×
78
                if (class_exists(PhpDocParser::class)) {
×
79
                    $apiProperty = $apiProperty->withNativeType((new StringTypeResolver())->resolve($value));
×
80

81
                    continue;
×
82
                }
83

84
                try {
85
                    $apiProperty = $apiProperty->withNativeType(Type::builtin($value));
×
86
                } catch (\ValueError) {
×
87
                    throw new RuntimeException(\sprintf('Cannot create a type from "%s". Try running "composer require phpstan/phpdoc-parser" to support all types.', $value));
×
88
                }
89

90
                continue;
×
91
            }
92

93
            $methodName = 'with'.ucfirst($key);
×
94

95
            if (method_exists($apiProperty, $methodName) && null !== $value) {
×
96
                $apiProperty = $apiProperty->{$methodName}($value);
×
97
            }
98
        }
99

100
        return $this->handleUserDefinedSchema($apiProperty);
×
101
    }
102

103
    /**
104
     * Returns the metadata from the decorated factory if available or throws an exception.
105
     *
106
     * @throws PropertyNotFoundException
107
     */
108
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
109
    {
110
        if ($parentPropertyMetadata) {
194✔
111
            return $parentPropertyMetadata;
194✔
112
        }
113

114
        throw new PropertyNotFoundException(\sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass));
194✔
115
    }
116

117
    /**
118
     * Creates a new instance of metadata if the property is not already set.
119
     */
120
    private function update(ApiProperty $propertyMetadata, array $metadata): ApiProperty
121
    {
122
        foreach (get_class_methods(ApiProperty::class) as $method) {
4✔
123
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== ($val = $metadata[lcfirst($matches[1])] ?? null) && method_exists($propertyMetadata, "with{$matches[1]}")) {
4✔
124
                $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
4✔
125
            }
126
        }
127

128
        return $propertyMetadata;
4✔
129
    }
130

131
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
132
    {
133
        // can't know later if the schema has been defined by the user or by API Platform
134
        // store extra key to make this difference
135
        if (null !== $propertyMetadata->getSchema()) {
4✔
136
            $extraProperties = $propertyMetadata->getExtraProperties();
×
137
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
138
        }
139

140
        return $propertyMetadata;
4✔
141
    }
142
}
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