Skip to content

Commit 9f82b04

Browse files
committed
Updated code quality for phpstan to level 6
1 parent ac90f35 commit 9f82b04

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

Diff for: phpstan.neon

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
parameters:
22
paths:
33
- ./src
4-
- ./tests/unit
54
# The level 9 is the highest level (with check for mixed type)
6-
level: 8
5+
level: 6

Diff for: rector.php

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
return RectorConfig::configure()
1717
->withPaths([
1818
__DIR__.'/src',
19-
__DIR__.'/tests/unit',
2019
])
2120
// uncomment to reach your current PHP version
2221
->withPhpSets()

Diff for: src/Codeception/Lib/Connector/Guzzle.php

+30-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
class Guzzle extends AbstractBrowser
2525
{
26+
/**
27+
* @var array<string, mixed>
28+
*/
2629
protected array $requestOptions = [
2730
'allow_redirects' => false,
2831
'headers' => [],
@@ -159,7 +162,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
159162
protected function getAbsoluteUri(string $uri): string
160163
{
161164
$baseUri = $this->client->getConfig('base_uri');
162-
if ((str_contains($uri, '://') === 0 || str_contains($uri, '://') === false) && !str_starts_with($uri, '//')) {
165+
if (str_contains($uri, '://') === false && !str_starts_with($uri, '//')) {
163166
if (str_starts_with($uri, '/')) {
164167
$baseUriPath = $baseUri->getPath();
165168
if (!empty($baseUriPath) && str_starts_with($uri, (string) $baseUriPath)) {
@@ -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): BrowserKitResponse
182185
{
183-
/** @var $request BrowserKitRequest **/
186+
/** @var BrowserKitRequest $request **/
184187
$guzzleRequest = new Psr7Request(
185188
$request->getMethod(),
186189
$request->getUri(),
@@ -190,12 +193,12 @@ 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

@@ -213,6 +216,7 @@ protected function doRequest($request)
213216
$response = $requestException->getResponse();
214217
}
215218

219+
// @phpstan-ignore-next-line
216220
return $this->createResponse($response);
217221
}
218222

@@ -237,6 +241,9 @@ protected function extractHeaders(BrowserKitRequest $browserKitRequest): 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,7 +264,10 @@ 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 [];
@@ -275,7 +285,10 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275285
return $parts;
276286
}
277287

278-
protected function formatMultipart($parts, string $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) {
@@ -289,7 +302,11 @@ protected function formatMultipart($parts, string $key, $value)
289302
return $parts;
290303
}
291304

292-
protected function mapFiles($requestFiles, ?string $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, ?string $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;
@@ -370,6 +387,9 @@ public static function createHandler($handler): GuzzleHandlerStack
370387
return GuzzleHandlerStack::create();
371388
}
372389

390+
/**
391+
* @param array<string, mixed> $config
392+
*/
373393
public function setAwsAuth(array $config): void
374394
{
375395
$this->awsCredentials = new AwsCredentials($config['key'], $config['secret']);

Diff for: src/Codeception/Module/PhpBrowser.php

+24-16
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ public function _initialize(): void
132132

133133
public function _before(TestInterface $test): void
134134
{
135-
if (!$this->client instanceof \Symfony\Component\BrowserKit\AbstractBrowser) {
135+
if (!$this->client instanceof AbstractBrowser) {
136136
$this->client = new Guzzle();
137137
}
138138

139139
$this->_prepareSession();
140140
}
141141

142-
public function _getUrl()
142+
public function _getUrl(): string
143143
{
144144
return $this->config['url'];
145145
}
@@ -152,12 +152,14 @@ public function setHeader(string $name, string $value): void
152152
$this->haveHttpHeader($name, $value);
153153
}
154154

155-
public function amHttpAuthenticated($username, $password): void
155+
public function amHttpAuthenticated(string $username, string $password): void
156156
{
157-
$this->client->setAuth($username, $password);
157+
if ($this->client instanceof Guzzle) {
158+
$this->client->setAuth($username, $password);
159+
}
158160
}
159161

160-
public function amOnUrl($url): void
162+
public function amOnUrl(string $url): void
161163
{
162164
$host = Uri::retrieveHost($url);
163165
$config = $this->config;
@@ -172,7 +174,7 @@ public function amOnUrl($url): void
172174
$this->amOnPage($page);
173175
}
174176

175-
public function amOnSubdomain($subdomain): void
177+
public function amOnSubdomain(string $subdomain): void
176178
{
177179
$url = $this->config['url'];
178180
$url = preg_replace('#(https?://)(.*\.)(.*\.)#', "$1$3", $url); // removing current subdomain
@@ -183,7 +185,7 @@ public function amOnSubdomain($subdomain): void
183185
$this->_reconfigure($config);
184186
}
185187

186-
protected function onReconfigure()
188+
protected function onReconfigure(): void
187189
{
188190
$this->_prepareSession();
189191
}
@@ -203,18 +205,13 @@ protected function onReconfigure()
203205
*
204206
* It is not recommended to use this command on a regular basis.
205207
* If Codeception lacks important Guzzle Client methods, implement them and submit patches.
206-
*
207-
* @return mixed
208208
*/
209-
public function executeInGuzzle(Closure $function)
209+
public function executeInGuzzle(Closure $function): mixed
210210
{
211211
return $function($this->guzzle);
212212
}
213213

214-
/**
215-
* @return int|string
216-
*/
217-
public function _getResponseCode()
214+
public function _getResponseCode(): int|string
218215
{
219216
return $this->getResponseStatusCode();
220217
}
@@ -252,10 +249,15 @@ public function _prepareSession(): void
252249
$defaults['handler'] = $handlerStack;
253250
$this->guzzle = new GuzzleClient($defaults);
254251

255-
$this->client->setRefreshMaxInterval($this->config['refresh_max_interval']);
256-
$this->client->setClient($this->guzzle);
252+
if ($this->client instanceof Guzzle) {
253+
$this->client->setRefreshMaxInterval($this->config['refresh_max_interval']);
254+
$this->client->setClient($this->guzzle);
255+
}
257256
}
258257

258+
/**
259+
* @return array<string, mixed>
260+
*/
259261
public function _backupSession(): array
260262
{
261263
return [
@@ -266,13 +268,19 @@ public function _backupSession(): array
266268
];
267269
}
268270

271+
/**
272+
* @param array<string, mixed> $session
273+
*/
269274
public function _loadSession($session): void
270275
{
271276
foreach ($session as $key => $val) {
272277
$this->$key = $val;
273278
}
274279
}
275280

281+
/**
282+
* @param ?array<string, mixed> $session
283+
*/
276284
public function _closeSession($session = null): void
277285
{
278286
unset($session);

0 commit comments

Comments
 (0)