Skip to content

Commit 18c9e82

Browse files
committed
minor #118 Add native types where possible (derrabus)
This PR was merged into the 2.2-dev branch. Discussion ---------- Add native types where possible This PR improves the typing throughout the codebase: * Native types are added where possible without a BC break * PHPDoc``@return`` annotations have been added to prevent deprecations triggered by ErrorHandler * More PHPDoc has been added where a native type cannot be set as long as we support PHP 7.2. * PHPUnit data providers have been declared `static`. Commits ------- 4fd4323 Add native types where possible
2 parents 28a732c + 4fd4323 commit 18c9e82

12 files changed

+35
-19
lines changed

Factory/HttpFoundationFactory.php

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function __construct(int $responseBufferMaxLength = 16372)
4141

4242
/**
4343
* {@inheritdoc}
44+
*
45+
* @return Request
4446
*/
4547
public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false)
4648
{
@@ -121,6 +123,8 @@ protected function getTemporaryPath()
121123

122124
/**
123125
* {@inheritdoc}
126+
*
127+
* @return Response
124128
*/
125129
public function createResponse(ResponseInterface $psrResponse, bool $streamed = false)
126130
{

Factory/PsrHttpFactory.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Bridge\PsrHttpMessage\Factory;
1313

1414
use Psr\Http\Message\ResponseFactoryInterface;
15+
use Psr\Http\Message\ResponseInterface;
1516
use Psr\Http\Message\ServerRequestFactoryInterface;
17+
use Psr\Http\Message\ServerRequestInterface;
1618
use Psr\Http\Message\StreamFactoryInterface;
1719
use Psr\Http\Message\UploadedFileFactoryInterface;
1820
use Psr\Http\Message\UploadedFileInterface;
@@ -45,6 +47,8 @@ public function __construct(ServerRequestFactoryInterface $serverRequestFactory,
4547

4648
/**
4749
* {@inheritdoc}
50+
*
51+
* @return ServerRequestInterface
4852
*/
4953
public function createRequest(Request $symfonyRequest)
5054
{
@@ -84,10 +88,8 @@ public function createRequest(Request $symfonyRequest)
8488

8589
/**
8690
* Converts Symfony uploaded files array to the PSR one.
87-
*
88-
* @return array
8991
*/
90-
private function getFiles(array $uploadedFiles)
92+
private function getFiles(array $uploadedFiles): array
9193
{
9294
$files = [];
9395

@@ -108,10 +110,8 @@ private function getFiles(array $uploadedFiles)
108110

109111
/**
110112
* Creates a PSR-7 UploadedFile instance from a Symfony one.
111-
*
112-
* @return UploadedFileInterface
113113
*/
114-
private function createUploadedFile(UploadedFile $symfonyUploadedFile)
114+
private function createUploadedFile(UploadedFile $symfonyUploadedFile): UploadedFileInterface
115115
{
116116
return $this->uploadedFileFactory->createUploadedFile(
117117
$this->streamFactory->createStreamFromFile(
@@ -126,6 +126,8 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile)
126126

127127
/**
128128
* {@inheritdoc}
129+
*
130+
* @return ResponseInterface
129131
*/
130132
public function createResponse(Response $symfonyResponse)
131133
{

Factory/UploadedFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(UploadedFileInterface $psrUploadedFile, callable $ge
5252
/**
5353
* {@inheritdoc}
5454
*/
55-
public function move($directory, $name = null): File
55+
public function move(string $directory, string $name = null): File
5656
{
5757
if (!$this->isValid() || $this->test) {
5858
return parent::move($directory, $name);

Tests/EventListener/PsrResponseListenerTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function testDoesNotConvertControllerResult()
4646
self::assertFalse($event->hasResponse());
4747
}
4848

49+
/**
50+
* @param mixed $controllerResult
51+
*/
4952
private function createEventMock($controllerResult): ViewEvent
5053
{
5154
return new ViewEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $controllerResult);

Tests/Factory/HttpFoundationFactoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function testCreateUploadedFileWithError()
196196
$symfonyUploadedFile->move($this->tmpDir, 'shouldFail.txt');
197197
}
198198

199-
private function createUploadedFile($content, $error, $clientFileName, $clientMediaType): UploadedFile
199+
private function createUploadedFile(string $content, int $error, string $clientFileName, string $clientMediaType): UploadedFile
200200
{
201201
$filePath = tempnam($this->tmpDir, uniqid());
202202
file_put_contents($filePath, $content);

Tests/Factory/PsrHttpFactoryTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
*/
2929
class PsrHttpFactoryTest extends TestCase
3030
{
31+
/** @var HttpMessageFactoryInterface */
3132
private $factory;
33+
34+
/** @var string */
3235
private $tmpDir;
3336

3437
protected function buildHttpMessageFactory(): HttpMessageFactoryInterface
@@ -135,7 +138,7 @@ public function testGetContentCanBeCalledAfterRequestCreation()
135138
$this->assertSame('Content', $request->getContent());
136139
}
137140

138-
private function createUploadedFile($content, $originalName, $mimeType, $error)
141+
private function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile
139142
{
140143
$path = tempnam($this->tmpDir, uniqid());
141144
file_put_contents($path, $content);

Tests/Fixtures/Message.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Message implements MessageInterface
2525
private $headers = [];
2626
private $body;
2727

28-
public function __construct($version = '1.1', array $headers = [], StreamInterface $body = null)
28+
public function __construct(string $version = '1.1', array $headers = [], StreamInterface $body = null)
2929
{
3030
$this->version = $version;
3131
$this->headers = $headers;

Tests/Fixtures/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Response extends Message implements ResponseInterface
2121
{
2222
private $statusCode;
2323

24-
public function __construct($version = '1.1', array $headers = [], StreamInterface $body = null, $statusCode = 200)
24+
public function __construct(string $version = '1.1', array $headers = [], StreamInterface $body = null, int $statusCode = 200)
2525
{
2626
parent::__construct($version, $headers, $body);
2727

Tests/Fixtures/ServerRequest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class ServerRequest extends Message implements ServerRequestInterface
3131
private $data;
3232
private $attributes;
3333

34-
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 = [])
34+
/**
35+
* @param UriInterface|string|null $uri
36+
* @param array|object|null $data
37+
*/
38+
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 = [])
3539
{
3640
parent::__construct($version, $headers, $body);
3741

Tests/Fixtures/Stream.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Stream implements StreamInterface
2121
private $stringContent;
2222
private $eof = true;
2323

24-
public function __construct($stringContent = '')
24+
public function __construct(string $stringContent = '')
2525
{
2626
$this->stringContent = $stringContent;
2727
}

Tests/Fixtures/UploadedFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class UploadedFile implements UploadedFileInterface
2525
private $clientFileName;
2626
private $clientMediaType;
2727

28-
public function __construct($filePath, $size = null, $error = \UPLOAD_ERR_OK, $clientFileName = null, $clientMediaType = null)
28+
public function __construct(string $filePath, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFileName = null, string $clientMediaType = null)
2929
{
3030
$this->filePath = $filePath;
3131
$this->size = $size;

Tests/Functional/CovertTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testConvertRequestMultipleTimes($request, $firstFactory, $second
9898
}
9999
}
100100

101-
public function requestProvider()
101+
public static function requestProvider(): array
102102
{
103103
$sfRequest = new Request(
104104
[
@@ -120,8 +120,8 @@ public function requestProvider()
120120
'c2' => ['c3' => 'bar'],
121121
],
122122
[
123-
'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', \UPLOAD_ERR_OK),
124-
'foo' => ['f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', \UPLOAD_ERR_OK)],
123+
'f1' => self::createUploadedFile('F1', 'f1.txt', 'text/plain', \UPLOAD_ERR_OK),
124+
'foo' => ['f2' => self::createUploadedFile('F2', 'f2.txt', 'text/plain', \UPLOAD_ERR_OK)],
125125
],
126126
[
127127
'REQUEST_METHOD' => 'POST',
@@ -195,7 +195,7 @@ public function testConvertResponseMultipleTimes($response, $firstFactory, $seco
195195
}
196196
}
197197

198-
public function responseProvider()
198+
public static function responseProvider(): array
199199
{
200200
$sfResponse = new Response(
201201
'Response content.',
@@ -227,7 +227,7 @@ public function responseProvider()
227227
];
228228
}
229229

230-
private function createUploadedFile($content, $originalName, $mimeType, $error)
230+
private static function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile
231231
{
232232
$path = tempnam(sys_get_temp_dir(), uniqid());
233233
file_put_contents($path, $content);

0 commit comments

Comments
 (0)