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

api-platform / core / 15063570239

16 May 2025 07:56AM UTC coverage: 27.494% (-0.001%) from 27.495%
15063570239

push

github

web-flow
fix: error formats (#7148)

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

11139 existing lines in 371 files now uncovered.

13476 of 49015 relevant lines covered (27.49%)

74.77 hits per line

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

88.57
/src/State/Util/ParameterParserTrait.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\Util;
15

16
use ApiPlatform\Metadata\HeaderParameter;
17
use ApiPlatform\Metadata\HeaderParameterInterface;
18
use ApiPlatform\Metadata\Parameter;
19
use ApiPlatform\State\ParameterNotFound;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\TypeInfo\Type\CollectionType;
22
use Symfony\Component\TypeInfo\Type\UnionType;
23

24
/**
25
 * @internal
26
 */
27
trait ParameterParserTrait
28
{
29
    /**
30
     * @param array<string, mixed> $context
31
     *
32
     * @return array<string, mixed>
33
     */
34
    private function getParameterValues(Parameter $parameter, ?Request $request, array $context): array
35
    {
UNCOV
36
        if ($request) {
752✔
UNCOV
37
            return ($parameter instanceof HeaderParameterInterface ? $request->attributes->get('_api_header_parameters') : $request->attributes->get('_api_query_parameters')) ?? [];
750✔
38
        }
39

UNCOV
40
        return $context['args'] ?? [];
2✔
41
    }
42

43
    /**
44
     * @param array<string, mixed> $values
45
     *
46
     * @return array<mixed, mixed>|ParameterNotFound|array
47
     */
48
    private function extractParameterValues(Parameter $parameter, array $values): string|ParameterNotFound|array
49
    {
UNCOV
50
        $accessors = null;
752✔
UNCOV
51
        $key = $parameter->getKey();
752✔
UNCOV
52
        if (null === $key) {
752✔
53
            throw new \RuntimeException('A Parameter should have a key.');
×
54
        }
55

UNCOV
56
        if ($parameter instanceof HeaderParameterInterface) {
752✔
UNCOV
57
            $key = strtolower($key);
38✔
58
        }
59

UNCOV
60
        $parsedKey = explode('[:property]', $key);
752✔
UNCOV
61
        if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
752✔
UNCOV
62
            $key = $parsedKey[0];
366✔
UNCOV
63
        } elseif (str_contains($key, '[')) {
740✔
UNCOV
64
            preg_match_all('/[^\[\]]+/', $key, $matches);
506✔
UNCOV
65
            $key = array_shift($matches[0]);
506✔
UNCOV
66
            $accessors = $matches[0];
506✔
67
        }
68

UNCOV
69
        $value = $values[$key] ?? new ParameterNotFound();
752✔
UNCOV
70
        foreach ($accessors ?? [] as $accessor) {
752✔
UNCOV
71
            if (\is_array($value) && isset($value[$accessor])) {
372✔
72
                $value = $value[$accessor];
62✔
73
            } else {
UNCOV
74
                $value = new ParameterNotFound();
370✔
UNCOV
75
                continue;
370✔
76
            }
77
        }
78

UNCOV
79
        if ($value instanceof ParameterNotFound) {
752✔
UNCOV
80
            return $value;
733✔
81
        }
82

UNCOV
83
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
493✔
UNCOV
84
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
493✔
85

86
        // type-info 7.2
UNCOV
87
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
493✔
88
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
89
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
90
                    break;
×
91
                }
92
            }
93
        }
94

UNCOV
95
        if ($isCollection) {
493✔
UNCOV
96
            $value = \is_array($value) ? $value : [$value];
168✔
UNCOV
97
        } elseif ($parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
329✔
UNCOV
98
            $value = $value[0];
6✔
99
        }
100

UNCOV
101
        return $value;
493✔
102
    }
103
}
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