Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"require-dev": {
"nyholm/psr7": "^1.0",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18",
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);
};
4 changes: 1 addition & 3 deletions src/BadRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
final class BadRequestHandler implements BadRequestHandlerInterface
{
private ResponseFactoryInterface $responseFactory;
private ?ParserException $parserException = null;

public function __construct(ResponseFactoryInterface $responseFactory)
public function __construct(private ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down
14 changes: 2 additions & 12 deletions src/Parser/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,13 @@
*/
final class JsonParser implements ParserInterface
{
private bool $convertToAssociativeArray;
private int $depth;
private int $options;

/**
* @param bool $convertToAssociativeArray Whether objects should be converted to associative array during parsing.
* @param int $depth Maximum JSON recursion depth.
* @param int $options JSON decoding options. {@see json_decode()}.
*/
public function __construct(
bool $convertToAssociativeArray = true,
int $depth = 512,
int $options = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE
) {
$this->convertToAssociativeArray = $convertToAssociativeArray;
$this->depth = $depth;
$this->options = $options;
public function __construct(private bool $convertToAssociativeArray = true, private int $depth = 512, private int $options = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE)
{
}

public function parse(string $rawBody)
Expand Down
15 changes: 3 additions & 12 deletions src/RequestBodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Yiisoft\Http\Header;
use Yiisoft\Request\Body\Parser\JsonParser;
use function array_key_exists;
use function get_class;
use function is_array;
use function is_object;

Expand All @@ -28,7 +27,6 @@
*/
final class RequestBodyParser implements MiddlewareInterface
{
private ContainerInterface $container;
private BadRequestHandlerInterface $badRequestHandler;

/**
Expand All @@ -42,10 +40,9 @@ final class RequestBodyParser implements MiddlewareInterface

public function __construct(
ResponseFactoryInterface $responseFactory,
ContainerInterface $container,
private ContainerInterface $container,
BadRequestHandlerInterface $badRequestHandler = null
) {
$this->container = $container;
$this->badRequestHandler = $badRequestHandler ?? new BadRequestHandler($responseFactory);
}

Expand All @@ -54,8 +51,6 @@ public function __construct(
*
* @param string $mimeType Mime type to register parser for.
* @param string $parserClass Parser fully qualified name.
*
* @return self
*/
public function withParser(string $mimeType, string $parserClass): self
{
Expand All @@ -77,8 +72,6 @@ public function withParser(string $mimeType, string $parserClass): self
* Returns new instance with parsers un-registered for mime types specified.
*
* @param string ...$mimeTypes Mime types to unregister parsers for.
*
* @return self
*/
public function withoutParsers(string ...$mimeTypes): self
{
Expand All @@ -96,8 +89,6 @@ public function withoutParsers(string ...$mimeTypes): self

/**
* Makes the middleware to simple skip requests it cannot parse.
*
* @return self
*/
public function ignoreBadRequestBody(): self
{
Expand All @@ -114,7 +105,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/** @var mixed $parsed */
$parsed = $parser->parse((string)$request->getBody());
if ($parsed !== null && !is_object($parsed) && !is_array($parsed)) {
$parserClass = get_class($parser);
$parserClass = $parser::class;
throw new RuntimeException(
"$parserClass::parse() return value must be an array, an object, or null."
);
Expand Down Expand Up @@ -161,7 +152,7 @@ private function getContentType(ServerRequestInterface $request): ?string
*/
private function validateMimeType(string $mimeType): void
{
if (strpos($mimeType, '/') === false) {
if (!str_contains($mimeType, '/')) {
throw new InvalidArgumentException('Invalid mime type.');
}
}
Expand Down
7 changes: 1 addition & 6 deletions tests/MockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

final class MockParser implements ParserInterface
{
private $response;
private bool $throwException;

public function __construct($response, bool $throwException)
public function __construct(private $response, private bool $throwException)
{
$this->response = $response;
$this->throwException = $throwException;
}

public function parse(string $rawBody)
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testWithoutAssoc(): void
$object->test = 'value';

$parser = new JsonParser(false);
$parsed = $parser->parse(json_encode($object));
$parsed = $parser->parse(json_encode($object, JSON_THROW_ON_ERROR));

$this->assertEquals($object, $parsed);
}
Expand Down
15 changes: 3 additions & 12 deletions tests/RequestBodyParsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ private function getContainerWithResponseFactory(): SimpleContainer
{
return new SimpleContainer(
[
ResponseFactoryInterface::class => static function () {
return new Psr17Factory();
},
ResponseFactoryInterface::class => static fn () => new Psr17Factory(),
JsonParser::class => new JsonParser(),
]
);
Expand Down Expand Up @@ -195,11 +193,9 @@ private function createHandler(): BadRequestHandlerInterface

return new class ($mockResponse) implements BadRequestHandlerInterface {
private $requestParsedBody;
private ResponseInterface $mockResponse;

public function __construct(ResponseInterface $mockResponse)
public function __construct(private ResponseInterface $mockResponse)
{
$this->mockResponse = $mockResponse;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down Expand Up @@ -239,13 +235,8 @@ private function getRequestBodyParser(
private function createCustomBadResponseHandler(string $body): BadRequestHandlerInterface
{
return new class ($body, new Psr17Factory()) implements BadRequestHandlerInterface {
private string $body;
private ResponseFactoryInterface $responseFactory;

public function __construct(string $body, ResponseFactoryInterface $responseFactory)
public function __construct(private string $body, private ResponseFactoryInterface $responseFactory)
{
$this->body = $body;
$this->responseFactory = $responseFactory;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down