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

sirn-se / websocket-php / 12619595558

05 Jan 2025 12:40PM UTC coverage: 99.715% (-0.3%) from 100.0%
12619595558

Pull #90

github

web-flow
Merge 60d71d18a into 52a374457
Pull Request #90: Adding phpstan for static analysis

15 of 18 new or added lines in 7 files covered. (83.33%)

2 existing lines in 2 files now uncovered.

1048 of 1051 relevant lines covered (99.71%)

22.23 hits per line

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

93.33
/src/Http/Response.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2024 Textalk and contributors.
5
 * This file is part of Websocket PHP and is free software under the ISC License.
6
 */
7

8
namespace WebSocket\Http;
9

10
use InvalidArgumentException;
11
use Phrity\Net\Uri;
12
use Psr\Http\Message\{
13
    ResponseInterface,
14
    UriInterface
15
};
16
use RuntimeException;
17

18
/**
19
 * Phrity\WebSocket\Http\Response class.
20
 * Only used for handshake procedure.
21
 */
22
class Response extends Message implements ResponseInterface
23
{
24
    private static array $codes = [
25
        100 => 'Continue',
26
        101 => 'Switching Protocols',
27
        102 => 'Processing',
28
        103 => 'Early Hints',
29
        200 => 'OK',
30
        201 => 'Created',
31
        202 => 'Accepted',
32
        203 => 'Non-Authoritative Information',
33
        204 => 'No Content',
34
        205 => 'Reset Content',
35
        206 => 'Partial Content',
36
        207 => 'Multi-Status',
37
        208 => 'Already Reported',
38
        226 => 'IM Used',
39
        300 => 'Multiple Choices',
40
        301 => 'Moved Permanently',
41
        302 => 'Found',
42
        303 => 'See Other',
43
        304 => 'Not Modified',
44
        305 => 'Use Proxy',
45
        307 => 'Temporary Redirect',
46
        308 => 'Permanent Redirect',
47
        400 => 'Bad Request',
48
        401 => 'Unauthorized',
49
        402 => 'Payment Required',
50
        403 => 'Forbidden',
51
        404 => 'Not Found',
52
        405 => 'Method Not Allowed',
53
        406 => 'Not Acceptable',
54
        407 => 'Proxy Authentication Required',
55
        408 => 'Request Timeout',
56
        409 => 'Conflict',
57
        410 => 'Gone',
58
        411 => 'Length Required',
59
        412 => 'Precondition Failed',
60
        413 => 'Content Too Large',
61
        414 => 'URI Too Long',
62
        415 => 'Unsupported Media Type',
63
        416 => 'Range Not Satisfiable',
64
        417 => 'Expectation Failed',
65
        421 => 'Misdirected Request',
66
        422 => 'Unprocessable Content',
67
        423 => 'Locked',
68
        424 => 'Failed Dependency',
69
        425 => 'Too Early',
70
        426 => 'Upgrade Required',
71
        428 => 'Precondition Required',
72
        429 => 'Too Many Requests',
73
        431 => 'Request Header Fields Too Large',
74
        451 => 'Unavailable For Legal Reasons',
75
        500 => 'Internal Server Error',
76
        501 => 'Not Implemented',
77
        502 => 'Bad Gateway',
78
        503 => 'Service Unavailable',
79
        504 => 'Gateway Timeout',
80
        505 => 'HTTP Version Not Supported',
81
        506 => 'Variant Also Negotiates',
82
        507 => 'Insufficient Storage',
83
        508 => 'Loop Detected',
84
        510 => 'Not Extended',
85
        511 => 'Network Authentication Required',
86
    ];
87

88
    private int $code;
89
    private string $reason;
90

91
    public function __construct(int $code = 200, string $reasonPhrase = '')
92
    {
93
        $this->code = $code;
81✔
94
        $this->reason = $reasonPhrase;
81✔
95
    }
96

97
    /**
98
     * Gets the response status code.
99
     * @return int Status code.
100
     */
101
    public function getStatusCode(): int
102
    {
103
        return $this->code;
71✔
104
    }
105

106
    /**
107
     * Return an instance with the specified status code and, optionally, reason phrase.
108
     * @param int $code The 3-digit integer result code to set.
109
     * @param string $reasonPhrase The reason phrase to use.
110
     * @return static
111
     * @throws InvalidArgumentException For invalid status code arguments.
112
     */
113
    public function withStatus(int $code, string $reasonPhrase = ''): self
114
    {
115
        if ($code < 100 || $code > 599) {
9✔
NEW
UNCOV
116
            throw new InvalidArgumentException("Invalid status code '{$code}' provided.");
×
117
        }
118
        $new = clone $this;
9✔
119
        $new->code = $code;
9✔
120
        $new->reason = $reasonPhrase;
9✔
121
        return $new;
9✔
122
    }
123

124
    /**
125
     * Gets the response reason phrase associated with the status code.
126
     * @return string Reason phrase; must return an empty string if none present.
127
     */
128
    public function getReasonPhrase(): string
129
    {
130
        $d = self::$codes[$this->code];
32✔
131
        return $this->reason ?: $d;
32✔
132
    }
133

134
    public function __toString(): string
135
    {
136
        return $this->stringable('%s', $this->getStatusCode());
3✔
137
    }
138

139
    public function getAsArray(): array
140
    {
141
        return array_merge([
30✔
142
            "HTTP/{$this->getProtocolVersion()} {$this->getStatusCode()} {$this->getReasonPhrase()}",
30✔
143
        ], parent::getAsArray());
30✔
144
    }
145
}
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