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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
    {
UNCOV
37
    }
×
38

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

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

UNCOV
60
        if ($parentPropertyMetadata) {
×
UNCOV
61
            return $this->handleUserDefinedSchema($this->update($parentPropertyMetadata, $propertyMetadata));
×
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
    {
UNCOV
110
        if ($parentPropertyMetadata) {
×
UNCOV
111
            return $parentPropertyMetadata;
×
112
        }
113

UNCOV
114
        throw new PropertyNotFoundException(\sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass));
×
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
    {
UNCOV
122
        foreach (get_class_methods(ApiProperty::class) as $method) {
×
UNCOV
123
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== ($val = $metadata[lcfirst($matches[1])] ?? null) && method_exists($propertyMetadata, "with{$matches[1]}")) {
×
UNCOV
124
                $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
×
125
            }
126
        }
127

UNCOV
128
        return $propertyMetadata;
×
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
UNCOV
135
        if (null !== $propertyMetadata->getSchema()) {
×
136
            $extraProperties = $propertyMetadata->getExtraProperties();
×
137
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
138
        }
139

UNCOV
140
        return $propertyMetadata;
×
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