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

voku / httpful / 42395164963

07 Nov 2026 05:12PM UTC coverage: 95.599% (-0.2%) from 95.82%
42395164963

push

github

web-flow
Update dependency psr/http-factory to 1.1.* (#38)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

2737 of 2863 relevant lines covered (95.6%)

59.7 hits per line

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

98.36
/src/Httpful/Http.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Httpful;
6

7
use Httpful\Exception\ResponseException;
8
use Psr\Http\Message\StreamInterface;
9

10
class Http
11
{
12
    const DELETE = 'DELETE';
13

14
    const GET = 'GET';
15

16
    const HEAD = 'HEAD';
17

18
    const OPTIONS = 'OPTIONS';
19

20
    const PATCH = 'PATCH';
21

22
    const POST = 'POST';
23

24
    const PUT = 'PUT';
25

26
    /**
27
     * The QUERY method (RFC 10008) asks a resource to perform a query described
28
     * by the request content. Unlike POST it is safe and idempotent, and unlike
29
     * GET it carries a request body.
30
     */
31
    const QUERY = 'QUERY';
32

33
    const TRACE = 'TRACE';
34

35
    const HTTP_1_0 = '1.0';
36

37
    const HTTP_1_1 = '1.1';
38

39
    const HTTP_2_0 = '2';
40

41
    const HTTP_3 = '3';
42

43
    /**
44
     * @return list<string>
45
     */
46
    public static function allMethods(): array
47
    {
48
        return [
465 ✔
49
            self::HEAD,
465 ✔
50
            self::POST,
465 ✔
51
            self::GET,
465 ✔
52
            self::PUT,
465 ✔
53
            self::DELETE,
465 ✔
54
            self::OPTIONS,
465 ✔
55
            self::TRACE,
465 ✔
56
            self::PATCH,
465 ✔
57
            self::QUERY,
465 ✔
58
        ];
465 ✔
59
    }
60

61
    /**
62
     * @return list<string> list of (always) idempotent HTTP methods
63
     */
64
    public static function idempotentMethods(): array
65
    {
66
        return [
4 ✔
67
            self::HEAD,
4 ✔
68
            self::GET,
4 ✔
69
            self::PUT,
4 ✔
70
            self::DELETE,
4 ✔
71
            self::OPTIONS,
4 ✔
72
            self::TRACE,
4 ✔
73
            self::PATCH,
4 ✔
74
            self::QUERY,
4 ✔
75
        ];
4 ✔
76
    }
77

78
    /**
79
     * @param string $method HTTP method
80
     *
81
     * @return bool
82
     */
83
    public static function isIdempotent($method): bool
84
    {
85
        return \in_array($method, self::idempotentMethods(), true);
2 ✔
86
    }
87

88
    /**
89
     * @param string $method HTTP method
90
     *
91
     * @return bool
92
     */
93
    public static function isNotIdempotent($method): bool
94
    {
95
        return !\in_array($method, self::idempotentMethods(), true);
2 ✔
96
    }
97

98
    /**
99
     * @param string $method HTTP method
100
     *
101
     * @return bool
102
     */
103
    public static function isSafeMethod($method): bool
104
    {
105
        return \in_array($method, self::safeMethods(), true);
2 ✔
106
    }
107

108
    /**
109
     * @param string $method HTTP method
110
     *
111
     * @return bool
112
     */
113
    public static function isUnsafeMethod($method): bool
114
    {
115
        return !\in_array($method, self::safeMethods(), true);
2 ✔
116
    }
117

118
    /**
119
     * @param int $code
120
     *
121
     * @throws \Exception
122
     *
123
     * @return string
124
     */
125
    public static function reason(int $code): string
126
    {
127
        $codes = self::responseCodes();
153 ✔
128

129
        if (!\array_key_exists($code, $codes)) {
153 ✔
130
            throw new ResponseException('Unable to parse response code from HTTP response due to malformed response. Code: ' . $code);
2 ✔
131
        }
132

133
        return $codes[$code];
151 ✔
134
    }
135

136
    /**
137
     * @param int $code
138
     *
139
     * @return bool
140
     */
141
    public static function responseCodeExists(int $code): bool
142
    {
143
        return \array_key_exists($code, self::responseCodes());
24 ✔
144
    }
145

146
    /**
147
     * @return list<string> of HTTP method strings
148
     */
149
    public static function safeMethods(): array
150
    {
151
        return [
4 ✔
152
            self::HEAD,
4 ✔
153
            self::GET,
4 ✔
154
            self::OPTIONS,
4 ✔
155
            self::TRACE,
4 ✔
156
            self::QUERY,
4 ✔
157
        ];
4 ✔
158
    }
159

160
    /**
161
     * Create a new stream based on the input type.
162
     *
163
     * Options is an associative array that can contain the following keys:
164
     * - metadata: Array of custom metadata.
165
     * - size: Size of the stream.
166
     *
167
     * @param mixed               $resource
168
     * @param array<string, mixed> $options
169
     *
170
     * @throws \InvalidArgumentException if the $resource arg is not valid
171
     *
172
     * @return StreamInterface
173
     */
174
    public static function stream($resource = '', array $options = []): StreamInterface
175
    {
176
        // init
177
        $options['serialized'] = false;
36 ✔
178

179
        if (\is_array($resource)) {
36 ✔
180
            $resource = \serialize($resource);
12 ✔
181

182
            $options['serialized'] = true;
12 ✔
183
        }
184

185
        if (\is_scalar($resource)) {
36 ✔
186
            $stream = \fopen('php://temp', 'r+b');
30 ✔
187

188
            if (!\is_resource($stream)) {
30 ✔
189
                throw new \RuntimeException('fopen must create a resource');
×
190
            }
191

192
            if ($resource !== '') {
30 ✔
193
                \fwrite($stream, (string) $resource);
23 ✔
194
                \fseek($stream, 0);
23 ✔
195
            }
196

197
            return new Stream($stream, $options);
30 ✔
198
        }
199

200
        switch (\gettype($resource)) {
14 ✔
201
            case 'resource':
14 ✔
202
                return new Stream($resource, $options);
1 ✔
203
            case 'object':
13 ✔
204
                if ($resource instanceof StreamInterface) {
11 ✔
205
                    return $resource;
7 ✔
206
                }
207

208
                if (\method_exists($resource, '__toString')) {
4 ✔
209
                    return self::stream((string) $resource, $options);
2 ✔
210
                }
211

212
                break;
2 ✔
213
            case 'NULL':
2 ✔
214
                $stream = \fopen('php://temp', 'r+b');
2 ✔
215

216
                if (!\is_resource($stream)) {
2 ✔
217
                    throw new \RuntimeException('fopen must create a resource');
×
218
                }
219

220
                return new Stream($stream, $options);
2 ✔
221
        }
222

223
        throw new \InvalidArgumentException('Invalid resource type: ' . \gettype($resource));
2 ✔
224
    }
225

226
    /**
227
     * get all response-codes
228
     *
229
     * @return array<int, string>
230
     */
231
    private static function responseCodes(): array
232
    {
233
        return [
155 ✔
234
            100 => 'Continue',
155 ✔
235
            101 => 'Switching Protocols',
155 ✔
236
            102 => 'Processing',
155 ✔
237
            200 => 'OK',
155 ✔
238
            201 => 'Created',
155 ✔
239
            202 => 'Accepted',
155 ✔
240
            203 => 'Non-Authoritative Information',
155 ✔
241
            204 => 'No Content',
155 ✔
242
            205 => 'Reset Content',
155 ✔
243
            206 => 'Partial Content',
155 ✔
244
            207 => 'Multi-Status',
155 ✔
245
            300 => 'Multiple Choices',
155 ✔
246
            301 => 'Moved Permanently',
155 ✔
247
            302 => 'Found',
155 ✔
248
            303 => 'See Other',
155 ✔
249
            304 => 'Not Modified',
155 ✔
250
            305 => 'Use Proxy',
155 ✔
251
            306 => 'Switch Proxy',
155 ✔
252
            307 => 'Temporary Redirect',
155 ✔
253
            400 => 'Bad Request',
155 ✔
254
            401 => 'Unauthorized',
155 ✔
255
            402 => 'Payment Required',
155 ✔
256
            403 => 'Forbidden',
155 ✔
257
            404 => 'Not Found',
155 ✔
258
            405 => 'Method Not Allowed',
155 ✔
259
            406 => 'Not Acceptable',
155 ✔
260
            407 => 'Proxy Authentication Required',
155 ✔
261
            408 => 'Request Timeout',
155 ✔
262
            409 => 'Conflict',
155 ✔
263
            410 => 'Gone',
155 ✔
264
            411 => 'Length Required',
155 ✔
265
            412 => 'Precondition Failed',
155 ✔
266
            413 => 'Request Entity Too Large',
155 ✔
267
            414 => 'Request-URI Too Long',
155 ✔
268
            415 => 'Unsupported Media Type',
155 ✔
269
            416 => 'Requested Range Not Satisfiable',
155 ✔
270
            417 => 'Expectation Failed',
155 ✔
271
            418 => 'I\'m a teapot',
155 ✔
272
            422 => 'Unprocessable Entity',
155 ✔
273
            423 => 'Locked',
155 ✔
274
            424 => 'Failed Dependency',
155 ✔
275
            425 => 'Unordered Collection',
155 ✔
276
            426 => 'Upgrade Required',
155 ✔
277
            429 => 'Too Many Requests',
155 ✔
278
            449 => 'Retry With',
155 ✔
279
            450 => 'Blocked by Windows Parental Controls',
155 ✔
280
            500 => 'Internal Server Error',
155 ✔
281
            501 => 'Not Implemented',
155 ✔
282
            502 => 'Bad Gateway',
155 ✔
283
            503 => 'Service Unavailable',
155 ✔
284
            504 => 'Gateway Timeout',
155 ✔
285
            505 => 'HTTP Version Not Supported',
155 ✔
286
            506 => 'Variant Also Negotiates',
155 ✔
287
            507 => 'Insufficient Storage',
155 ✔
288
            509 => 'Bandwidth Limit Exceeded',
155 ✔
289
            510 => 'Not Extended',
155 ✔
290
        ];
155 ✔
291
    }
292
}
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