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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

83.33
/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
25
    {
26
        $resourceClass = $operation->getClass();
32✔
27
        if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) {
32✔
28
            throw new RuntimeException('This resource is not an enum');
×
29
        }
30

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

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

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

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

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

55
        if ('int' === $type) {
18✔
56
            if (!is_numeric($id)) {
16✔
57
                return null;
×
58
            }
59

60
            return $resourceClass::tryFrom((int) $id);
16✔
61
        }
62

63
        return $resourceClass::tryFrom($id);
2✔
64
    }
65
}
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

© 2026 Coveralls, Inc