Skip to content

Commit 87b2033

Browse files
committed
fix(code-style): used rector to cleanup and update code
1 parent f5842e2 commit 87b2033

13 files changed

Lines changed: 76 additions & 72 deletions

File tree

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
->append(
1818
[
1919
__FILE__,
20+
__DIR__ . '/rector.php',
2021
]
2122
)
2223
;

config/routes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
return new TextResponse($content, 200, [
4747
'Content-Type' => $contentType,
4848
]);
49-
} catch (Throwable $e) {
50-
return new TextResponse('Error loading spec: ' . $e->getMessage(), 500);
49+
} catch (Throwable $throwable) {
50+
return new TextResponse('Error loading spec: ' . $throwable->getMessage(), 500);
5151
}
5252
};
5353

@@ -94,7 +94,7 @@
9494
<script>
9595
window.onload = () => {
9696
window.ui = SwaggerUIBundle({
97-
url: '$specUrl',
97+
url: '{$specUrl}',
9898
dom_id: '#swagger-ui',
9999
deepLinking: true,
100100
presets: [

rector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
__DIR__ . '/public',
1313
__DIR__ . '/src',
1414
__DIR__ . '/tests',
15+
__DIR__ . '/bin',
1516
])
1617
// Target PHP version
1718
->withPhpVersion(PhpVersion::PHP_83)
@@ -21,8 +22,9 @@
2122
codeQuality: true,
2223
typeDeclarations: true,
2324
privatization: true,
24-
earlyReturn: true,
2525
naming: true,
26+
instanceOf: true,
27+
earlyReturn: true,
2628
)
2729
->withSets([
2830
SetList::CODING_STYLE,
@@ -37,6 +39,7 @@
3739
->withPHPStanConfigs([
3840
__DIR__ . '/phpstan.neon',
3941
__DIR__ . '/vendor/slam/phpstan-laminas-framework/extension.neon',
42+
__DIR__ . '/vendor/phpstan/phpstan-webmozart-assert/extension.neon',
4043
])
4144
// Parallel processing
4245
->withParallel(

src/Middleware/MockMiddleware/Faker/SchemaFaker/BooleanFaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function generateStatic(Schema $schema): ?bool
5252
/** @var array<bool> $enums */
5353
$enums = $schema->enum;
5454

55-
return (bool) reset($enums);
55+
return reset($enums);
5656
}
5757

5858
return true;

src/Middleware/MockMiddleware/Faker/SchemaFaker/FakerContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
final readonly class FakerContext
99
{
1010
public const string REQUEST = 'request';
11+
1112
public const string RESPONSE = 'response';
1213

1314
/**

src/Middleware/MockMiddleware/Faker/SchemaFaker/NumberFaker.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ private function generateStaticFromFormat(Schema $schema): int|float
105105

106106
switch ($schema->format) {
107107
case 'int32':
108-
return (int) NumberUtils::ensureRange(0, $minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, $multipleOf);
109-
110108
case 'int64':
111109
return (int) NumberUtils::ensureRange(0, $minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, $multipleOf);
112110

src/Middleware/MockMiddleware/Faker/SchemaFaker/StringFaker.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
use WebProject\PhpOpenApiMockServer\Middleware\MockMiddleware\Faker\Options;
1414

1515
/** @internal */
16-
final class StringFaker implements FakerInterface
16+
final readonly class StringFaker implements FakerInterface
1717
{
18-
private Generator $faker;
18+
private Generator $generator;
1919

2020
public function __construct()
2121
{
22-
$this->faker = Factory::create();
22+
$this->generator = Factory::create();
2323
}
2424

2525
public function generate(Schema $schema, Options $options, FakerRegistry $fakerRegistry, FakerContext $fakerContext): ?string
@@ -35,27 +35,27 @@ private function generateDynamic(Schema $schema): string
3535
{
3636
if (!empty($schema->enum)) {
3737
/** @var string $value */
38-
$value = $this->faker->randomElement($schema->enum);
38+
$value = $this->generator->randomElement($schema->enum);
3939

4040
return $value;
4141
}
4242

4343
if (null !== $schema->pattern) {
44-
return $this->faker->regexify($schema->pattern);
44+
return $this->generator->regexify($schema->pattern);
4545
}
4646

4747
$maxLength = $schema->maxLength ?? 255;
4848

4949
return match ($schema->format) {
50-
'date' => $this->faker->date(),
51-
'date-time' => $this->faker->iso8601(),
52-
'email' => $this->faker->email(),
53-
'uuid' => $this->faker->uuid(),
54-
'uri' => $this->faker->url(),
55-
'hostname' => $this->faker->domainName(),
56-
'ipv4' => $this->faker->ipv4(),
57-
'ipv6' => $this->faker->ipv6(),
58-
default => $this->faker->text($maxLength > 5 ? $maxLength : 20),
50+
'date' => $this->generator->date(),
51+
'date-time' => $this->generator->iso8601(),
52+
'email' => $this->generator->email(),
53+
'uuid' => $this->generator->uuid(),
54+
'uri' => $this->generator->url(),
55+
'hostname' => $this->generator->domainName(),
56+
'ipv4' => $this->generator->ipv4(),
57+
'ipv6' => $this->generator->ipv6(),
58+
default => $this->generator->text($maxLength > 5 ? $maxLength : 20),
5959
};
6060
}
6161

src/Middleware/MockMiddleware/OpenApiMockMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9191
}
9292

9393
return $response;
94-
} catch (Throwable $exception) {
94+
} catch (Throwable $throwable) {
9595
return $this->requestHandler->handleInvalidRequest(
96-
$exception,
96+
$throwable,
9797
isset($requestValidatorResult) ? $requestValidatorResult->getSchema() : null,
9898
isset($requestValidatorResult) ? $requestValidatorResult->getOperationAddress() : null,
9999
$acceptedContentTypes,

src/Middleware/MockMiddleware/Request/RequestHandler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,27 @@ public function handleValidRequest(
6262
* @throws Throwable
6363
*/
6464
public function handleInvalidRequest(
65-
Throwable $exception,
65+
Throwable $throwable,
6666
?OpenApi $openApi,
6767
?OperationAddress $operationAddress,
6868
array $acceptedContentTypes,
6969
array $pathParameters = []
7070
): ResponseInterface {
7171
$errorContentType = $acceptedContentTypes[0] ?? 'application/json';
7272

73-
if (null === $openApi || null === $operationAddress) {
74-
return $this->responseFaker->handleException(ValidationException::forViolations($exception), $errorContentType);
73+
if (!$openApi instanceof OpenApi || !$operationAddress instanceof OperationAddress) {
74+
return $this->responseFaker->handleException(ValidationException::forViolations($throwable), $errorContentType);
7575
}
7676

7777
return match (true) {
78-
$exception instanceof NoPath,
79-
$exception instanceof FakerNoPath => $this->handleNoPathMatchedRequest($exception, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
78+
$throwable instanceof NoPath,
79+
$throwable instanceof FakerNoPath => $this->handleNoPathMatchedRequest($throwable, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
8080

81-
$exception instanceof InvalidSecurity => $this->handleInvalidSecurityRequest($exception, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
81+
$throwable instanceof InvalidSecurity => $this->handleInvalidSecurityRequest($throwable, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
8282

83-
$exception instanceof ValidationFailed => $this->handleValidationFailedRequest($exception, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
83+
$throwable instanceof ValidationFailed => $this->handleValidationFailedRequest($throwable, $openApi, $operationAddress, $acceptedContentTypes, $pathParameters),
8484

85-
default => $this->responseFaker->handleException(ValidationException::forViolations($exception), $errorContentType),
85+
default => $this->responseFaker->handleException(ValidationException::forViolations($throwable), $errorContentType),
8686
};
8787
}
8888

src/Middleware/MockMiddleware/Response/ResponseFaker.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ private function negotiateContentType(
148148
return $acceptedContentTypes[0] ?? 'application/json';
149149
}
150150

151-
foreach ($acceptedContentTypes as $acceptedType) {
152-
if ('*/*' === $acceptedType) {
151+
foreach ($acceptedContentTypes as $acceptedContentType) {
152+
if ('*/*' === $acceptedContentType) {
153153
return $availableTypes[0];
154154
}
155155

156-
if (in_array($acceptedType, $availableTypes, true)) {
157-
return $acceptedType;
156+
if (in_array($acceptedContentType, $availableTypes, true)) {
157+
return $acceptedContentType;
158158
}
159159
}
160160

0 commit comments

Comments
 (0)