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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

80.43
/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
    }
160✔
31

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

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

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

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

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

70
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
4✔
71
        }
72

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

80
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
48✔
81
            $methodName = $prefix.ucfirst($property);
48✔
82
            if (!$reflectionClass->hasMethod($methodName)) {
48✔
83
                continue;
48✔
84
            }
85

86
            $reflectionMethod = $reflectionClass->getMethod($methodName);
36✔
87
            if (!$reflectionMethod->isPublic()) {
36✔
88
                continue;
×
89
            }
90

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

96
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
48✔
97
    }
98

99
    /**
100
     * Returns the metadata from the decorated factory if available or throws an exception.
101
     *
102
     * @throws PropertyNotFoundException
103
     */
104
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
105
    {
106
        if (null !== $parentPropertyMetadata) {
48✔
107
            return $parentPropertyMetadata;
48✔
108
        }
109

110
        throw new PropertyNotFoundException(sprintf('Property "%s" of class "%s" not found.', $property, $resourceClass));
×
111
    }
112

113
    private function createMetadata(ApiProperty $attribute, ApiProperty $propertyMetadata = null): ApiProperty
114
    {
115
        if (null === $propertyMetadata) {
20✔
116
            return $this->handleUserDefinedSchema($attribute);
×
117
        }
118

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

125
        return $this->handleUserDefinedSchema($propertyMetadata);
20✔
126
    }
127

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

137
        return $propertyMetadata;
20✔
138
    }
139
}
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