Skip to content

Commit

Permalink
Add native types where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jul 25, 2023
1 parent 28a732c commit 4fd4323
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Factory/HttpFoundationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function __construct(int $responseBufferMaxLength = 16372)

/**
* {@inheritdoc}
*
* @return Request
*/
public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false)
{
Expand Down Expand Up @@ -121,6 +123,8 @@ protected function getTemporaryPath()

/**
* {@inheritdoc}
*
* @return Response
*/
public function createResponse(ResponseInterface $psrResponse, bool $streamed = false)
{
Expand Down
14 changes: 8 additions & 6 deletions Factory/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +47,8 @@ public function __construct(ServerRequestFactoryInterface $serverRequestFactory,

/**
* {@inheritdoc}
*
* @return ServerRequestInterface
*/
public function createRequest(Request $symfonyRequest)
{
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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(
Expand All @@ -126,6 +126,8 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile)

/**
* {@inheritdoc}
*
* @return ResponseInterface
*/
public function createResponse(Response $symfonyResponse)
{
Expand Down
2 changes: 1 addition & 1 deletion Factory/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Tests/EventListener/PsrResponseListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Factory/HttpFoundationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion Tests/Factory/PsrHttpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*/
class PsrHttpFactoryTest extends TestCase
{
/** @var HttpMessageFactoryInterface */
private $factory;

/** @var string */
private $tmpDir;

protected function buildHttpMessageFactory(): HttpMessageFactoryInterface
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion Tests/Fixtures/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions Tests/Functional/CovertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testConvertRequestMultipleTimes($request, $firstFactory, $second
}
}

public function requestProvider()
public static function requestProvider(): array
{
$sfRequest = new Request(
[
Expand All @@ -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',
Expand Down Expand Up @@ -195,7 +195,7 @@ public function testConvertResponseMultipleTimes($response, $firstFactory, $seco
}
}

public function responseProvider()
public static function responseProvider(): array
{
$sfResponse = new Response(
'Response content.',
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4fd4323

Please sign in to comment.