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

ICanBoogie / HTTP / 46320792394

29 Nov 2026 10:46PM UTC coverage: 98.242% (+0.03%) from 98.216%
46320792394

push

github

olvlvl
fix(request): parse query_params when with() changes the URI

with() updated QUERY_STRING but kept the query parameters of the previous URI, unlike from().

8 of 8 new or added lines in 1 file covered. (100.0%)

3 existing lines in 2 files now uncovered.

950 of 967 relevant lines covered (98.24%)

37.86 hits per line

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

98.59
/lib/RequestOptionsMapper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace ICanBoogie\HTTP;
6

7
use InvalidArgumentException;
8

9
/**
10
 * Maps options to the environment.
11
 */
12
final class RequestOptionsMapper
13
{
14
    /**
15
     * Maps options to environment.
16
     *
17
     * The options mapped to the environment are removed from the `$options` array. The options
18
     * that define header fields are kept, see {@see map_headers()}.
19
     *
20
     * @param array<RequestOptions::*, mixed> $options Reference to the options.
21
     * @param array<string, mixed> $env Reference to the environment.
22
     *
23
     * @throws InvalidArgumentException on invalid option.
24
     *
25
     * @return array{
26
     *     path_params?: array<int|string, mixed>,
27
     *     query_params?: array<string, mixed>,
28
     *     request_params?: array<string, mixed>,
29
     *     cookie?: array<string, string>,
30
     *     files?: FileList,
31
     *     headers?: Headers,
32
     *     body_stream?: \SplFileObject,
33
     *     cache_control?: mixed,
34
     *     content_length?: mixed,
35
     *     is_xhr?: mixed,
36
     *     referer?: mixed,
37
     *     user_agent?: mixed
38
     * }
39
     */
40
    public static function map(array $options, array &$env): array
41
    {
42
        foreach ($options as $option => &$value) {
123 ✔
43
            $mapper = self::get_value_mapper($option);
123 ✔
44

45
            if ($mapper) {
123 ✔
46
                $value = $mapper($value);
76 ✔
47

48
                if ($value === null) {
76 ✔
UNCOV
49
                    unset($options[$option]);
×
50
                }
51

52
                continue;
76 ✔
53
            }
54

55
            $mapper = self::get_env_mapper($option);
66 ✔
56

57
            if ($mapper) {
66 ✔
58
                $mapper($value, $env);
46 ✔
59

60
                unset($options[$option]);
46 ✔
61

62
                continue;
46 ✔
63
            }
64

65
            if (self::get_header_mapper($option)) {
21 ✔
66
                continue;
20 ✔
67
            }
68

69
            throw new InvalidArgumentException("Option not supported: `$option`.");
1 ✔
70
        }
71

72
        return $options;
122 ✔
73
    }
74

75
    /**
76
     * Maps options to header fields.
77
     *
78
     * Options that don't define header fields are ignored.
79
     *
80
     * @param array<string, mixed> $options
81
     *
82
     * @return Headers The specified instance if no option defines a header field, a modified
83
     * clone of it otherwise.
84
     *
85
     * @throws InvalidArgumentException if a value is not valid for its header field.
86
     */
87
    public static function map_headers(array $options, Headers $headers): Headers
88
    {
89
        $changed = null;
172 ✔
90

91
        foreach ($options as $option => $value) {
172 ✔
92
            $mapper = self::get_header_mapper($option);
95 ✔
93

94
            if ($mapper) {
95 ✔
95
                $changed ??= clone $headers;
20 ✔
96
                $mapper($value, $changed);
20 ✔
97
            }
98
        }
99

100
        return $changed ?? $headers;
172 ✔
101
    }
102

103
    /**
104
     * @phpstan-return (callable(mixed $value): mixed)|null
105
     */
106
    private static function get_value_mapper(string $option): ?callable
107
    {
108
        return [
123 ✔
109

110
            RequestOptions::OPTION_PATH_PARAMS => fn($value) => $value,
123 ✔
111
            RequestOptions::OPTION_QUERY_PARAMS => fn($value) => $value,
123 ✔
112
            RequestOptions::OPTION_REQUEST_PARAMS => fn($value) => $value,
123 ✔
113
            RequestOptions::OPTION_COOKIE => fn($value) => $value,
123 ✔
114
            RequestOptions::OPTION_FILES => fn($value) => ($value instanceof FileList) ? $value : new FileList($value),
123 ✔
115
            RequestOptions::OPTION_HEADERS => fn($value) => ($value instanceof Headers) ? $value : new Headers($value),
123 ✔
116
            RequestOptions::OPTION_BODY_STREAM => fn($value) => $value,
123 ✔
117

118
        ][$option] ?? null;
123 ✔
119
    }
120

121
    /**
122
     * Returns request properties mappers.
123
     *
124
     * @phpstan-return (callable(mixed $value, array<string, mixed> &$env): mixed)|null
125
     */
126
    private static function get_env_mapper(string $option): ?callable
127
    {
128
        return [
66 ✔
129

130
            RequestOptions::OPTION_IP => function ($value, array &$env) {
66 ✔
131
                if ($value) {
7 ✔
132
                    $env['REMOTE_ADDR'] = $value;
7 ✔
133
                }
134
            },
66 ✔
135
            RequestOptions::OPTION_IS_LOCAL => function ($value, array &$env) {
66 ✔
136
                if ($value) {
1 ✔
137
                    $env['REMOTE_ADDR'] = '::1';
1 ✔
138
                }
139
            },
66 ✔
140
            RequestOptions::OPTION_METHOD => function ($value, array &$env) {
66 ✔
141
                if ($value) {
15 ✔
142
                    $env['REQUEST_METHOD'] = $value;
15 ✔
143
                }
144
            },
66 ✔
145
            RequestOptions::OPTION_PATH => function ($value, array &$env) {
66 ✔
146
                $env['REQUEST_URI'] = $value;
1 ✔
147
            }, // TODO-20130521: handle query string
66 ✔
148
            RequestOptions::OPTION_URI => function ($value, array &$env) {
66 ✔
149
                $env['REQUEST_URI'] = $value;
27 ✔
150
                $qs = strpos($value, '?');
27 ✔
151
                $env['QUERY_STRING'] = $qs === false ? '' : substr($value, $qs + 1);
27 ✔
152
            },
66 ✔
153

154
        ][$option] ?? null;
66 ✔
155
    }
156

157
    /**
158
     * Returns header fields mappers.
159
     *
160
     * @phpstan-return (callable(mixed $value, Headers $headers): void)|null
161
     */
162
    private static function get_header_mapper(string $option): ?callable
163
    {
164
        return [
96 ✔
165

166
            RequestOptions::OPTION_CACHE_CONTROL => function ($value, Headers $headers) {
96 ✔
167
                $headers[Headers::HEADER_CACHE_CONTROL] = $value;
3 ✔
168
            },
96 ✔
169
            RequestOptions::OPTION_CONTENT_LENGTH => function ($value, Headers $headers) {
96 ✔
170
                $headers[Headers::HEADER_CONTENT_LENGTH] = $value;
3 ✔
171
            },
96 ✔
172
            RequestOptions::OPTION_IS_XHR => function ($value, Headers $headers) {
96 ✔
173
                $headers['X-Requested-With'] = $value ? 'XMLHttpRequest' : null;
6 ✔
174
            },
96 ✔
175
            RequestOptions::OPTION_REFERER => function ($value, Headers $headers) {
96 ✔
176
                $headers['Referer'] = $value;
3 ✔
177
            },
96 ✔
178
            RequestOptions::OPTION_USER_AGENT => function ($value, Headers $headers) {
96 ✔
179
                $headers['User-Agent'] = $value;
5 ✔
180
            },
96 ✔
181

182
        ][$option] ?? null;
96 ✔
183
    }
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc