• 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

85.0
/src/State/Provider/BackedEnumProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\ProviderInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21

22
final class BackedEnumProvider implements ProviderInterface
23
{
24
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
25
    {
UNCOV
26
        $resourceClass = $operation->getClass();
16✔
UNCOV
27
        if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) {
16✔
28
            throw new RuntimeException('This resource is not an enum');
×
29
        }
30

UNCOV
31
        if ($operation instanceof CollectionOperationInterface) {
16✔
UNCOV
32
            return $resourceClass::cases();
7✔
33
        }
34

UNCOV
35
        $id = $uriVariables['id'] ?? null;
9✔
UNCOV
36
        if (null === $id) {
9✔
37
            throw new NotFoundHttpException('Not Found');
×
38
        }
39

UNCOV
40
        if ($enum = $this->resolveEnum($resourceClass, $id)) {
9✔
UNCOV
41
            return $enum;
7✔
42
        }
43

UNCOV
44
        throw new NotFoundHttpException('Not Found');
2✔
45
    }
46

47
    /**
48
     * @param class-string $resourceClass
49
     */
50
    private function resolveEnum(string $resourceClass, string|int $id): ?\BackedEnum
51
    {
UNCOV
52
        $reflectEnum = new \ReflectionEnum($resourceClass);
9✔
UNCOV
53
        $type = (string) $reflectEnum->getBackingType();
9✔
54

UNCOV
55
        if ('int' === $type) {
9✔
UNCOV
56
            if (!is_numeric($id)) {
8✔
57
                return null;
×
58
            }
UNCOV
59
            $enum = $resourceClass::tryFrom((int) $id);
8✔
60
        } else {
UNCOV
61
            $enum = $resourceClass::tryFrom($id);
1✔
62
        }
63

64
        // @deprecated enums will be indexable only by value in 4.0
UNCOV
65
        $enum ??= array_reduce($resourceClass::cases(), static fn ($c, \BackedEnum $case) => $id === $case->name ? $case : $c, null);
9✔
66

UNCOV
67
        return $enum;
9✔
68
    }
69
}
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