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

api-platform / core / 14955007251

11 May 2025 10:44AM UTC coverage: 0.0% (-8.5%) from 8.457%
14955007251

Pull #7135

github

web-flow
Merge 8a231af70 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    {
UNCOV
34
    }
×
35

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

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

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

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

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

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

UNCOV
64
        $targetClass = $actualClass ?? $resourceClass;
×
65

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

UNCOV
70
        $mostSpecificResourceClass = null;
×
71

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

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

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

UNCOV
86
        $this->localMostSpecificResourceClassCache[$targetClass] = $mostSpecificResourceClass;
×
87

UNCOV
88
        return $mostSpecificResourceClass;
×
89
    }
90

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

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

UNCOV
106
        return $this->localIsResourceClassCache[$type] = false;
×
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