From 4fd4323581b0fca927fc720236d049a13db41425 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 25 Jul 2023 10:20:48 +0200 Subject: [PATCH] Add native types where possible --- Factory/HttpFoundationFactory.php | 4 ++++ Factory/PsrHttpFactory.php | 14 ++++++++------ Factory/UploadedFile.php | 2 +- Tests/EventListener/PsrResponseListenerTest.php | 3 +++ Tests/Factory/HttpFoundationFactoryTest.php | 2 +- Tests/Factory/PsrHttpFactoryTest.php | 5 ++++- Tests/Fixtures/Message.php | 2 +- Tests/Fixtures/Response.php | 2 +- Tests/Fixtures/ServerRequest.php | 6 +++++- Tests/Fixtures/Stream.php | 2 +- Tests/Fixtures/UploadedFile.php | 2 +- Tests/Functional/CovertTest.php | 10 +++++----- 12 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Factory/HttpFoundationFactory.php b/Factory/HttpFoundationFactory.php index 457e346..bbe8c27 100644 --- a/Factory/HttpFoundationFactory.php +++ b/Factory/HttpFoundationFactory.php @@ -41,6 +41,8 @@ public function __construct(int $responseBufferMaxLength = 16372) /** * {@inheritdoc} + * + * @return Request */ public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false) { @@ -121,6 +123,8 @@ protected function getTemporaryPath() /** * {@inheritdoc} + * + * @return Response */ public function createResponse(ResponseInterface $psrResponse, bool $streamed = false) { diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index b1b6f9a..ed4536a 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -12,7 +12,9 @@ namespace Symfony\Bridge\PsrHttpMessage\Factory; use Psr\Http\Message\ResponseFactoryInterface; +use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestFactoryInterface; +use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UploadedFileInterface; @@ -45,6 +47,8 @@ public function __construct(ServerRequestFactoryInterface $serverRequestFactory, /** * {@inheritdoc} + * + * @return ServerRequestInterface */ public function createRequest(Request $symfonyRequest) { @@ -84,10 +88,8 @@ public function createRequest(Request $symfonyRequest) /** * Converts Symfony uploaded files array to the PSR one. - * - * @return array */ - private function getFiles(array $uploadedFiles) + private function getFiles(array $uploadedFiles): array { $files = []; @@ -108,10 +110,8 @@ private function getFiles(array $uploadedFiles) /** * Creates a PSR-7 UploadedFile instance from a Symfony one. - * - * @return UploadedFileInterface */ - private function createUploadedFile(UploadedFile $symfonyUploadedFile) + private function createUploadedFile(UploadedFile $symfonyUploadedFile): UploadedFileInterface { return $this->uploadedFileFactory->createUploadedFile( $this->streamFactory->createStreamFromFile( @@ -126,6 +126,8 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile) /** * {@inheritdoc} + * + * @return ResponseInterface */ public function createResponse(Response $symfonyResponse) { diff --git a/Factory/UploadedFile.php b/Factory/UploadedFile.php index b951045..4d38a1f 100644 --- a/Factory/UploadedFile.php +++ b/Factory/UploadedFile.php @@ -52,7 +52,7 @@ public function __construct(UploadedFileInterface $psrUploadedFile, callable $ge /** * {@inheritdoc} */ - public function move($directory, $name = null): File + public function move(string $directory, string $name = null): File { if (!$this->isValid() || $this->test) { return parent::move($directory, $name); diff --git a/Tests/EventListener/PsrResponseListenerTest.php b/Tests/EventListener/PsrResponseListenerTest.php index 3f6060c..c700cca 100644 --- a/Tests/EventListener/PsrResponseListenerTest.php +++ b/Tests/EventListener/PsrResponseListenerTest.php @@ -46,6 +46,9 @@ public function testDoesNotConvertControllerResult() self::assertFalse($event->hasResponse()); } + /** + * @param mixed $controllerResult + */ private function createEventMock($controllerResult): ViewEvent { return new ViewEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $controllerResult); diff --git a/Tests/Factory/HttpFoundationFactoryTest.php b/Tests/Factory/HttpFoundationFactoryTest.php index 3a00e2f..cc389a5 100644 --- a/Tests/Factory/HttpFoundationFactoryTest.php +++ b/Tests/Factory/HttpFoundationFactoryTest.php @@ -196,7 +196,7 @@ public function testCreateUploadedFileWithError() $symfonyUploadedFile->move($this->tmpDir, 'shouldFail.txt'); } - private function createUploadedFile($content, $error, $clientFileName, $clientMediaType): UploadedFile + private function createUploadedFile(string $content, int $error, string $clientFileName, string $clientMediaType): UploadedFile { $filePath = tempnam($this->tmpDir, uniqid()); file_put_contents($filePath, $content); diff --git a/Tests/Factory/PsrHttpFactoryTest.php b/Tests/Factory/PsrHttpFactoryTest.php index a1cd1b1..db37c15 100644 --- a/Tests/Factory/PsrHttpFactoryTest.php +++ b/Tests/Factory/PsrHttpFactoryTest.php @@ -28,7 +28,10 @@ */ class PsrHttpFactoryTest extends TestCase { + /** @var HttpMessageFactoryInterface */ private $factory; + + /** @var string */ private $tmpDir; protected function buildHttpMessageFactory(): HttpMessageFactoryInterface @@ -135,7 +138,7 @@ public function testGetContentCanBeCalledAfterRequestCreation() $this->assertSame('Content', $request->getContent()); } - private function createUploadedFile($content, $originalName, $mimeType, $error) + private function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile { $path = tempnam($this->tmpDir, uniqid()); file_put_contents($path, $content); diff --git a/Tests/Fixtures/Message.php b/Tests/Fixtures/Message.php index 2f511ae..8fc18f7 100644 --- a/Tests/Fixtures/Message.php +++ b/Tests/Fixtures/Message.php @@ -25,7 +25,7 @@ class Message implements MessageInterface private $headers = []; private $body; - public function __construct($version = '1.1', array $headers = [], StreamInterface $body = null) + public function __construct(string $version = '1.1', array $headers = [], StreamInterface $body = null) { $this->version = $version; $this->headers = $headers; diff --git a/Tests/Fixtures/Response.php b/Tests/Fixtures/Response.php index e7ec334..39a1b56 100644 --- a/Tests/Fixtures/Response.php +++ b/Tests/Fixtures/Response.php @@ -21,7 +21,7 @@ class Response extends Message implements ResponseInterface { private $statusCode; - public function __construct($version = '1.1', array $headers = [], StreamInterface $body = null, $statusCode = 200) + public function __construct(string $version = '1.1', array $headers = [], StreamInterface $body = null, int $statusCode = 200) { parent::__construct($version, $headers, $body); diff --git a/Tests/Fixtures/ServerRequest.php b/Tests/Fixtures/ServerRequest.php index f6c85c1..8cfc59f 100644 --- a/Tests/Fixtures/ServerRequest.php +++ b/Tests/Fixtures/ServerRequest.php @@ -31,7 +31,11 @@ class ServerRequest extends Message implements ServerRequestInterface private $data; private $attributes; - public function __construct($version = '1.1', array $headers = [], StreamInterface $body = null, $requestTarget = '/', $method = 'GET', $uri = null, array $server = [], array $cookies = [], array $query = [], array $uploadedFiles = [], $data = null, array $attributes = []) + /** + * @param UriInterface|string|null $uri + * @param array|object|null $data + */ + public function __construct(string $version = '1.1', array $headers = [], StreamInterface $body = null, string $requestTarget = '/', string $method = 'GET', $uri = null, array $server = [], array $cookies = [], array $query = [], array $uploadedFiles = [], $data = null, array $attributes = []) { parent::__construct($version, $headers, $body); diff --git a/Tests/Fixtures/Stream.php b/Tests/Fixtures/Stream.php index f664bae..34e243f 100644 --- a/Tests/Fixtures/Stream.php +++ b/Tests/Fixtures/Stream.php @@ -21,7 +21,7 @@ class Stream implements StreamInterface private $stringContent; private $eof = true; - public function __construct($stringContent = '') + public function __construct(string $stringContent = '') { $this->stringContent = $stringContent; } diff --git a/Tests/Fixtures/UploadedFile.php b/Tests/Fixtures/UploadedFile.php index 7b98057..92254fc 100644 --- a/Tests/Fixtures/UploadedFile.php +++ b/Tests/Fixtures/UploadedFile.php @@ -25,7 +25,7 @@ class UploadedFile implements UploadedFileInterface private $clientFileName; private $clientMediaType; - public function __construct($filePath, $size = null, $error = \UPLOAD_ERR_OK, $clientFileName = null, $clientMediaType = null) + public function __construct(string $filePath, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFileName = null, string $clientMediaType = null) { $this->filePath = $filePath; $this->size = $size; diff --git a/Tests/Functional/CovertTest.php b/Tests/Functional/CovertTest.php index cf82f82..25bbdc9 100644 --- a/Tests/Functional/CovertTest.php +++ b/Tests/Functional/CovertTest.php @@ -98,7 +98,7 @@ public function testConvertRequestMultipleTimes($request, $firstFactory, $second } } - public function requestProvider() + public static function requestProvider(): array { $sfRequest = new Request( [ @@ -120,8 +120,8 @@ public function requestProvider() 'c2' => ['c3' => 'bar'], ], [ - 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', \UPLOAD_ERR_OK), - 'foo' => ['f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', \UPLOAD_ERR_OK)], + 'f1' => self::createUploadedFile('F1', 'f1.txt', 'text/plain', \UPLOAD_ERR_OK), + 'foo' => ['f2' => self::createUploadedFile('F2', 'f2.txt', 'text/plain', \UPLOAD_ERR_OK)], ], [ 'REQUEST_METHOD' => 'POST', @@ -195,7 +195,7 @@ public function testConvertResponseMultipleTimes($response, $firstFactory, $seco } } - public function responseProvider() + public static function responseProvider(): array { $sfResponse = new Response( 'Response content.', @@ -227,7 +227,7 @@ public function responseProvider() ]; } - private function createUploadedFile($content, $originalName, $mimeType, $error) + private static function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile { $path = tempnam(sys_get_temp_dir(), uniqid()); file_put_contents($path, $content);