• 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

81.25
/src/Metadata/ResourceClassResolver.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;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
use ApiPlatform\Metadata\Util\ClassInfoTrait;
19

20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author Kévin Dunglas <dunglas@gmail.com>
24
 * @author Samuel ROZE <samuel.roze@gmail.com>
25
 */
26
final class ResourceClassResolver implements ResourceClassResolverInterface
27
{
28
    use ClassInfoTrait;
29
    private array $localIsResourceClassCache = [];
30
    private array $localMostSpecificResourceClassCache = [];
31

32
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
33
    {
34
    }
724✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getResourceClass(mixed $value, ?string $resourceClass = null, bool $strict = false): string
40
    {
41
        if ($strict && null === $resourceClass) {
574✔
42
            throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.');
×
43
        }
44

45
        $objectClass = \is_object($value) ? $this->getObjectClass($value) : null;
574✔
46
        $actualClass = ($objectClass && (!$value instanceof \Traversable || $this->isResourceClass($objectClass))) ? $this->getObjectClass($value) : null;
574✔
47

48
        if (null === $actualClass && null === $resourceClass) {
574✔
49
            throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.');
×
50
        }
51

52
        if (null !== $actualClass && !$this->isResourceClass($actualClass)) {
574✔
53
            throw new InvalidArgumentException(\sprintf('No resource class found for object of type "%s".', $actualClass));
×
54
        }
55

56
        if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
574✔
57
            throw new InvalidArgumentException(\sprintf('Specified class "%s" is not a resource class.', $resourceClass));
×
58
        }
59

60
        if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) {
574✔
61
            throw new InvalidArgumentException(\sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass));
×
62
        }
63

64
        $targetClass = $actualClass ?? $resourceClass;
574✔
65

66
        if (isset($this->localMostSpecificResourceClassCache[$targetClass])) {
574✔
67
            return $this->localMostSpecificResourceClassCache[$targetClass];
570✔
68
        }
69

70
        $mostSpecificResourceClass = null;
574✔
71

72
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) {
574✔
73
            if (!is_a($targetClass, $resourceClassName, true)) {
574✔
74
                continue;
574✔
75
            }
76

77
            if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) {
574✔
78
                $mostSpecificResourceClass = $resourceClassName;
574✔
79
            }
80
        }
81

82
        if (null === $mostSpecificResourceClass) {
574✔
83
            throw new \LogicException('Unexpected execution flow.');
×
84
        }
85

86
        $this->localMostSpecificResourceClassCache[$targetClass] = $mostSpecificResourceClass;
574✔
87

88
        return $mostSpecificResourceClass;
574✔
89
    }
90

91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isResourceClass(string $type): bool
95
    {
96
        if (isset($this->localIsResourceClassCache[$type])) {
675✔
97
            return $this->localIsResourceClassCache[$type];
636✔
98
        }
99

100
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
675✔
101
            if (is_a($type, $resourceClass, true)) {
675✔
102
                return $this->localIsResourceClassCache[$type] = true;
661✔
103
            }
104
        }
105

106
        return $this->localIsResourceClassCache[$type] = false;
486✔
107
    }
108
}
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