diff --git a/Factory/HttpFoundationFactory.php b/Factory/HttpFoundationFactory.php index cad798e..3c3e272 100644 --- a/Factory/HttpFoundationFactory.php +++ b/Factory/HttpFoundationFactory.php @@ -15,7 +15,6 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; -use Psr\Http\Message\UriInterface; use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Request; @@ -40,19 +39,17 @@ public function createRequest(ServerRequestInterface $psrRequest, bool $streamed $server = []; $uri = $psrRequest->getUri(); - if ($uri instanceof UriInterface) { - $server['SERVER_NAME'] = $uri->getHost(); - $server['SERVER_PORT'] = $uri->getPort() ?: ('https' === $uri->getScheme() ? 443 : 80); - $server['REQUEST_URI'] = $uri->getPath(); - $server['QUERY_STRING'] = $uri->getQuery(); + $server['SERVER_NAME'] = $uri->getHost(); + $server['SERVER_PORT'] = $uri->getPort() ?: ('https' === $uri->getScheme() ? 443 : 80); + $server['REQUEST_URI'] = $uri->getPath(); + $server['QUERY_STRING'] = $uri->getQuery(); - if ('' !== $server['QUERY_STRING']) { - $server['REQUEST_URI'] .= '?'.$server['QUERY_STRING']; - } + if ('' !== $server['QUERY_STRING']) { + $server['REQUEST_URI'] .= '?'.$server['QUERY_STRING']; + } - if ('https' === $uri->getScheme()) { - $server['HTTPS'] = 'on'; - } + if ('https' === $uri->getScheme()) { + $server['HTTPS'] = 'on'; } $server['REQUEST_METHOD'] = $psrRequest->getMethod(); @@ -107,7 +104,7 @@ private function createUploadedFile(UploadedFileInterface $psrUploadedFile): Upl */ protected function getTemporaryPath(): string { - return tempnam(sys_get_temp_dir(), uniqid('symfony', true)); + return tempnam(sys_get_temp_dir(), 'symfony'); } public function createResponse(ResponseInterface $psrResponse, bool $streamed = false): Response diff --git a/Factory/PsrHttpFactory.php b/Factory/PsrHttpFactory.php index 7c824fd..d3b5467 100644 --- a/Factory/PsrHttpFactory.php +++ b/Factory/PsrHttpFactory.php @@ -50,7 +50,7 @@ public function __construct( $psr17Factory = match (true) { class_exists(DiscoveryPsr17Factory::class) => new DiscoveryPsr17Factory(), class_exists(NyholmPsr17Factory::class) => new NyholmPsr17Factory(), - default => throw new \LogicException(sprintf('You cannot use the "%s" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', self::class)), + default => throw new \LogicException(\sprintf('You cannot use the "%s" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', self::class)), }; $serverRequestFactory ??= $psr17Factory; @@ -85,12 +85,7 @@ public function createRequest(Request $symfonyRequest): ServerRequestInterface } $body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true)); - - if (method_exists(Request::class, 'getContentTypeFormat')) { - $format = $symfonyRequest->getContentTypeFormat(); - } else { - $format = $symfonyRequest->getContentType(); - } + $format = $symfonyRequest->getContentTypeFormat(); if ('json' === $format) { $parsedBody = json_decode($symfonyRequest->getContent(), true, 512, \JSON_BIGINT_AS_STRING); @@ -183,7 +178,7 @@ public function createResponse(Response $symfonyResponse): ResponseInterface $headers = $symfonyResponse->headers->all(); $cookies = $symfonyResponse->headers->getCookies(); - if (!empty($cookies)) { + if ($cookies) { $headers['Set-Cookie'] = []; foreach ($cookies as $cookie) { @@ -200,8 +195,7 @@ public function createResponse(Response $symfonyResponse): ResponseInterface } $protocolVersion = $symfonyResponse->getProtocolVersion(); - $response = $response->withProtocolVersion($protocolVersion); - return $response; + return $response->withProtocolVersion($protocolVersion); } } diff --git a/Factory/UploadedFile.php b/Factory/UploadedFile.php index f680dd5..34d4058 100644 --- a/Factory/UploadedFile.php +++ b/Factory/UploadedFile.php @@ -59,7 +59,7 @@ public function move(string $directory, ?string $name = null): File try { $this->psrUploadedFile->moveTo((string) $target); } catch (\RuntimeException $e) { - throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, $e->getMessage()), 0, $e); + throw new FileException(\sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, $e->getMessage()), 0, $e); } @chmod($target, 0666 & ~umask()); diff --git a/Tests/Factory/HttpFoundationFactoryTest.php b/Tests/Factory/HttpFoundationFactoryTest.php index e40992e..ed71b36 100644 --- a/Tests/Factory/HttpFoundationFactoryTest.php +++ b/Tests/Factory/HttpFoundationFactoryTest.php @@ -172,15 +172,15 @@ public function testCreateUploadedFile() $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile); $size = $symfonyUploadedFile->getSize(); - $uniqid = uniqid('', true); - $symfonyUploadedFile->move($this->tmpDir, $uniqid); + $filename = 'upload'; + $symfonyUploadedFile->move($this->tmpDir, $filename); $this->assertEquals($uploadedFile->getSize(), $size); $this->assertEquals(\UPLOAD_ERR_OK, $symfonyUploadedFile->getError()); $this->assertEquals('myfile.txt', $symfonyUploadedFile->getClientOriginalName()); $this->assertEquals('txt', $symfonyUploadedFile->getClientOriginalExtension()); $this->assertEquals('text/plain', $symfonyUploadedFile->getClientMimeType()); - $this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$uniqid)); + $this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$filename)); } public function testCreateUploadedFileWithError() @@ -198,7 +198,7 @@ public function testCreateUploadedFileWithError() private function createUploadedFile(string $content, int $error, string $clientFileName, string $clientMediaType): UploadedFile { - $filePath = tempnam($this->tmpDir, uniqid('', true)); + $filePath = tempnam($this->tmpDir, 'sftest'); file_put_contents($filePath, $content); return new UploadedFile($filePath, filesize($filePath), $error, $clientFileName, $clientMediaType); diff --git a/Tests/Factory/PsrHttpFactoryTest.php b/Tests/Factory/PsrHttpFactoryTest.php index 0c41221..f5b09c8 100644 --- a/Tests/Factory/PsrHttpFactoryTest.php +++ b/Tests/Factory/PsrHttpFactoryTest.php @@ -131,7 +131,7 @@ public function testGetContentCanBeCalledAfterRequestCreation() private function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile { - $path = tempnam($this->tmpDir, uniqid('', true)); + $path = $this->createTempFile(); file_put_contents($path, $content); return new UploadedFile($path, $originalName, $mimeType, $error, true); @@ -182,7 +182,7 @@ public function testCreateResponseFromStreamed() public function testCreateResponseFromBinaryFile() { - $path = tempnam($this->tmpDir, uniqid('', true)); + $path = $this->createTempFile(); file_put_contents($path, 'Binary'); $response = new BinaryFileResponse($path); @@ -194,7 +194,7 @@ public function testCreateResponseFromBinaryFile() public function testCreateResponseFromBinaryFileWithRange() { - $path = tempnam($this->tmpDir, uniqid('', true)); + $path = $this->createTempFile(); file_put_contents($path, 'Binary'); $request = new Request(); @@ -219,14 +219,14 @@ public function testUploadErrNoFile() [], [], [ - 'f1' => $file, - 'f2' => ['name' => null, 'type' => null, 'tmp_name' => null, 'error' => \UPLOAD_ERR_NO_FILE, 'size' => 0], - ], + 'f1' => $file, + 'f2' => ['name' => null, 'type' => null, 'tmp_name' => null, 'error' => \UPLOAD_ERR_NO_FILE, 'size' => 0], + ], [ - 'REQUEST_METHOD' => 'POST', - 'HTTP_HOST' => 'dunglas.fr', - 'HTTP_X_SYMFONY' => '2.8', - ], + 'REQUEST_METHOD' => 'POST', + 'HTTP_HOST' => 'dunglas.fr', + 'HTTP_X_SYMFONY' => '2.8', + ], 'Content' ); @@ -287,4 +287,9 @@ private static function buildHttpMessageFactory(): PsrHttpFactory return new PsrHttpFactory($factory, $factory, $factory, $factory); } + + private function createTempFile(): string + { + return tempnam($this->tmpDir, 'sftest'); + } } diff --git a/Tests/Fixtures/ServerRequest.php b/Tests/Fixtures/ServerRequest.php index 99b7abb..f7ea108 100644 --- a/Tests/Fixtures/ServerRequest.php +++ b/Tests/Fixtures/ServerRequest.php @@ -11,7 +11,6 @@ namespace Symfony\Bridge\PsrHttpMessage\Tests\Fixtures; -use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; diff --git a/Tests/Fixtures/Uri.php b/Tests/Fixtures/Uri.php index ded92bf..6643149 100644 --- a/Tests/Fixtures/Uri.php +++ b/Tests/Fixtures/Uri.php @@ -47,13 +47,13 @@ public function getScheme(): string public function getAuthority(): string { - if (empty($this->host)) { + if (!$this->host) { return ''; } $authority = $this->host; - if (!empty($this->userInfo)) { + if ($this->userInfo) { $authority = $this->userInfo.'@'.$authority; } diff --git a/Tests/Functional/CovertTest.php b/Tests/Functional/CovertTest.php index c350f29..23bdbb9 100644 --- a/Tests/Functional/CovertTest.php +++ b/Tests/Functional/CovertTest.php @@ -217,7 +217,7 @@ public static function responseProvider(): array private static function createUploadedFile(string $content, string $originalName, string $mimeType, int $error): UploadedFile { - $path = tempnam(sys_get_temp_dir(), uniqid('', true)); + $path = tempnam(sys_get_temp_dir(), 'sftest'); file_put_contents($path, $content); return new UploadedFile($path, $originalName, $mimeType, $error, true); diff --git a/composer.json b/composer.json index 4d7589c..a34dfb1 100644 --- a/composer.json +++ b/composer.json @@ -16,23 +16,23 @@ } ], "require": { - "php": ">=8.1", + "php": ">=8.2", "psr/http-message": "^1.0|^2.0", - "symfony/http-foundation": "^5.4|^6.0|^7.0" + "symfony/http-foundation": "^6.4|^7.0" }, "require-dev": { - "symfony/browser-kit": "^5.4|^6.0|^7.0", - "symfony/config": "^5.4|^6.0|^7.0", - "symfony/event-dispatcher": "^5.4|^6.0|^7.0", - "symfony/framework-bundle": "^6.2|^7.0", - "symfony/http-kernel": "^6.2|^7.0", + "symfony/browser-kit": "^6.4|^7.0", + "symfony/config": "^6.4|^7.0", + "symfony/event-dispatcher": "^6.4|^7.0", + "symfony/framework-bundle": "^6.4|^7.0", + "symfony/http-kernel": "^6.4|^7.0", "nyholm/psr7": "^1.1", "php-http/discovery": "^1.15", "psr/log": "^1.1.4|^2|^3" }, "conflict": { "php-http/discovery": "<1.15", - "symfony/http-kernel": "<6.2" + "symfony/http-kernel": "<6.4" }, "config": { "allow-plugins": {