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

sirn-se / websocket-php / 7071466687

02 Dec 2023 04:59PM UTC coverage: 99.659%. First build
7071466687

push

github

sirn-se
Http middleware, cleanup

37 of 40 new or added lines in 5 files covered. (92.5%)

876 of 879 relevant lines covered (99.66%)

20.88 hits per line

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

97.3
/src/Http/Message.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2023 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Http;
11

12
use BadMethodCallException;
13
use InvalidArgumentException;
14
use Psr\Http\Message\{
15
    MessageInterface,
16
    StreamInterface
17
};
18

19
/**
20
 * Phrity\WebSocket\Http\Message class.
21
 * Only used for handshake procedure.
22
 */
23
abstract class Message implements MessageInterface
24
{
25
    protected $version = '1.1';
26
    protected $headers = [];
27

28
    /**
29
     * Retrieves the HTTP protocol version as a string.
30
     * @return string HTTP protocol version.
31
     */
32
    public function getProtocolVersion(): string
33
    {
34
        return $this->version;
73✔
35
    }
36

37
    /**
38
     * Return an instance with the specified HTTP protocol version.
39
     * @param string $version HTTP protocol version
40
     * @return static
41
     */
42
    public function withProtocolVersion(string $version): self
43
    {
44
        $new = clone $this;
59✔
45
        $new->version = $version;
59✔
46
        return $new;
59✔
47
    }
48

49
    /**
50
     * Retrieves all message header values.
51
     * @return string[][] Returns an associative array of the message's headers.
52
     */
53
    public function getHeaders(): array
54
    {
55
        return array_merge(...array_values($this->headers));
73✔
56
    }
57

58
    /**
59
     * Checks if a header exists by the given case-insensitive name.
60
     * @param string $name Case-insensitive header field name.
61
     * @return bool Returns true if any header names match the given header.
62
     */
63
    public function hasHeader(string $name): bool
64
    {
65
        return array_key_exists(strtolower($name), $this->headers);
72✔
66
    }
67

68
    /**
69
     * Retrieves a message header value by the given case-insensitive name.
70
     * @param string $name Case-insensitive header field name.
71
     * @return string[] An array of string values as provided for the given header.
72
     */
73
    public function getHeader(string $name): array
74
    {
75
        return $this->hasHeader($name)
62✔
76
            ? array_merge(...array_values($this->headers[strtolower($name)] ?: []))
58✔
77
            : [];
62✔
78
    }
79

80
    /**
81
     * Retrieves a comma-separated string of the values for a single header.
82
     * @param string $name Case-insensitive header field name.
83
     * @return string A string of values as provided for the given header.
84
     */
85
    public function getHeaderLine(string $name): string
86
    {
87
        return implode(',', $this->getHeader($name));
60✔
88
    }
89

90
    /**
91
     * Return an instance with the provided value replacing the specified header.
92
     * @param string $name Case-insensitive header field name.
93
     * @param string|string[] $value Header value(s).
94
     * @return static
95
     * @throws \InvalidArgumentException for invalid header names or values.
96
     */
97
    public function withHeader(string $name, $value): self
98
    {
99
        $new = clone $this;
66✔
100
        if ($this->hasHeader($name)) {
66✔
101
            unset($new->headers[strtolower($name)]);
1✔
102
        }
103
        $new->handleHeader($name, $value);
66✔
104
        return $new;
62✔
105
    }
106

107
    /**
108
     * Return an instance with the specified header appended with the given value.
109
     * @param string $name Case-insensitive header field name to add.
110
     * @param string|string[] $value Header value(s).
111
     * @return static
112
     * @throws \InvalidArgumentException for invalid header names.
113
     * @throws \InvalidArgumentException for invalid header values.
114
     */
115
    public function withAddedHeader(string $name, $value): self
116
    {
117
        $new = clone $this;
1✔
118
        $new->handleHeader($name, $value);
1✔
119
        return $new;
1✔
120
    }
121

122
    /**
123
     * Return an instance without the specified header.
124
     * @param string $name Case-insensitive header field name to remove.
125
     * @return static
126
     */
127
    public function withoutHeader(string $name): self
128
    {
129
        $new = clone $this;
1✔
130
        if ($this->hasHeader($name)) {
1✔
131
            unset($new->headers[strtolower($name)]);
1✔
132
        }
133
        return $new;
1✔
134
    }
135

136
    /**
137
     * Not implemented, WebSocket only use headers.
138
     */
139
    public function getBody(): StreamInterface
140
    {
141
        throw new BadMethodCallException("Not implemented.");
2✔
142
    }
143

144
    /**
145
     * Not implemented, WebSocket only use headers.
146
     */
147
    public function withBody(StreamInterface $body): self
148
    {
149
        throw new BadMethodCallException("Not implemented.");
2✔
150
    }
151

152
    public function getAsArray(): array
153
    {
154
        $lines = [];
70✔
155
        foreach ($this->getHeaders() as $name => $values) {
70✔
156
            foreach ($values as $value) {
59✔
157
                $lines[] = "{$name}: {$value}";
59✔
158
            }
159
        }
160
        return $lines;
70✔
161
    }
162

163
    public function __toString(): string
164
    {
NEW
165
        return get_class($this);
×
166
    }
167

168
    private function handleHeader(string $name, string|array $value): void
169
    {
170
        // @todo: Add all available characters, these are just some of them.
171
        if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
66✔
172
            throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
2✔
173
        }
174
        $value = is_array($value) ? $value : [$value];
64✔
175
        foreach ($value as $content) {
64✔
176
            if (!(is_string($content) || is_numeric($content)) || empty($content = trim($content))) {
64✔
177
                throw new InvalidArgumentException("Invalid header value(s) provided.");
2✔
178
            }
179
            $this->headers[strtolower($name)][$name][] = $content;
62✔
180
        }
181
    }
182
}
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