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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 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/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
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
21

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

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

UNCOV
47
        $reflectionClass = null;
×
UNCOV
48
        $reflectionEnum = null;
×
49

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

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

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

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

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

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

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

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

UNCOV
101
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
102
    }
103

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

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

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

UNCOV
124
        foreach (get_class_methods(ApiProperty::class) as $method) {
×
UNCOV
125
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches)) {
×
126
                // BC layer, to remove in 5.0
UNCOV
127
                if ('getBuiltinTypes' === $method) {
×
UNCOV
128
                    if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
129
                        continue;
×
130
                    }
131

132
                    if ($builtinTypes = $attribute->getBuiltinTypes()) {
×
133
                        $propertyMetadata = $propertyMetadata->withBuiltinTypes($builtinTypes);
×
134
                    }
135

136
                    continue;
×
137
                }
138

UNCOV
139
                if (null !== $val = $attribute->{$method}()) {
×
UNCOV
140
                    $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
×
141
                }
142
            }
143
        }
144

UNCOV
145
        return $this->handleUserDefinedSchema($propertyMetadata);
×
146
    }
147

148
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
149
    {
150
        // can't know later if the schema has been defined by the user or by API Platform
151
        // store extra key to make this difference
UNCOV
152
        if (null !== $propertyMetadata->getSchema()) {
×
UNCOV
153
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
×
UNCOV
154
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
155
        }
156

UNCOV
157
        return $propertyMetadata;
×
158
    }
159
}
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