11<?php
2+
3+ declare (strict_types=1 );
4+
25namespace Codeception \Lib \Connector ;
36
47use Aws \Credentials \Credentials ;
1417use GuzzleHttp \Psr7 \Request as Psr7Request ;
1518use GuzzleHttp \Psr7 \Response as Psr7Response ;
1619use GuzzleHttp \Psr7 \Uri as Psr7Uri ;
17- use Symfony \Component \BrowserKit \AbstractBrowser as Client ;
20+ use Symfony \Component \BrowserKit \AbstractBrowser ;
1821use Symfony \Component \BrowserKit \Request as BrowserKitRequest ;
1922use Symfony \Component \BrowserKit \Response as BrowserKitResponse ;
2023
21- class Guzzle extends Client
24+ class Guzzle extends AbstractBrowser
2225{
26+ /**
27+ * @var array
28+ */
2329 protected $ requestOptions = [
2430 'allow_redirects ' => false ,
2531 'headers ' => [],
2632 ];
33+
34+ /**
35+ * @var int
36+ */
2737 protected $ refreshMaxInterval = 0 ;
2838
39+ /**
40+ * @var \Aws\Credentials\Credentials|null
41+ */
2942 protected $ awsCredentials ;
43+
44+ /**
45+ * @var \Aws\Signature\SignatureV4|null
46+ */
3047 protected $ awsSignature ;
3148
32- /** @var GuzzleClient */
49+ /**
50+ * @var GuzzleClient
51+ */
3352 protected $ client ;
3453
3554 /**
@@ -43,14 +62,14 @@ class Guzzle extends Client
4362 *
4463 * @param int $seconds Number of seconds
4564 */
46- public function setRefreshMaxInterval ($ seconds )
65+ public function setRefreshMaxInterval (int $ seconds ): void
4766 {
4867 $ this ->refreshMaxInterval = $ seconds ;
4968 }
5069
51- public function setClient (GuzzleClient $ client )
70+ public function setClient (GuzzleClient $ guzzleClient ): void
5271 {
53- $ this ->client = $ client ;
72+ $ this ->client = $ guzzleClient ;
5473 }
5574
5675 /**
@@ -63,9 +82,9 @@ public function setClient(GuzzleClient $client)
6382 * @param string $name the name of the header
6483 * @param string $value the value of the header
6584 */
66- public function setHeader ($ name , $ value )
85+ public function setHeader (string $ name , string $ value ): void
6786 {
68- if (( string ) $ value === '' ) {
87+ if ($ value === '' ) {
6988 $ this ->deleteHeader ($ name );
7089 } else {
7190 $ this ->requestOptions ['headers ' ][$ name ] = $ value ;
@@ -78,19 +97,14 @@ public function setHeader($name, $value)
7897 *
7998 * @param string $name the name of the header to delete.
8099 */
81- public function deleteHeader ($ name )
100+ public function deleteHeader (string $ name ): void
82101 {
83102 unset($ this ->requestOptions ['headers ' ][$ name ]);
84103 }
85104
86- /**
87- * @param string $username
88- * @param string $password
89- * @param string $type Default: 'basic'
90- */
91- public function setAuth ($ username , $ password , $ type = 'basic ' )
105+ public function setAuth (string $ username , string $ password , string $ type = 'basic ' ): void
92106 {
93- if (! $ username ) {
107+ if ($ username === '' ) {
94108 unset($ this ->requestOptions ['auth ' ]);
95109 return ;
96110 }
@@ -100,14 +114,12 @@ public function setAuth($username, $password, $type = 'basic')
100114 /**
101115 * Taken from Mink\BrowserKitDriver
102116 *
103- * @param Psr7Response $response
104- *
105117 * @return BrowserKitResponse
106118 */
107- protected function createResponse (Psr7Response $ response )
119+ protected function createResponse (Psr7Response $ psr7Response ): BrowserKitResponse
108120 {
109- $ body = (string ) $ response ->getBody ();
110- $ headers = $ response ->getHeaders ();
121+ $ body = (string ) $ psr7Response ->getBody ();
122+ $ headers = $ psr7Response ->getHeaders ();
111123
112124 $ contentType = null ;
113125
@@ -119,26 +131,26 @@ protected function createResponse(Psr7Response $response)
119131 }
120132
121133 if (strpos ($ contentType , 'charset= ' ) === false ) {
122- if (preg_match ('/ <meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)/ i ' , $ body , $ matches )) {
134+ if (preg_match ('# <meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)# i ' , $ body , $ matches )) {
123135 $ contentType .= ';charset= ' . $ matches [1 ];
124136 }
125137 $ headers ['Content-Type ' ] = [$ contentType ];
126138 }
127139
128- $ status = $ response ->getStatusCode ();
140+ $ status = $ psr7Response ->getStatusCode ();
129141 if ($ status < 300 || $ status >= 400 ) {
130142 $ matches = [];
131143
132144 $ matchesMeta = preg_match (
133- '/ <meta[^>]+http-equiv="refresh" content="\s*(\d*)\s*;\s*url=(.*?)"/ i ' ,
145+ '# <meta[^>]+http-equiv="refresh" content="\s*(\d*)\s*;\s*url=(.*?)"# i ' ,
134146 $ body ,
135147 $ matches
136148 );
137149
138150 if (!$ matchesMeta && isset ($ headers ['Refresh ' ])) {
139151 // match by header
140152 preg_match (
141- '/ ^\s*(\d*)\s*;\s*url=(.*)/ i ' ,
153+ '# ^\s*(\d*)\s*;\s*url=(.*)# i ' ,
142154 (string ) reset ($ headers ['Refresh ' ]),
143155 $ matches
144156 );
@@ -150,15 +162,15 @@ protected function createResponse(Psr7Response $response)
150162
151163 if ($ uri ->withFragment ('' ) !== $ currentUri ->withFragment ('' )) {
152164 $ status = 302 ;
153- $ headers ['Location ' ] = $ matchesMeta ? htmlspecialchars_decode ($ uri ) : (string )$ uri ;
165+ $ headers ['Location ' ] = $ matchesMeta ? htmlspecialchars_decode (( string ) $ uri ) : (string )$ uri ;
154166 }
155167 }
156168 }
157169
158170 return new BrowserKitResponse ($ body , $ status , $ headers );
159171 }
160172
161- public function getAbsoluteUri ($ uri )
173+ protected function getAbsoluteUri ($ uri )
162174 {
163175 $ baseUri = $ this ->client ->getConfig ('base_uri ' );
164176 if (strpos ($ uri , ':// ' ) === false && strpos ($ uri , '// ' ) !== 0 ) {
@@ -172,7 +184,7 @@ public function getAbsoluteUri($uri)
172184 }
173185 // relative url
174186 if (!$ this ->getHistory ()->isEmpty ()) {
175- return Uri::mergeUrls (( string ) $ this ->getHistory ()->current ()->getUri (), $ uri );
187+ return Uri::mergeUrls ($ this ->getHistory ()->current ()->getUri (), $ uri );
176188 }
177189 }
178190 return Uri::mergeUrls ($ baseUri , $ uri );
@@ -214,7 +226,7 @@ protected function doRequest($request)
214226 return $ this ->createResponse ($ response );
215227 }
216228
217- protected function extractHeaders (BrowserKitRequest $ request )
229+ protected function extractHeaders (BrowserKitRequest $ request ): array
218230 {
219231 $ headers = [];
220232 $ server = $ request ->getServer ();
@@ -231,39 +243,37 @@ protected function extractHeaders(BrowserKitRequest $request)
231243 return $ headers ;
232244 }
233245
234- protected function extractFormData (BrowserKitRequest $ request )
246+ protected function extractFormData (BrowserKitRequest $ browserKitRequest ): ? array
235247 {
236- if (!in_array (strtoupper ($ request ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
248+ if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
237249 return null ;
238250 }
239251
240252 // guessing if it is a form data
241- $ headers = $ request ->getServer ();
242- if (isset ($ headers ['HTTP_CONTENT_TYPE ' ])) {
243- // not a form
244- if ($ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
245- return null ;
246- }
253+ $ headers = $ browserKitRequest ->getServer ();
254+ // not a form
255+ if (isset ($ headers ['HTTP_CONTENT_TYPE ' ]) && $ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
256+ return null ;
247257 }
248- if ($ request ->getContent () !== null ) {
258+ if ($ browserKitRequest ->getContent () !== null ) {
249259 return null ;
250260 }
251- return $ request ->getParameters ();
261+ return $ browserKitRequest ->getParameters ();
252262 }
253263
254- protected function extractMultipartFormData (BrowserKitRequest $ request )
264+ protected function extractMultipartFormData (BrowserKitRequest $ browserKitRequest )
255265 {
256- if (!in_array (strtoupper ($ request ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' ])) {
266+ if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' ])) {
257267 return [];
258268 }
259269
260- $ parts = $ this ->mapFiles ($ request ->getFiles ());
270+ $ parts = $ this ->mapFiles ($ browserKitRequest ->getFiles ());
261271 if (empty ($ parts )) {
262272 return [];
263273 }
264274
265- foreach ($ request ->getParameters () as $ k => $ v ) {
266- $ parts = $ this ->formatMultipart ($ parts , $ k , $ v );
275+ foreach ($ browserKitRequest ->getParameters () as $ k => $ parameter ) {
276+ $ parts = $ this ->formatMultipart ($ parts , $ k , $ parameter );
267277 }
268278 return $ parts ;
269279 }
@@ -272,15 +282,15 @@ protected function formatMultipart($parts, $key, $value)
272282 {
273283 if (is_array ($ value )) {
274284 foreach ($ value as $ subKey => $ subValue ) {
275- $ parts = array_merge ($ this ->formatMultipart ([], $ key ." [ $ subKey] " , $ subValue ), $ parts );
285+ $ parts = array_merge ($ this ->formatMultipart ([], $ key .sprintf ( ' [%s] ' , $ subKey) , $ subValue ), $ parts );
276286 }
277287 return $ parts ;
278288 }
279289 $ parts [] = ['name ' => $ key , 'contents ' => (string ) $ value ];
280290 return $ parts ;
281291 }
282292
283- protected function mapFiles ($ requestFiles , $ arrayName = '' )
293+ protected function mapFiles ($ requestFiles , $ arrayName = '' ): array
284294 {
285295 $ files = [];
286296 foreach ($ requestFiles as $ name => $ info ) {
@@ -292,7 +302,7 @@ protected function mapFiles($requestFiles, $arrayName = '')
292302 if (isset ($ info ['tmp_name ' ])) {
293303 if ($ info ['tmp_name ' ]) {
294304 $ handle = fopen ($ info ['tmp_name ' ], 'rb ' );
295- $ filename = isset ( $ info ['name ' ]) ? $ info [ ' name ' ] : null ;
305+ $ filename = $ info ['name ' ] ?? null ;
296306 $ file = [
297307 'name ' => $ name ,
298308 'contents ' => $ handle ,
@@ -319,7 +329,7 @@ protected function mapFiles($requestFiles, $arrayName = '')
319329 return $ files ;
320330 }
321331
322- protected function extractCookies ($ host )
332+ protected function extractCookies ($ host ): \ GuzzleHttp \ Cookie \ CookieJar
323333 {
324334 $ jar = [];
325335 $ cookies = $ this ->getCookieJar ()->all ();
@@ -333,7 +343,7 @@ protected function extractCookies($host)
333343 return new CookieJar (false , $ jar );
334344 }
335345
336- public static function createHandler ($ handler )
346+ public static function createHandler ($ handler ): \ GuzzleHttp \ HandlerStack
337347 {
338348 if ($ handler instanceof HandlerStack) {
339349 return $ handler ;
@@ -353,7 +363,7 @@ public static function createHandler($handler)
353363 return HandlerStack::create ();
354364 }
355365
356- public function setAwsAuth ($ config )
366+ public function setAwsAuth ($ config ): void
357367 {
358368 $ this ->awsCredentials = new Credentials ($ config ['key ' ], $ config ['secret ' ]);
359369 $ this ->awsSignature = new SignatureV4 ($ config ['service ' ], $ config ['region ' ]);
0 commit comments