2323
2424class Guzzle extends AbstractBrowser
2525{
26+ /**
27+ * @var array<string, mixed>
28+ */
2629 protected array $ requestOptions = [
2730 'allow_redirects ' => false ,
2831 'headers ' => [],
@@ -115,7 +118,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
115118 $ contentType = 'text/html ' ;
116119 }
117120
118- if (strpos ($ contentType , 'charset= ' ) === false ) {
121+ if (str_contains ($ contentType , 'charset= ' ) === false ) {
119122 if (preg_match ('#<meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)#i ' , $ body , $ matches )) {
120123 $ contentType .= ';charset= ' . $ matches [1 ];
121124 }
@@ -159,10 +162,10 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
159162 protected function getAbsoluteUri (string $ uri ): string
160163 {
161164 $ baseUri = $ this ->client ->getConfig ('base_uri ' );
162- if (strpos ($ uri , ':// ' ) === false && strpos ($ uri , '// ' ) !== 0 ) {
163- if (strpos ($ uri , '/ ' ) === 0 ) {
165+ if (str_contains ($ uri , ':// ' ) === false && ! str_starts_with ($ uri , '// ' )) {
166+ if (str_starts_with ($ uri , '/ ' )) {
164167 $ baseUriPath = $ baseUri ->getPath ();
165- if (!empty ($ baseUriPath ) && strpos ($ uri , (string ) $ baseUriPath ) === 0 ) {
168+ if (!empty ($ baseUriPath ) && str_starts_with ($ uri , (string ) $ baseUriPath )) {
166169 $ uri = substr ($ uri , strlen ($ baseUriPath ));
167170 }
168171
@@ -178,9 +181,9 @@ protected function getAbsoluteUri(string $uri): string
178181 return Uri::mergeUrls ((string )$ baseUri , $ uri );
179182 }
180183
181- protected function doRequest ($ request )
184+ protected function doRequest (object $ request )
182185 {
183- /** @var $request BrowserKitRequest **/
186+ /** @var BrowserKitRequest $request **/
184187 $ guzzleRequest = new Psr7Request (
185188 $ request ->getMethod (),
186189 $ request ->getUri (),
@@ -190,17 +193,17 @@ protected function doRequest($request)
190193 $ options = $ this ->requestOptions ;
191194 $ options ['cookies ' ] = $ this ->extractCookies ($ guzzleRequest ->getUri ()->getHost ());
192195 $ multipartData = $ this ->extractMultipartFormData ($ request );
193- if (! empty ( $ multipartData) ) {
196+ if ($ multipartData !== [] ) {
194197 $ options ['multipart ' ] = $ multipartData ;
195198 }
196199
197200 $ formData = $ this ->extractFormData ($ request );
198- if (empty ( $ multipartData) && $ formData ) {
201+ if ($ multipartData === [] && $ formData ) {
199202 $ options ['form_params ' ] = $ formData ;
200203 }
201204
202205 try {
203- if (null !== $ this ->awsCredentials ) {
206+ if ($ this ->awsCredentials instanceof AwsCredentials ) {
204207 $ response = $ this ->client ->send ($ this ->awsSignature ->signRequest ($ guzzleRequest , $ this ->awsCredentials ), $ options );
205208 } else {
206209 $ response = $ this ->client ->send ($ guzzleRequest , $ options );
@@ -213,6 +216,7 @@ protected function doRequest($request)
213216 $ response = $ exception ->getResponse ();
214217 }
215218
219+ // @phpstan-ignore-next-line
216220 return $ this ->createResponse ($ response );
217221 }
218222
@@ -227,7 +231,7 @@ protected function extractHeaders(BrowserKitRequest $request): array
227231 $ contentHeaders = ['Content-Length ' => true , 'Content-Md5 ' => true , 'Content-Type ' => true ];
228232 foreach ($ server as $ header => $ val ) {
229233 $ header = html_entity_decode (implode ('- ' , array_map ('ucfirst ' , explode ('- ' , strtolower (str_replace ('_ ' , '- ' , $ header ))))), ENT_NOQUOTES );
230- if (strpos ($ header , 'Http- ' ) === 0 ) {
234+ if (str_starts_with ($ header , 'Http- ' )) {
231235 $ headers [substr ($ header , 5 )] = $ val ;
232236 } elseif (isset ($ contentHeaders [$ header ])) {
233237 $ headers [$ header ] = $ val ;
@@ -237,6 +241,9 @@ protected function extractHeaders(BrowserKitRequest $request): array
237241 return $ headers ;
238242 }
239243
244+ /**
245+ * @return array<int, mixed>|null
246+ */
240247 protected function extractFormData (BrowserKitRequest $ browserKitRequest ): ?array
241248 {
242249 if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
@@ -257,14 +264,17 @@ protected function extractFormData(BrowserKitRequest $browserKitRequest): ?array
257264 return $ browserKitRequest ->getParameters ();
258265 }
259266
260- protected function extractMultipartFormData (BrowserKitRequest $ browserKitRequest )
267+ /**
268+ * @return array<string, mixed>
269+ */
270+ protected function extractMultipartFormData (BrowserKitRequest $ browserKitRequest ): array
261271 {
262272 if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' ])) {
263273 return [];
264274 }
265275
266276 $ parts = $ this ->mapFiles ($ browserKitRequest ->getFiles ());
267- if (empty ( $ parts) ) {
277+ if ($ parts === [] ) {
268278 return [];
269279 }
270280
@@ -275,11 +285,14 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275285 return $ parts ;
276286 }
277287
278- protected function formatMultipart ($ parts , $ key , $ value )
288+ /**
289+ * @return array<string, mixed>
290+ */
291+ protected function formatMultipart (mixed $ parts , string $ key , mixed $ value ): array
279292 {
280293 if (is_array ($ value )) {
281294 foreach ($ value as $ subKey => $ subValue ) {
282- $ parts = array_merge ($ this ->formatMultipart ([], $ key. sprintf ('[%s] ' , $ subKey ), $ subValue ), $ parts );
295+ $ parts = array_merge ($ this ->formatMultipart ([], $ key . sprintf ('[%s] ' , $ subKey ), $ subValue ), $ parts );
283296 }
284297
285298 return $ parts ;
@@ -289,7 +302,11 @@ protected function formatMultipart($parts, $key, $value)
289302 return $ parts ;
290303 }
291304
292- protected function mapFiles ($ requestFiles , $ arrayName = '' ): array
305+ /**
306+ * @param array<int, mixed> $requestFiles
307+ * @return array<int, mixed>
308+ */
309+ protected function mapFiles (array $ requestFiles , ?string $ arrayName = '' ): array
293310 {
294311 $ files = [];
295312 foreach ($ requestFiles as $ name => $ info ) {
@@ -329,7 +346,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
329346 return $ files ;
330347 }
331348
332- protected function extractCookies ($ host ): GuzzleCookieJar
349+ protected function extractCookies (string $ host ): GuzzleCookieJar
333350 {
334351 $ jar = [];
335352 $ cookies = $ this ->getCookieJar ()->all ();
@@ -345,7 +362,7 @@ protected function extractCookies($host): GuzzleCookieJar
345362 return new GuzzleCookieJar (false , $ jar );
346363 }
347364
348- public static function createHandler ($ handler ): GuzzleHandlerStack
365+ public static function createHandler (mixed $ handler ): GuzzleHandlerStack
349366 {
350367 if ($ handler instanceof GuzzleHandlerStack) {
351368 return $ handler ;
@@ -360,7 +377,7 @@ public static function createHandler($handler): GuzzleHandlerStack
360377 }
361378
362379 if (is_string ($ handler ) && class_exists ($ handler )) {
363- return GuzzleHandlerStack::create (new $ handler );
380+ return GuzzleHandlerStack::create (new $ handler() );
364381 }
365382
366383 if (is_callable ($ handler )) {
@@ -370,7 +387,10 @@ public static function createHandler($handler): GuzzleHandlerStack
370387 return GuzzleHandlerStack::create ();
371388 }
372389
373- public function setAwsAuth ($ config ): void
390+ /**
391+ * @param array<string, mixed> $config
392+ */
393+ public function setAwsAuth (array $ config ): void
374394 {
375395 $ this ->awsCredentials = new AwsCredentials ($ config ['key ' ], $ config ['secret ' ]);
376396 $ this ->awsSignature = new AwsSignatureV4 ($ config ['service ' ], $ config ['region ' ]);
0 commit comments