• 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

87.76
/src/Metadata/Property/Factory/AttributePropertyMetadataFactory.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\Util\Reflection;
20

21
/**
22
 * Creates a property metadata from {@see ApiProperty} attribute.
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
final class AttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface
27
{
28
    public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null)
29
    {
UNCOV
30
    }
979✔
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
36
    {
UNCOV
37
        $parentPropertyMetadata = null;
240✔
UNCOV
38
        if ($this->decorated) {
240✔
39
            try {
UNCOV
40
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
240✔
41
            } catch (PropertyNotFoundException) {
×
42
                // Ignore not found exception from decorated factories
43
            }
44
        }
45

UNCOV
46
        $reflectionClass = null;
240✔
UNCOV
47
        $reflectionEnum = null;
240✔
48

49
        try {
UNCOV
50
            $reflectionClass = new \ReflectionClass($resourceClass);
240✔
51
        } catch (\ReflectionException) {
×
52
        }
53
        try {
UNCOV
54
            $reflectionEnum = new \ReflectionEnum($resourceClass);
240✔
UNCOV
55
        } catch (\ReflectionException) {
232✔
56
        }
57

UNCOV
58
        if (!$reflectionClass && !$reflectionEnum) {
240✔
59
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
60
        }
61

UNCOV
62
        if ($reflectionEnum && $reflectionEnum->hasCase($property)) {
240✔
UNCOV
63
            $reflectionCase = $reflectionEnum->getCase($property);
2✔
UNCOV
64
            if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
2✔
UNCOV
65
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
2✔
66
            }
67
        }
68

UNCOV
69
        if ($reflectionClass->hasProperty($property)) {
240✔
UNCOV
70
            $reflectionProperty = $reflectionClass->getProperty($property);
235✔
UNCOV
71
            if ($attributes = $reflectionProperty->getAttributes(ApiProperty::class)) {
235✔
UNCOV
72
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
66✔
73
            }
74
        }
75

UNCOV
76
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
235✔
UNCOV
77
            $methodName = $prefix.ucfirst($property);
235✔
UNCOV
78
            if (!$reflectionClass->hasMethod($methodName) && !$reflectionEnum?->hasMethod($methodName)) {
235✔
UNCOV
79
                continue;
232✔
80
            }
81

UNCOV
82
            $reflectionMethod = $reflectionClass->hasMethod($methodName) ? $reflectionClass->getMethod($methodName) : $reflectionEnum?->getMethod($methodName);
168✔
UNCOV
83
            if (!$reflectionMethod->isPublic()) {
168✔
84
                continue;
×
85
            }
86

UNCOV
87
            if ($attributes = $reflectionMethod->getAttributes(ApiProperty::class)) {
168✔
UNCOV
88
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
19✔
89
            }
90
        }
91

UNCOV
92
        $attributes = $reflectionClass->getAttributes(ApiProperty::class);
232✔
UNCOV
93
        foreach ($attributes as $attribute) {
232✔
UNCOV
94
            $instance = $attribute->newInstance();
11✔
UNCOV
95
            if ($instance->getProperty() === $property) {
11✔
UNCOV
96
                return $this->createMetadata($instance, $parentPropertyMetadata);
5✔
97
            }
98
        }
99

UNCOV
100
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
232✔
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 (null !== $parentPropertyMetadata) {
232✔
UNCOV
111
            return $parentPropertyMetadata;
232✔
112
        }
113

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

117
    private function createMetadata(ApiProperty $attribute, ?ApiProperty $propertyMetadata = null): ApiProperty
118
    {
UNCOV
119
        if (null === $propertyMetadata) {
70✔
120
            return $this->handleUserDefinedSchema($attribute);
×
121
        }
122

UNCOV
123
        foreach (get_class_methods(ApiProperty::class) as $method) {
70✔
UNCOV
124
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== $val = $attribute->{$method}()) {
70✔
UNCOV
125
                $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
70✔
126
            }
127
        }
128

UNCOV
129
        return $this->handleUserDefinedSchema($propertyMetadata);
70✔
130
    }
131

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

UNCOV
141
        return $propertyMetadata;
70✔
142
    }
143
}
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