• 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

84.38
/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
    }
979✔
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) {
867✔
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;
867✔
UNCOV
46
        $actualClass = ($objectClass && (!$value instanceof \Traversable || $this->isResourceClass($objectClass))) ? $this->getObjectClass($value) : null;
867✔
47

UNCOV
48
        if (null === $actualClass && null === $resourceClass) {
867✔
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)) {
867✔
UNCOV
53
            throw new InvalidArgumentException(\sprintf('No resource class found for object of type "%s".', $actualClass));
2✔
54
        }
55

UNCOV
56
        if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
867✔
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)) {
867✔
61
            throw new InvalidArgumentException(\sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass));
×
62
        }
63

UNCOV
64
        $targetClass = $actualClass ?? $resourceClass;
867✔
65

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

UNCOV
70
        $mostSpecificResourceClass = null;
867✔
71

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

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

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

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

UNCOV
88
        return $mostSpecificResourceClass;
867✔
89
    }
90

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

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

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