Skip to content

Commit ac90f35

Browse files
committed
Added more code quality tools
1 parent 82613e7 commit ac90f35

24 files changed

+732
-570
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ jobs:
2626
- name: Install dependencies
2727
run: composer install --prefer-dist --no-progress --no-interaction --no-suggest
2828

29+
- name: Execute Code Sniffer
30+
run: vendor/bin/phpcs
31+
32+
- name: Execute PHP Stan
33+
run: vendor/bin/phpstan
34+
35+
- name: Execute Rector
36+
run: vendor/bin/rector --dry-run
37+
2938
- name: Run test suite
3039
run: |
3140
php -S 127.0.0.1:8000 -t tests/data/app >/dev/null 2>&1 &

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
},
2828
"require-dev": {
2929
"ext-curl": "*",
30+
"squizlabs/php_codesniffer": "^3.10",
31+
"phpstan/phpstan": "^1.10",
32+
"rector/rector": "^1.0",
3033
"aws/aws-sdk-php": "^3.199",
3134
"codeception/module-rest": "^2.0 || *@dev"
3235
},
@@ -37,7 +40,6 @@
3740
"suggest": {
3841
"codeception/phpbuiltinserver": "Start and stop PHP built-in web server for your tests"
3942
},
40-
"minimum-stability": "dev",
4143
"autoload": {
4244
"classmap": [
4345
"src/"

phpcs.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>The coding standard.</description>
4+
5+
<config name="testVersion" value="8.0-"/>
6+
7+
<file>src</file>
8+
9+
<arg name="basepath" value="./"/>
10+
<arg name="colors"/>
11+
<arg name="parallel" value="5"/>
12+
<arg value="np"/>
13+
14+
<!-- Don't hide tokenizer exceptions -->
15+
<rule ref="Internal.Tokenizer.Exception">
16+
<type>error</type>
17+
</rule>
18+
19+
<!-- Include the whole PSR12 standard -->
20+
<rule ref="PSR12"/>
21+
22+
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
23+
<type>warning</type>
24+
</rule>
25+
</ruleset>

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
paths:
3+
- ./src
4+
- ./tests/unit
5+
# The level 9 is the highest level (with check for mixed type)
6+
level: 8

rector.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
7+
use Rector\Visibility\Rector\ClassConst\ChangeConstantVisibilityRector;
8+
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
9+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
10+
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
11+
use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
12+
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
13+
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
14+
use Rector\PHPUnit\Set\PHPUnitSetList;
15+
16+
return RectorConfig::configure()
17+
->withPaths([
18+
__DIR__.'/src',
19+
__DIR__.'/tests/unit',
20+
])
21+
// uncomment to reach your current PHP version
22+
->withPhpSets()
23+
->withRules([
24+
AddVoidReturnTypeWhereNoReturnRector::class,
25+
ChangeConstantVisibilityRector::class,
26+
RenameForeachValueVariableToMatchExprVariableRector::class,
27+
ReturnTypeFromReturnNewRector::class,
28+
CountArrayToEmptyArrayComparisonRector::class,
29+
StrictArraySearchRector::class,
30+
SymplifyQuoteEscapeRector::class,
31+
DeclareStrictTypesRector::class,
32+
])
33+
->withSets([
34+
PHPUnitSetList::PHPUNIT_110,
35+
])
36+
->withPhpSets()
37+
->withPHPStanConfigs(['phpstan.neon'])
38+
->withPreparedSets(
39+
deadCode: true,
40+
codeQuality: true,
41+
codingStyle: true,
42+
typeDeclarations: true,
43+
privatization: true,
44+
naming: true,
45+
instanceOf: true,
46+
earlyReturn: true,
47+
strictBooleans: true
48+
);

src/Codeception/Lib/Connector/Guzzle.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
115115
$contentType = 'text/html';
116116
}
117117

118-
if (strpos($contentType, 'charset=') === false) {
118+
if (str_contains($contentType, 'charset=') === false) {
119119
if (preg_match('#<meta[^>]+charset *= *["\']?([a-zA-Z\-0-9]+)#i', $body, $matches)) {
120120
$contentType .= ';charset=' . $matches[1];
121121
}
@@ -133,7 +133,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
133133
$matches
134134
);
135135

136-
if (!$matchesMeta && isset($headers['Refresh'])) {
136+
if (($matchesMeta === 0 || $matchesMeta === false) && isset($headers['Refresh'])) {
137137
// match by header
138138
preg_match(
139139
'#^\s*(\d*)\s*;\s*url=(.*)#i',
@@ -159,10 +159,10 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
159159
protected function getAbsoluteUri(string $uri): string
160160
{
161161
$baseUri = $this->client->getConfig('base_uri');
162-
if (strpos($uri, '://') === false && strpos($uri, '//') !== 0) {
163-
if (strpos($uri, '/') === 0) {
162+
if ((str_contains($uri, '://') === 0 || str_contains($uri, '://') === false) && !str_starts_with($uri, '//')) {
163+
if (str_starts_with($uri, '/')) {
164164
$baseUriPath = $baseUri->getPath();
165-
if (!empty($baseUriPath) && strpos($uri, (string) $baseUriPath) === 0) {
165+
if (!empty($baseUriPath) && str_starts_with($uri, (string) $baseUriPath)) {
166166
$uri = substr($uri, strlen($baseUriPath));
167167
}
168168

@@ -200,17 +200,17 @@ protected function doRequest($request)
200200
}
201201

202202
try {
203-
if (null !== $this->awsCredentials) {
203+
if ($this->awsCredentials instanceof AwsCredentials) {
204204
$response = $this->client->send($this->awsSignature->signRequest($guzzleRequest, $this->awsCredentials), $options);
205205
} else {
206206
$response = $this->client->send($guzzleRequest, $options);
207207
}
208-
} catch (RequestException $exception) {
209-
if (!$exception->hasResponse()) {
210-
throw $exception;
208+
} catch (RequestException $requestException) {
209+
if (!$requestException->hasResponse()) {
210+
throw $requestException;
211211
}
212212

213-
$response = $exception->getResponse();
213+
$response = $requestException->getResponse();
214214
}
215215

216216
return $this->createResponse($response);
@@ -219,15 +219,15 @@ protected function doRequest($request)
219219
/**
220220
* @return array<string, mixed>
221221
*/
222-
protected function extractHeaders(BrowserKitRequest $request): array
222+
protected function extractHeaders(BrowserKitRequest $browserKitRequest): array
223223
{
224224
$headers = [];
225-
$server = $request->getServer();
225+
$server = $browserKitRequest->getServer();
226226

227227
$contentHeaders = ['Content-Length' => true, 'Content-Md5' => true, 'Content-Type' => true];
228228
foreach ($server as $header => $val) {
229229
$header = html_entity_decode(implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $header))))), ENT_NOQUOTES);
230-
if (strpos($header, 'Http-') === 0) {
230+
if (str_starts_with($header, 'Http-')) {
231231
$headers[substr($header, 5)] = $val;
232232
} elseif (isset($contentHeaders[$header])) {
233233
$headers[$header] = $val;
@@ -264,7 +264,7 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
264264
}
265265

266266
$parts = $this->mapFiles($browserKitRequest->getFiles());
267-
if (empty($parts)) {
267+
if ($parts === []) {
268268
return [];
269269
}
270270

@@ -275,11 +275,11 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275275
return $parts;
276276
}
277277

278-
protected function formatMultipart($parts, $key, $value)
278+
protected function formatMultipart($parts, string $key, $value)
279279
{
280280
if (is_array($value)) {
281281
foreach ($value as $subKey => $subValue) {
282-
$parts = array_merge($this->formatMultipart([], $key.sprintf('[%s]', $subKey), $subValue), $parts);
282+
$parts = array_merge($this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue), $parts);
283283
}
284284

285285
return $parts;
@@ -289,11 +289,11 @@ protected function formatMultipart($parts, $key, $value)
289289
return $parts;
290290
}
291291

292-
protected function mapFiles($requestFiles, $arrayName = ''): array
292+
protected function mapFiles($requestFiles, ?string $arrayName = ''): array
293293
{
294294
$files = [];
295295
foreach ($requestFiles as $name => $info) {
296-
if (!empty($arrayName)) {
296+
if ($arrayName !== null && $arrayName !== '' && $arrayName !== '0') {
297297
$name = $arrayName . '[' . $name . ']';
298298
}
299299

@@ -360,7 +360,7 @@ public static function createHandler($handler): GuzzleHandlerStack
360360
}
361361

362362
if (is_string($handler) && class_exists($handler)) {
363-
return GuzzleHandlerStack::create(new $handler);
363+
return GuzzleHandlerStack::create(new $handler());
364364
}
365365

366366
if (is_callable($handler)) {
@@ -370,7 +370,7 @@ public static function createHandler($handler): GuzzleHandlerStack
370370
return GuzzleHandlerStack::create();
371371
}
372372

373-
public function setAwsAuth($config): void
373+
public function setAwsAuth(array $config): void
374374
{
375375
$this->awsCredentials = new AwsCredentials($config['key'], $config['secret']);
376376
$this->awsSignature = new AwsSignatureV4($config['service'], $config['region']);

src/Codeception/Module/PhpBrowser.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,18 @@ class PhpBrowser extends InnerBrowser implements Remote, MultiSession
121121
'connect_timeout'
122122
];
123123

124-
/**
125-
* @var Guzzle
126-
*/
127124
public ?AbstractBrowser $client = null;
128125

129126
public ?GuzzleClient $guzzle = null;
130127

131-
public function _initialize()
128+
public function _initialize(): void
132129
{
133130
$this->_initializeSession();
134131
}
135132

136-
public function _before(TestInterface $test)
133+
public function _before(TestInterface $test): void
137134
{
138-
if (!$this->client) {
135+
if (!$this->client instanceof \Symfony\Component\BrowserKit\AbstractBrowser) {
139136
$this->client = new Guzzle();
140137
}
141138

@@ -245,14 +242,14 @@ public function _prepareSession(): void
245242

246243
$defaults['base_uri'] = $this->config['url'];
247244
$defaults['curl'] = $curlOptions;
248-
$handler = Guzzle::createHandler($this->config['handler']);
249-
if ($handler && is_array($this->config['middleware'])) {
245+
$handlerStack = Guzzle::createHandler($this->config['handler']);
246+
if (is_array($this->config['middleware'])) {
250247
foreach ($this->config['middleware'] as $middleware) {
251-
$handler->push($middleware);
248+
$handlerStack->push($middleware);
252249
}
253250
}
254251

255-
$defaults['handler'] = $handler;
252+
$defaults['handler'] = $handlerStack;
256253
$this->guzzle = new GuzzleClient($defaults);
257254

258255
$this->client->setRefreshMaxInterval($this->config['refresh_max_interval']);

tests/_support/UnitTester.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
/**
54
* Inherited Methods
65
* @method void wantToTest($text)

0 commit comments

Comments
 (0)