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

api-platform / core / 20895290858

11 Jan 2026 12:40PM UTC coverage: 25.25% (-3.6%) from 28.875%
20895290858

Pull #7667

github

VincentLanglet
Fix
Pull Request #7667: POC NameConverterAwareInterface

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

14884 existing lines in 487 files now uncovered.

14719 of 58293 relevant lines covered (25.25%)

30.74 hits per line

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

75.0
/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) {
440✔
UNCOV
37
            return ($parameter instanceof HeaderParameterInterface ? $request->attributes->get('_api_header_parameters') : $request->attributes->get('_api_query_parameters')) ?? [];
438✔
38
        }
39

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

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

UNCOV
54
        if ($parameter instanceof HeaderParameterInterface) {
440✔
UNCOV
55
            $key = strtolower($key);
70✔
56
        }
57

UNCOV
58
        $parsedKey = explode('[:property]', $key);
440✔
UNCOV
59
        if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
440✔
UNCOV
60
            $key = $parsedKey[0];
378✔
UNCOV
61
        } elseif (str_contains($key, '[')) {
426✔
UNCOV
62
            preg_match_all('/[^\[\]]+/', $key, $matches);
64✔
UNCOV
63
            $key = array_shift($matches[0]);
64✔
UNCOV
64
            $accessors = $matches[0];
64✔
65
        }
66

UNCOV
67
        $value = $values[$key] ?? new ParameterNotFound();
440✔
UNCOV
68
        foreach ($accessors ?? [] as $accessor) {
440✔
UNCOV
69
            if ($value instanceof ParameterNotFound) {
64✔
UNCOV
70
                break;
52✔
71
            }
72

UNCOV
73
            if (\is_array($value) && isset($value[$accessor])) {
24✔
UNCOV
74
                $value = $value[$accessor];
24✔
UNCOV
75
            } elseif (\is_array($value) && array_is_list($value)) {
20✔
76
                $l = [];
×
77
                foreach ($value as $i) {
×
78
                    if (\is_array($i) && isset($i[$accessor])) {
×
79
                        $l[] = $i[$accessor];
×
80
                    }
81
                }
82

83
                if (!$l) {
×
84
                    $value = new ParameterNotFound();
×
85
                } else {
86
                    $value = $l;
×
87
                }
88
            } else {
UNCOV
89
                $value = new ParameterNotFound();
20✔
90
            }
91
        }
92

UNCOV
93
        if ($value instanceof ParameterNotFound) {
440✔
UNCOV
94
            return $value;
422✔
95
        }
96

UNCOV
97
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
398✔
UNCOV
98
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
398✔
99

100
        // type-info 7.2
UNCOV
101
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
398✔
102
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
103
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
104
                    break;
×
105
                }
106
            }
107
        }
108

UNCOV
109
        if ($isCollection && true === $parameter->getCastToArray() && !\is_array($value)) {
398✔
UNCOV
110
            $value = [$value];
2✔
111
        }
112

UNCOV
113
        if (!$isCollection && $parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
398✔
UNCOV
114
            $value = $value[0];
42✔
115
        }
116

UNCOV
117
        if (true === $parameter->getCastToNativeType() && ($castFn = $parameter->getCastFn())) {
398✔
UNCOV
118
            if (\is_array($value)) {
64✔
119
                $value = array_map(fn ($v) => $castFn($v, $parameter), $value);
×
120
            } else {
UNCOV
121
                $value = $castFn($value, $parameter);
64✔
122
            }
123
        }
124

UNCOV
125
        return $value;
398✔
126
    }
127
}
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