• 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

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

UNCOV
40
        return $context['args'] ?? [];
1✔
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;
375✔
UNCOV
51
        $key = $parameter->getKey();
375✔
UNCOV
52
        if (null === $key) {
375✔
53
            throw new \RuntimeException('A Parameter should have a key.');
×
54
        }
55

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

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

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

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

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

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

UNCOV
95
        if ($isCollection && true === $parameter->getCastToArray() && !\is_array($value)) {
250✔
UNCOV
96
            $value = [$value];
1✔
97
        }
98

UNCOV
99
        if (!$isCollection && $parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
250✔
UNCOV
100
            $value = $value[0];
5✔
101
        }
102

UNCOV
103
        return $value;
250✔
104
    }
105
}
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