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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

81.63
/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
    {
30
    }
2,252✔
31

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

46
        $reflectionClass = null;
532✔
47
        $reflectionEnum = null;
532✔
48

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

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

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

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

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

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

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

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

100
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
521✔
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
    {
110
        if (null !== $parentPropertyMetadata) {
521✔
111
            return $parentPropertyMetadata;
521✔
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
    {
119
        if (null === $propertyMetadata) {
142✔
120
            return $this->handleUserDefinedSchema($attribute);
×
121
        }
122

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

129
        return $this->handleUserDefinedSchema($propertyMetadata);
142✔
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
136
        if (null !== $propertyMetadata->getSchema()) {
142✔
137
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
3✔
138
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
3✔
139
        }
140

141
        return $propertyMetadata;
142✔
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

© 2026 Coveralls, Inc