@@ -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 ;
0 commit comments