• 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

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

18
/**
19
 * WebSocket\Http\Request class.
20
 * Only used for handshake procedure.
21
 */
22
class Request extends Message implements RequestInterface
23
{
24
    private static array $methods = ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'PUT', 'DELETE', 'POST', 'PATCH', 'CONNECT'];
25

26
    private string $target = '';
27
    private string $method;
28
    private Uri $uri;
29

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

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

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

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

71
    /**
72
     * Return an instance with the provided HTTP method.
73
     * @param string $method Case-sensitive method.
74
     * @return static
75
     * @throws InvalidArgumentException for invalid HTTP methods.
76
     */
77
    public function withMethod(string $method): self
78
    {
79
        if (!in_array($method, self::$methods)) {
2✔
NEW
UNCOV
80
            throw new InvalidArgumentException("Invalid method '{$method}' provided.");
×
81
        }
82
        $new = clone $this;
2✔
83
        $new->method = $method;
2✔
84
        return $new;
2✔
85
    }
86

87
    /**
88
     * Retrieves the URI instance.
89
     * This method MUST return a UriInterface instance.
90
     * @return UriInterface Returns a UriInterface instance representing the URI of the request.
91
     */
92
    public function getUri(): UriInterface
93
    {
94
        return $this->uri;
20✔
95
    }
96

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

116
    public function __toString(): string
117
    {
118
        return $this->stringable('%s %s', $this->getMethod(), $this->getUri());
3✔
119
    }
120

121
    public function getAsArray(): array
122
    {
123
        return array_merge([
52✔
124
            "{$this->getMethod()} {$this->getRequestTarget()} HTTP/{$this->getProtocolVersion()}",
52✔
125
        ], parent::getAsArray());
52✔
126
    }
127

128
    private function formatHostHeader(UriInterface $uri): string
129
    {
130
        $host = $uri->getHost();
105✔
131
        $port = $uri->getPort();
105✔
132
        return $host && $port ? "{$host}:{$port}" : $host;
105✔
133
    }
134
}
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