@@ -79,6 +79,9 @@ public function __construct(array $config)
79
79
* Called when the middleware is handled.
80
80
*
81
81
* @return \Closure
82
+ *
83
+ * @throws \InvalidArgumentException
84
+ * @throws \RuntimeException
82
85
*/
83
86
public function __invoke (callable $ handler )
84
87
{
@@ -91,9 +94,13 @@ public function __invoke(callable $handler)
91
94
};
92
95
}
93
96
94
- private function onBefore (RequestInterface $ request )
97
+ /**
98
+ * @throws \InvalidArgumentException
99
+ * @throws \RuntimeException
100
+ */
101
+ private function onBefore (RequestInterface $ request ): RequestInterface
95
102
{
96
- $ oauthparams = $ this -> getOauthParams (
103
+ $ oauthparams = self :: getOauthParams (
97
104
$ this ->generateNonce ($ request ),
98
105
$ this ->config
99
106
);
@@ -127,11 +134,9 @@ private function onBefore(RequestInterface $request)
127
134
* @param RequestInterface $request Request to generate a signature for
128
135
* @param array $params Oauth parameters.
129
136
*
130
- * @return string
131
- *
132
137
* @throws \RuntimeException
133
138
*/
134
- public function getSignature (RequestInterface $ request , array $ params )
139
+ public function getSignature (RequestInterface $ request , array $ params ): string
135
140
{
136
141
// Remove oauth_signature if present
137
142
// Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
@@ -149,7 +154,7 @@ public function getSignature(RequestInterface $request, array $params)
149
154
150
155
$ baseString = $ this ->createBaseString (
151
156
$ request ,
152
- $ this -> prepareParameters ($ params )
157
+ self :: prepareParameters ($ params )
153
158
);
154
159
155
160
// Implements double-dispatch to sign requests
@@ -181,10 +186,8 @@ public function getSignature(RequestInterface $request, array $params)
181
186
* timestamp to use separate nonce's.
182
187
*
183
188
* @param RequestInterface $request Request to generate a nonce for
184
- *
185
- * @return string
186
189
*/
187
- public function generateNonce (RequestInterface $ request )
190
+ private static function generateNonce (RequestInterface $ request ): string
188
191
{
189
192
return sha1 (uniqid ('' , true ).$ request ->getUri ()->getHost ().$ request ->getUri ()->getPath ());
190
193
}
@@ -199,29 +202,20 @@ public function generateNonce(RequestInterface $request)
199
202
* @param RequestInterface $request Request being signed
200
203
* @param array $params Associative array of OAuth parameters
201
204
*
202
- * @return string Returns the base string
203
- *
204
205
* @see https://oauth.net/core/1.0/#sig_base_example
205
206
*/
206
- protected function createBaseString (RequestInterface $ request , array $ params )
207
+ protected function createBaseString (RequestInterface $ request , array $ params ): string
207
208
{
208
209
// Remove query params from URL. Ref: Spec: 9.1.2.
209
- $ url = $ request ->getUri ()->withQuery ('' );
210
- $ query = http_build_query ($ params , '' , '& ' , PHP_QUERY_RFC3986 );
211
-
212
210
return strtoupper ($ request ->getMethod ())
213
- .'& ' .rawurlencode ((string ) $ url )
214
- .'& ' .rawurlencode ($ query );
211
+ .'& ' .rawurlencode ((string ) $ request -> getUri ()-> withQuery ( '' ) )
212
+ .'& ' .rawurlencode (Query:: build ( $ params ) );
215
213
}
216
214
217
215
/**
218
- * Convert booleans to strings, removed unset parameters, and sorts the array
219
- *
220
- * @param array $data Data array
221
- *
222
- * @return array
216
+ * @param array $data The data array
223
217
*/
224
- private function prepareParameters (array $ data )
218
+ private static function prepareParameters (array $ data ): array
225
219
{
226
220
// Parameters are sorted by name, using lexicographical byte value
227
221
// ordering. Ref: Spec: 9.1.1 (1).
@@ -238,10 +232,8 @@ private function prepareParameters(array $data)
238
232
239
233
/**
240
234
* @param string $algo Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)
241
- *
242
- * @return string
243
235
*/
244
- private function signUsingHmac (string $ algo , string $ baseString )
236
+ private function signUsingHmac (string $ algo , string $ baseString ): string
245
237
{
246
238
$ key = rawurlencode ($ this ->config ['consumer_secret ' ]).'& ' ;
247
239
if (isset ($ this ->config ['token_secret ' ])) {
@@ -252,13 +244,12 @@ private function signUsingHmac(string $algo, string $baseString)
252
244
}
253
245
254
246
/**
255
- * @return string
247
+ * @throws RuntimeException
256
248
*/
257
- private function signUsingRsaSha1 (string $ baseString )
249
+ private function signUsingRsaSha1 (string $ baseString ): string
258
250
{
259
251
if (!function_exists ('openssl_pkey_get_private ' )) {
260
- throw new \RuntimeException ('RSA-SHA1 signature method '
261
- .'requires the OpenSSL extension. ' );
252
+ throw new \RuntimeException ('RSA-SHA1 signature method requires the OpenSSL extension. ' );
262
253
}
263
254
264
255
$ privateKey = openssl_pkey_get_private (
@@ -285,10 +276,8 @@ private function signUsingPlaintext(string $baseString)
285
276
* Builds the Authorization header for a request
286
277
*
287
278
* @param array $params Associative array of authorization parameters.
288
- *
289
- * @return array
290
279
*/
291
- private function buildAuthorizationHeader (array $ params )
280
+ private function buildAuthorizationHeader (array $ params ): array
292
281
{
293
282
foreach ($ params as $ key => $ value ) {
294
283
$ params [$ key ] = $ key .'=" ' .rawurlencode ((string ) $ value ).'" ' ;
@@ -309,10 +298,8 @@ private function buildAuthorizationHeader(array $params)
309
298
*
310
299
* @param string $nonce Unique nonce
311
300
* @param array $config Configuration options of the plugin.
312
- *
313
- * @return array
314
301
*/
315
- private function getOauthParams (string $ nonce , array $ config )
302
+ private static function getOauthParams (string $ nonce , array $ config ): array
316
303
{
317
304
$ params = [
318
305
'oauth_consumer_key ' => $ config ['consumer_key ' ],
0 commit comments