• 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

44.83
/src/Http/Request.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 Phrity\Net\Uri;
11
use Psr\Http\Message\{
12
    RequestInterface,
13
    UriInterface
14
};
15
use RuntimeException;
16

17
/**
18
 * WebSocket\Http\Request class.
19
 * Only used for handshake procedure.
20
 */
21
class Request extends Message implements RequestInterface
22
{
23
    private $target;
24
    private $method;
25
    private $uri;
26

27
    public function __construct(string $method = 'GET', UriInterface|string|null $uri = null)
28
    {
29
        $this->uri = $uri instanceof Uri ? $uri : new Uri((string)$uri);
1✔
30
        $this->method = $method;
1✔
31
        $this->headers = ['host' => ['Host' => [$this->formatHostHeader($this->uri)]]];
1✔
32
    }
33

34
    /**
35
     * Retrieves the message's request target.
36
     * @return string
37
     */
38
    public function getRequestTarget(): string
39
    {
40
        if ($this->target) {
1✔
UNCOV
41
            return $this->target;
×
42
        }
43
        $uri = (new Uri())->withPath($this->uri->getPath())->withQuery($this->uri->getQuery());
1✔
44
        return $uri->toString(Uri::ABSOLUTE_PATH);
1✔
45
    }
46

47
    /**
48
     * Return an instance with the specific request-target.
49
     * @param mixed $requestTarget
50
     * @return static
51
     */
52
    public function withRequestTarget(mixed $requestTarget): self
53
    {
UNCOV
54
        $new = clone $this;
×
UNCOV
55
        $new->target = $requestTarget;
×
UNCOV
56
        return $new;
×
57
    }
58

59
    /**
60
     * Retrieves the HTTP method of the request.
61
     * @return string Returns the request method.
62
     */
63
    public function getMethod(): string
64
    {
65
        return $this->method;
1✔
66
    }
67

68
    /**
69
     * Return an instance with the provided HTTP method.
70
     * @param string $method Case-sensitive method.
71
     * @return static
72
     * @throws \InvalidArgumentException for invalid HTTP methods.
73
     */
74
    public function withMethod(string $method): self
75
    {
UNCOV
76
        $new = clone $this;
×
UNCOV
77
        $new->method = $method;
×
UNCOV
78
        return $new;
×
79
    }
80

81
    /**
82
     * Retrieves the URI instance.
83
     * This method MUST return a UriInterface instance.
84
     * @return UriInterface Returns a UriInterface instance representing the URI of the request.
85
     */
86
    public function getUri(): UriInterface
87
    {
UNCOV
88
        return $this->uri;
×
89
    }
90

91
    /**
92
     * Returns an instance with the provided URI.
93
     * @param UriInterface $uri New request URI to use.
94
     * @param bool $preserveHost Preserve the original state of the Host header.
95
     * @return static
96
     */
97
    public function withUri(UriInterface $uri, bool $preserveHost = false): self
98
    {
UNCOV
99
        $new = clone $this;
×
UNCOV
100
        $new->uri = $uri instanceof Uri ? $uri : new Uri((string)$uri);
×
UNCOV
101
        if (!$preserveHost || !$new->hasHeader('host')) {
×
UNCOV
102
            if (isset($new->headers['host'])) {
×
UNCOV
103
                unset($new->headers['host']);
×
104
            }
UNCOV
105
            $new->headers = array_merge(['host' => ['Host' => [$this->formatHostHeader($uri)]]], $new->headers);
×
106
        }
UNCOV
107
        return $new;
×
108
    }
109

110
    public function __toString(): string
111
    {
UNCOV
112
        return $this->stringable('%s %s', $this->getMethod(), $this->getUri());
×
113
    }
114

115
    public function getAsArray(): array
116
    {
117
        return array_merge([
1✔
118
            "{$this->getMethod()} {$this->getRequestTarget()} HTTP/{$this->getProtocolVersion()}",
1✔
119
        ], parent::getAsArray());
1✔
120
    }
121

122
    private function formatHostHeader(Uri $uri): string
123
    {
124
        $host = $uri->getHost();
1✔
125
        $port = $uri->getPort();
1✔
126
        return $host && $port ? "{$host}:{$port}" : $host;
1✔
127
    }
128
}
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