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

sirn-se / websocket-php / 8346487915

19 Mar 2024 04:15PM UTC coverage: 22.584% (-77.4%) from 100.0%
8346487915

push

github

sirn-se
Temp test verification

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

742 existing lines in 32 files now uncovered.

222 of 983 relevant lines covered (22.58%)

0.23 hits per line

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

67.57
/src/Http/Message.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 BadMethodCallException;
11
use InvalidArgumentException;
12
use Psr\Http\Message\{
13
    MessageInterface,
14
    StreamInterface
15
};
16
use Stringable;
17
use WebSocket\Trait\StringableTrait;
18

19
/**
20
 * Phrity\WebSocket\Http\Message class.
21
 * Only used for handshake procedure.
22
 */
23
abstract class Message implements MessageInterface, Stringable
24
{
25
    use StringableTrait;
26

27
    protected $version = '1.1';
28
    protected $headers = [];
29

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

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

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

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

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

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

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

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

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

138
    /**
139
     * Not implemented, WebSocket only use headers.
140
     */
141
    public function getBody(): StreamInterface
142
    {
UNCOV
143
        throw new BadMethodCallException("Not implemented.");
×
144
    }
145

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

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

165
    private function handleHeader(string $name, mixed $value): void
166
    {
167
        if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
1✔
UNCOV
168
            throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
×
169
        }
170
        $value = is_array($value) ? $value : [$value];
1✔
171
        foreach ($value as $content) {
1✔
172
            if (!is_string($content) && !is_numeric($content)) {
1✔
UNCOV
173
                throw new InvalidArgumentException("Invalid header value(s) provided.");
×
174
            }
175
            $content = trim($content);
1✔
176
            $this->headers[strtolower($name)][$name][] = $content;
1✔
177
        }
178
    }
179
}
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