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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

96.0
/src/Symfony/Bundle/DependencyInjection/Compiler/AttributeFilterPass.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\Symfony\Bundle\DependencyInjection\Compiler;
15

16
use ApiPlatform\Metadata\ApiFilter;
17
use ApiPlatform\Metadata\Util\AttributeFilterExtractorTrait;
18
use ApiPlatform\Metadata\Util\ReflectionClassRecursiveIterator;
19
use Symfony\Component\DependencyInjection\ChildDefinition;
20
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
24

25
/**
26
 * Registers filter services from {@see ApiFilter} attribute.
27
 *
28
 * @internal
29
 *
30
 * @author Antoine Bluchet <soyuka@gmail.com>
31
 */
32
final class AttributeFilterPass implements CompilerPassInterface
33
{
34
    use AttributeFilterExtractorTrait;
35

36
    private const TAG_FILTER_NAME = 'api_platform.filter';
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function process(ContainerBuilder $container): void
42
    {
UNCOV
43
        $resourceClassDirectories = $container->getParameter('api_platform.resource_class_directories');
3✔
44

UNCOV
45
        foreach (ReflectionClassRecursiveIterator::getReflectionClassesFromDirectories($resourceClassDirectories) as $className => $reflectionClass) {
3✔
UNCOV
46
            $this->createFilterDefinitions($reflectionClass, $container);
3✔
47
        }
48
    }
49

50
    /**
51
     * @throws InvalidArgumentException
52
     */
53
    private function createFilterDefinitions(\ReflectionClass $resourceReflectionClass, ContainerBuilder $container): void
54
    {
UNCOV
55
        foreach ($this->readFilterAttributes($resourceReflectionClass) as $id => [$arguments, $filterClass, $filterAttribute]) {
3✔
UNCOV
56
            if ($container->has($id)) {
3✔
UNCOV
57
                continue;
1✔
58
            }
59

UNCOV
60
            if (null === $filterReflectionClass = $container->getReflectionClass($filterClass, false)) {
3✔
UNCOV
61
                throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $filterClass, $id));
1✔
62
            }
63

UNCOV
64
            if ($container->has($filterClass) && ($parentDefinition = $container->findDefinition($filterClass))->isAbstract()) {
2✔
UNCOV
65
                $definition = new ChildDefinition($parentDefinition->getClass());
1✔
66
            } else {
UNCOV
67
                $definition = new Definition($filterReflectionClass->getName());
2✔
UNCOV
68
                $definition->setAutoconfigured(true);
2✔
69
            }
70

UNCOV
71
            $definition->addTag(self::TAG_FILTER_NAME);
2✔
UNCOV
72
            if ($filterAttribute->alias) {
2✔
UNCOV
73
                $definition->addTag(self::TAG_FILTER_NAME, ['id' => $filterAttribute->alias]);
1✔
74
            }
75

UNCOV
76
            $definition->setAutowired(true);
2✔
77

UNCOV
78
            $parameterNames = [];
2✔
UNCOV
79
            if (null !== $constructorReflectionMethod = $filterReflectionClass->getConstructor()) {
2✔
UNCOV
80
                foreach ($constructorReflectionMethod->getParameters() as $reflectionParameter) {
2✔
UNCOV
81
                    $parameterNames[$reflectionParameter->name] = true;
2✔
82
                }
83
            }
84

UNCOV
85
            foreach ($arguments as $key => $value) {
2✔
UNCOV
86
                if (!isset($parameterNames[$key])) {
1✔
87
                    throw new InvalidArgumentException(\sprintf('Class "%s" does not have argument "$%s".', $filterClass, $key));
×
88
                }
89

UNCOV
90
                $definition->setArgument("$$key", $value);
1✔
91
            }
92

UNCOV
93
            $container->setDefinition($id, $definition);
2✔
94
        }
95
    }
96
}
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