Skip to content

Commit e16d960

Browse files
author
Marek Fojtl
committed
Add type hints to methods in AbstractRequest, BufferedBody, Response, ServerRequest, UploadedFile, and Uri classes
1 parent 176d830 commit e16d960

File tree

6 files changed

+56
-74
lines changed

6 files changed

+56
-74
lines changed

src/Io/AbstractRequest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function __construct(
7171
$this->uri = $uri;
7272
}
7373

74-
public function getRequestTarget()
74+
public function getRequestTarget(): string
7575
{
7676
if ($this->requestTarget !== null) {
7777
return $this->requestTarget;
@@ -88,7 +88,7 @@ public function getRequestTarget()
8888
return $target;
8989
}
9090

91-
public function withRequestTarget($requestTarget)
91+
public function withRequestTarget(string $requestTarget): RequestInterface
9292
{
9393
if ((string) $requestTarget === $this->requestTarget) {
9494
return $this;
@@ -100,12 +100,12 @@ public function withRequestTarget($requestTarget)
100100
return $request;
101101
}
102102

103-
public function getMethod()
103+
public function getMethod(): string
104104
{
105105
return $this->method;
106106
}
107107

108-
public function withMethod($method)
108+
public function withMethod(string $method): RequestInterface
109109
{
110110
if ((string) $method === $this->method) {
111111
return $this;
@@ -117,12 +117,12 @@ public function withMethod($method)
117117
return $request;
118118
}
119119

120-
public function getUri()
120+
public function getUri(): UriInterface
121121
{
122122
return $this->uri;
123123
}
124124

125-
public function withUri(UriInterface $uri, $preserveHost = false)
125+
public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface
126126
{
127127
if ($uri === $this->uri) {
128128
return $this;

src/Io/BufferedBody.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($buffer)
2323
$this->buffer = $buffer;
2424
}
2525

26-
public function __toString()
26+
public function __toString(): string
2727
{
2828
if ($this->closed) {
2929
return '';
@@ -34,7 +34,7 @@ public function __toString()
3434
return $this->getContents();
3535
}
3636

37-
public function close()
37+
public function close(): void
3838
{
3939
$this->buffer = '';
4040
$this->position = 0;
@@ -48,12 +48,12 @@ public function detach()
4848
return null;
4949
}
5050

51-
public function getSize()
51+
public function getSize(): ?int
5252
{
5353
return $this->closed ? null : \strlen($this->buffer);
5454
}
5555

56-
public function tell()
56+
public function tell(): int
5757
{
5858
if ($this->closed) {
5959
throw new \RuntimeException('Unable to tell position of closed stream');
@@ -62,17 +62,17 @@ public function tell()
6262
return $this->position;
6363
}
6464

65-
public function eof()
65+
public function eof(): bool
6666
{
6767
return $this->position >= \strlen($this->buffer);
6868
}
6969

70-
public function isSeekable()
70+
public function isSeekable(): bool
7171
{
7272
return !$this->closed;
7373
}
7474

75-
public function seek($offset, $whence = \SEEK_SET)
75+
public function seek(int $offset, int $whence = \SEEK_SET): void
7676
{
7777
if ($this->closed) {
7878
throw new \RuntimeException('Unable to seek on closed stream');
@@ -96,17 +96,17 @@ public function seek($offset, $whence = \SEEK_SET)
9696
}
9797
}
9898

99-
public function rewind()
99+
public function rewind(): void
100100
{
101101
$this->seek(0);
102102
}
103103

104-
public function isWritable()
104+
public function isWritable(): bool
105105
{
106106
return !$this->closed;
107107
}
108108

109-
public function write($string)
109+
public function write(string $string): int
110110
{
111111
if ($this->closed) {
112112
throw new \RuntimeException('Unable to write to closed stream');
@@ -127,12 +127,12 @@ public function write($string)
127127
return $len;
128128
}
129129

130-
public function isReadable()
130+
public function isReadable(): bool
131131
{
132132
return !$this->closed;
133133
}
134134

135-
public function read($length)
135+
public function read(int $length): string
136136
{
137137
if ($this->closed) {
138138
throw new \RuntimeException('Unable to read from closed stream');
@@ -156,7 +156,7 @@ public function read($length)
156156
return \substr($this->buffer, $pos, $length);
157157
}
158158

159-
public function getContents()
159+
public function getContents(): string
160160
{
161161
if ($this->closed) {
162162
throw new \RuntimeException('Unable to read from closed stream');
@@ -172,7 +172,7 @@ public function getContents()
172172
return \substr($this->buffer, $pos);
173173
}
174174

175-
public function getMetadata($key = null)
175+
public function getMetadata(?string $key = null)
176176
{
177177
return $key === null ? [] : null;
178178
}

src/Io/UploadedFile.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ public function __construct(StreamInterface $stream, $size, $error, $filename, $
7676
$this->mediaType = $mediaType;
7777
}
7878

79-
/**
80-
* {@inheritdoc}
81-
*/
82-
public function getStream()
79+
public function getStream(): StreamInterface
8380
{
8481
if ($this->error !== \UPLOAD_ERR_OK) {
8582
throw new RuntimeException('Cannot retrieve stream due to upload error');
@@ -88,42 +85,27 @@ public function getStream()
8885
return $this->stream;
8986
}
9087

91-
/**
92-
* {@inheritdoc}
93-
*/
94-
public function moveTo($targetPath)
88+
public function moveTo(string $targetPath): void
9589
{
9690
throw new RuntimeException('Not implemented');
9791
}
9892

99-
/**
100-
* {@inheritdoc}
101-
*/
102-
public function getSize()
93+
public function getSize(): ?int
10394
{
10495
return $this->size;
10596
}
10697

107-
/**
108-
* {@inheritdoc}
109-
*/
110-
public function getError()
98+
public function getError(): int
11199
{
112100
return $this->error;
113101
}
114102

115-
/**
116-
* {@inheritdoc}
117-
*/
118-
public function getClientFilename()
103+
public function getClientFilename(): ?string
119104
{
120105
return $this->filename;
121106
}
122107

123-
/**
124-
* {@inheritdoc}
125-
*/
126-
public function getClientMediaType()
108+
public function getClientMediaType(): ?string
127109
{
128110
return $this->mediaType;
129111
}

src/Message/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ public function __construct(
319319
$this->reasonPhrase = ($reason !== '' && $reason !== null) ? (string) $reason : self::getReasonPhraseForStatusCode($status);
320320
}
321321

322-
public function getStatusCode()
322+
public function getStatusCode(): int
323323
{
324324
return $this->statusCode;
325325
}
326326

327-
public function withStatus($code, $reasonPhrase = '')
327+
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
328328
{
329329
if ((string) $reasonPhrase === '') {
330330
$reasonPhrase = self::getReasonPhraseForStatusCode($code);
@@ -341,7 +341,7 @@ public function withStatus($code, $reasonPhrase = '')
341341
return $response;
342342
}
343343

344-
public function getReasonPhrase()
344+
public function getReasonPhrase(): string
345345
{
346346
return $this->reasonPhrase;
347347
}

src/Message/ServerRequest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,41 @@ public function __construct(
8787
}
8888
}
8989

90-
public function getServerParams()
90+
public function getServerParams(): array
9191
{
9292
return $this->serverParams;
9393
}
9494

95-
public function getCookieParams()
95+
public function getCookieParams(): array
9696
{
9797
return $this->cookies;
9898
}
9999

100-
public function withCookieParams(array $cookies)
100+
public function withCookieParams(array $cookies): ServerRequestInterface
101101
{
102102
$new = clone $this;
103103
$new->cookies = $cookies;
104104
return $new;
105105
}
106106

107-
public function getQueryParams()
107+
public function getQueryParams(): array
108108
{
109109
return $this->queryParams;
110110
}
111111

112-
public function withQueryParams(array $query)
112+
public function withQueryParams(array $query): ServerRequestInterface
113113
{
114114
$new = clone $this;
115115
$new->queryParams = $query;
116116
return $new;
117117
}
118118

119-
public function getUploadedFiles()
119+
public function getUploadedFiles(): array
120120
{
121121
return $this->fileParams;
122122
}
123123

124-
public function withUploadedFiles(array $uploadedFiles)
124+
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
125125
{
126126
$new = clone $this;
127127
$new->fileParams = $uploadedFiles;
@@ -133,34 +133,34 @@ public function getParsedBody()
133133
return $this->parsedBody;
134134
}
135135

136-
public function withParsedBody($data)
136+
public function withParsedBody($data): ServerRequestInterface
137137
{
138138
$new = clone $this;
139139
$new->parsedBody = $data;
140140
return $new;
141141
}
142142

143-
public function getAttributes()
143+
public function getAttributes(): array
144144
{
145145
return $this->attributes;
146146
}
147147

148-
public function getAttribute($name, $default = null)
148+
public function getAttribute(string $name, $default = null)
149149
{
150150
if (!\array_key_exists($name, $this->attributes)) {
151151
return $default;
152152
}
153153
return $this->attributes[$name];
154154
}
155155

156-
public function withAttribute($name, $value)
156+
public function withAttribute(string $name, $value): ServerRequestInterface
157157
{
158158
$new = clone $this;
159159
$new->attributes[$name] = $value;
160160
return $new;
161161
}
162162

163-
public function withoutAttribute($name)
163+
public function withoutAttribute(string $name): ServerRequestInterface
164164
{
165165
$new = clone $this;
166166
unset($new->attributes[$name]);

0 commit comments

Comments
 (0)