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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

100.0
/src/Metadata/Util/AttributeFilterExtractorTrait.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\Util;
15

16
use ApiPlatform\Metadata\ApiFilter;
17
use Symfony\Component\String\UnicodeString;
18

19
/**
20
 * Generates a service id for a generic filter.
21
 *
22
 * @internal
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
trait AttributeFilterExtractorTrait
27
{
28
    /**
29
     * Filters annotations to get back only ApiFilter annotations.
30
     *
31
     * @return \Iterator only ApiFilter annotations
32
     */
33
    private function getFilterAttributes(\ReflectionClass|\ReflectionProperty $reflector): \Iterator
34
    {
35
        $attributes = $reflector->getAttributes(ApiFilter::class);
116✔
36

37
        foreach ($attributes as $attribute) {
116✔
38
            yield $attribute->newInstance();
16✔
39
        }
40
    }
41

42
    /**
43
     * Given a filter attribute and reflection elements, find out the properties where the filter is applied.
44
     */
45
    private function getFilterProperties(ApiFilter $filterAttribute, \ReflectionClass $reflectionClass, ?\ReflectionProperty $reflectionProperty = null): array
46
    {
47
        $properties = [];
16✔
48

49
        if ($filterAttribute->properties) {
16✔
50
            foreach ($filterAttribute->properties as $property => $strategy) {
14✔
51
                if (\is_int($property)) {
14✔
52
                    $properties[$strategy] = $properties[$strategy] = null !== $reflectionProperty ? $filterAttribute->strategy : null;
14✔
53
                } else {
54
                    $properties[$property] = $strategy;
8✔
55
                }
56
            }
57

58
            return $properties;
14✔
59
        }
60

61
        if (null !== $reflectionProperty) {
10✔
62
            $properties[$reflectionProperty->getName()] = $filterAttribute->strategy ?: null;
8✔
63

64
            return $properties;
8✔
65
        }
66

67
        if ($filterAttribute->strategy) {
8✔
68
            foreach ($reflectionClass->getProperties() as $reflectionProperty) {
6✔
69
                $properties[$reflectionProperty->getName()] = $filterAttribute->strategy;
6✔
70
            }
71
        }
72

73
        return $properties;
8✔
74
    }
75

76
    /**
77
     * Reads filter attribute from a ReflectionClass.
78
     *
79
     * @return array<string, array{array<string, mixed>, class-string, ApiFilter}> indexed by the filter id, the filter tuple has the filter arguments, the filter class and the ApiFilter attribute instance
80
     */
81
    private function readFilterAttributes(\ReflectionClass $reflectionClass): array
82
    {
83
        $filters = [];
116✔
84

85
        foreach ($this->getFilterAttributes($reflectionClass) as $filterAttribute) {
116✔
86
            $filterClass = $filterAttribute->filterClass;
16✔
87
            $id = $this->generateFilterId($reflectionClass, $filterClass, $filterAttribute->id ?? $filterAttribute->alias);
16✔
88

89
            if (!isset($filters[$id])) {
16✔
90
                $filters[$id] = [$filterAttribute->arguments, $filterClass, $filterAttribute];
16✔
91
            }
92

93
            if ($properties = $this->getFilterProperties($filterAttribute, $reflectionClass)) {
16✔
94
                $filters[$id][0]['properties'] = $properties;
14✔
95
            }
96
        }
97

98
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
116✔
99
            foreach ($this->getFilterAttributes($reflectionProperty) as $filterAttribute) {
106✔
100
                $filterClass = $filterAttribute->filterClass;
8✔
101
                $id = $this->generateFilterId($reflectionClass, $filterClass, $filterAttribute->id ?? $filterAttribute->alias);
8✔
102

103
                if (!isset($filters[$id])) {
8✔
104
                    $filters[$id] = [$filterAttribute->arguments, $filterClass, $filterAttribute];
8✔
105
                }
106

107
                if ($properties = $this->getFilterProperties($filterAttribute, $reflectionClass, $reflectionProperty)) {
8✔
108
                    if (isset($filters[$id][0]['properties'])) {
8✔
109
                        $filters[$id][0]['properties'] = array_merge($filters[$id][0]['properties'], $properties);
8✔
110
                    } else {
111
                        $filters[$id][0]['properties'] = $properties;
8✔
112
                    }
113
                }
114
            }
115
        }
116

117
        $parent = $reflectionClass->getParentClass();
116✔
118

119
        if (false !== $parent) {
116✔
120
            return array_merge($filters, $this->readFilterAttributes($parent));
14✔
121
        }
122

123
        return $filters;
116✔
124
    }
125

126
    /**
127
     * Generates a unique, per-class and per-filter identifier prefixed by `annotated_`.
128
     *
129
     * @param \ReflectionClass $reflectionClass the reflection class of a Resource
130
     * @param string           $filterClass     the filter class
131
     * @param string|null      $filterId        the filter id
132
     */
133
    private function generateFilterId(\ReflectionClass $reflectionClass, string $filterClass, ?string $filterId = null): string
134
    {
135
        $suffix = null !== $filterId ? '_'.$filterId : $filterId;
16✔
136

137
        return 'annotated_'.(new UnicodeString(str_replace('\\', '', $reflectionClass->getName().(new \ReflectionClass($filterClass))->getName().$suffix)))->snake()->toString();
16✔
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

© 2025 Coveralls, Inc