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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 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

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
    }
×
31

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

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

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

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

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

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

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

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

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

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

UNCOV
100
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
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) {
×
UNCOV
111
            return $parentPropertyMetadata;
×
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) {
×
120
            return $this->handleUserDefinedSchema($attribute);
×
121
        }
122

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

UNCOV
129
        return $this->handleUserDefinedSchema($propertyMetadata);
×
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()) {
×
UNCOV
137
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
×
UNCOV
138
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
139
        }
140

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