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

api-platform / core / 17723449516

15 Sep 2025 05:52AM UTC coverage: 0.0% (-22.6%) from 22.578%
17723449516

Pull #7383

github

web-flow
Merge fa5b61e35 into 949c3c975
Pull Request #7383: fix(metadata): compute isWritable during updates

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

11356 existing lines in 371 files now uncovered.

0 of 48868 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\Serializer\NameConverter\NameConverterInterface;
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(
30
        private readonly ?PropertyMetadataFactoryInterface $decorated = null,
31
        private readonly ?NameConverterInterface $nameConverter = null,
32
    ) {
UNCOV
33
    }
×
34

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

UNCOV
49
        $reflectionClass = null;
×
UNCOV
50
        $reflectionEnum = null;
×
51

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
132
        return $this->handleUserDefinedSchema($propertyMetadata);
×
133
    }
134

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

UNCOV
144
        return $propertyMetadata;
×
145
    }
146
}
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