Skip to content

Commit 74711a2

Browse files
Merge pull request #235 from SpencerMalone/context-docblocking
Add docblocks for \Twirp\Context array throughout codebase
2 parents 60a4c53 + edc43d9 commit 74711a2

7 files changed

Lines changed: 88 additions & 317 deletions

File tree

.php-cs-fixer.dist.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// until PHP 8.0
1515
'trailing_comma_in_multiline' => false,
1616
'native_function_invocation' => false,
17+
'phpdoc_to_comment' => [
18+
'allow_before_return_statement' => true,
19+
]
1720
])
1821
->setFinder(
1922
PhpCsFixer\Finder::create()

lib/src/Context.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ final class Context
1919
/**
2020
* Extracts the name of the method being handled in the given
2121
* context. If it is not known, it returns null.
22+
*
23+
* @param array<string, mixed> $ctx
2224
*/
2325
public static function methodName(array $ctx): ?string
2426
{
25-
if (isset($ctx[self::METHOD_NAME])) {
27+
if (isset($ctx[self::METHOD_NAME]) && is_string($ctx[self::METHOD_NAME])) {
2628
return $ctx[self::METHOD_NAME];
2729
}
2830

@@ -31,6 +33,10 @@ public static function methodName(array $ctx): ?string
3133

3234
/**
3335
* Sets the method name in the context.
36+
*
37+
* @param array<string, mixed> $ctx
38+
*
39+
* @return array<string, mixed>
3440
*/
3541
public static function withMethodName(array $ctx, string $name): array
3642
{
@@ -42,10 +48,12 @@ public static function withMethodName(array $ctx, string $name): array
4248
/**
4349
* Extracts the name of the service handling the given context. If
4450
* it is not known, it returns null.
51+
*
52+
* @param array<string, mixed> $ctx
4553
*/
4654
public static function serviceName(array $ctx): ?string
4755
{
48-
if (isset($ctx[self::SERVICE_NAME])) {
56+
if (isset($ctx[self::SERVICE_NAME]) && is_string($ctx[self::SERVICE_NAME])) {
4957
return $ctx[self::SERVICE_NAME];
5058
}
5159

@@ -54,6 +62,10 @@ public static function serviceName(array $ctx): ?string
5462

5563
/**
5664
* Sets the service name in the context.
65+
*
66+
* @param array<string, mixed> $ctx
67+
*
68+
* @return array<string, mixed>
5769
*/
5870
public static function withServiceName(array $ctx, string $name): array
5971
{
@@ -70,10 +82,12 @@ public static function withServiceName(array $ctx, string $name): array
7082
*
7183
* Note that the protobuf package name can be very different than the go package
7284
* name; the two are unrelated.
85+
*
86+
* @param array<string, mixed> $ctx
7387
*/
7488
public static function packageName(array $ctx): ?string
7589
{
76-
if (isset($ctx[self::PACKAGE_NAME])) {
90+
if (isset($ctx[self::PACKAGE_NAME]) && is_string($ctx[self::PACKAGE_NAME])) {
7791
return $ctx[self::PACKAGE_NAME];
7892
}
7993

@@ -82,6 +96,10 @@ public static function packageName(array $ctx): ?string
8296

8397
/**
8498
* Sets the package name in the context.
99+
*
100+
* @param array<string, mixed> $ctx
101+
*
102+
* @return array<string, mixed>
85103
*/
86104
public static function withPackageName(array $ctx, string $name): array
87105
{
@@ -94,10 +112,12 @@ public static function withPackageName(array $ctx, string $name): array
94112
* Retrieves the status code of the response (as string like "200").
95113
* If it is known returns the status.
96114
* If it is not known, it returns null.
115+
*
116+
* @param array<string, mixed> $ctx
97117
*/
98118
public static function statusCode(array $ctx): ?int
99119
{
100-
if (isset($ctx[self::STATUS_CODE])) {
120+
if (isset($ctx[self::STATUS_CODE]) && is_int($ctx[self::STATUS_CODE])) {
101121
return $ctx[self::STATUS_CODE];
102122
}
103123

@@ -106,6 +126,10 @@ public static function statusCode(array $ctx): ?int
106126

107127
/**
108128
* Sets the status code in the context.
129+
*
130+
* @param array<string, mixed> $ctx
131+
*
132+
* @return array<string, mixed>
109133
*/
110134
public static function withStatusCode(array $ctx, int $code): array
111135
{
@@ -117,10 +141,15 @@ public static function withStatusCode(array $ctx, int $code): array
117141
/**
118142
* Retrieves the HTTP headers sent as part of the request.
119143
* If there are no headers, it returns an empty array.
144+
*
145+
* @param array<string, mixed> $ctx
146+
*
147+
* @return array<string, string>
120148
*/
121149
public static function httpRequestHeaders(array $ctx): array
122150
{
123151
if (isset($ctx[self::REQUEST_HEADER])) {
152+
/** @var array{self::REQUEST_HEADER:array<string, string>} $ctx */
124153
return $ctx[self::REQUEST_HEADER];
125154
}
126155

@@ -140,6 +169,11 @@ public static function httpRequestHeaders(array $ctx): array
140169
* Throws an exception if the provided headers
141170
* would overwrite a header that is needed by Twirp, like "Content-Type".
142171
*
172+
* @param array<string, mixed> $ctx
173+
* @param array<string, string> $headers
174+
*
175+
* @return array<string, mixed>
176+
*
143177
* @throws \InvalidArgumentException when any of the following headers are included: Accept, Content-Type, Twirp-Version
144178
*/
145179
public static function withHttpRequestHeaders(array $ctx, array $headers): array
@@ -178,6 +212,10 @@ public static function withHttpRequestHeaders(array $ctx, array $headers): array
178212
* Throws an exception if the provided headers
179213
* would overwrite a header that is needed by Twirp, like "Content-Type".
180214
*
215+
* @param array<string, mixed> $ctx
216+
*
217+
* @return array<string, mixed>
218+
*
181219
* @throws \InvalidArgumentException when any of the following headers are included: Content-Type
182220
*/
183221
public static function withHttpResponseHeader(array $ctx, string $key, string $value): array
@@ -186,6 +224,10 @@ public static function withHttpResponseHeader(array $ctx, string $key, string $v
186224
throw new \InvalidArgumentException('header key can not be Content-Type');
187225
}
188226

227+
if (!isset($ctx[self::RESPONSE_HEADER]) || !is_array($ctx[self::RESPONSE_HEADER])) {
228+
$ctx[self::RESPONSE_HEADER] = [];
229+
}
230+
189231
$ctx[self::RESPONSE_HEADER][$key] = $value;
190232

191233
return $ctx;

lib/src/ServerHooks.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ interface ServerHooks
2828
* Called as soon as a request enters the Twirp
2929
* server at the earliest available moment.
3030
*
31+
* @param array<string, mixed> $ctx
32+
*
33+
* @return array<string, mixed>
34+
*
3135
* @throws \Throwable
3236
*/
3337
public function requestReceived(array $ctx): array;
@@ -36,6 +40,10 @@ public function requestReceived(array $ctx): array;
3640
* Called when a request has been routed to a
3741
* particular method of the Twirp server.
3842
*
43+
* @param array<string, mixed> $ctx
44+
*
45+
* @return array<string, mixed>
46+
*
3947
* @throws \Throwable
4048
*/
4149
public function requestRouted(array $ctx): array;
@@ -44,6 +52,10 @@ public function requestRouted(array $ctx): array;
4452
* Called when a request has been handled and a
4553
* response is ready to be sent to the client.
4654
*
55+
* @param array<string, mixed> $ctx
56+
*
57+
* @return array<string, mixed>
58+
*
4759
* @throws \Throwable
4860
*/
4961
public function responsePrepared(array $ctx): array;
@@ -54,12 +66,18 @@ public function responsePrepared(array $ctx): array;
5466
* does not return a context.
5567
*
5668
* For the same reason, it MUST NOT throw any exceptions.
69+
*
70+
* @param array<string, mixed> $ctx
5771
*/
5872
public function responseSent(array $ctx): void;
5973

6074
/**
6175
* Called when an error occurs while handling a request. The
6276
* Error is passed as argument to the hook.
77+
*
78+
* @param array<string, mixed> $ctx
79+
*
80+
* @return array<string, mixed>
6381
*/
6482
public function error(array $ctx, \Throwable $error): array;
6583
}

0 commit comments

Comments
 (0)