diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 451c925f373a..3cbe5a4732fc 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -2148,7 +2148,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 6, + 'count' => 5, 'path' => __DIR__ . '/system/HTTP/IncomingRequest.php', ]; $ignoreErrors[] = [ diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 079a3b69d02f..307a78d38b4a 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -169,9 +169,14 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U $body = file_get_contents('php://input'); } + // If file_get_contents() returns false or empty string, set null. + if ($body === false || $body === '') { + $body = null; + } + $this->config = $config; $this->uri = $uri; - $this->body = ! empty($body) ? $body : null; + $this->body = $body; $this->userAgent = $userAgent; $this->validLocales = $config->supportedLocales; diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 3855a4b85c94..cfc26a988857 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -518,7 +518,7 @@ public function testGetJsonVarReturnsNullFromNullBody(): void $this->assertNull($request->getJsonVar('myKey')); } - public function testgetJSONReturnsNullFromNullBody(): void + public function testGetJSONReturnsNullFromNullBody(): void { $config = new App(); $config->baseURL = 'http://example.com/'; @@ -847,7 +847,7 @@ public function testGetPostSecondStreams(): void $this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost()); } - public function testWithFalseBody(): void + public function testGetBodyWithFalseBody(): void { // Use `false` here to simulate file_get_contents returning a false value $request = $this->createRequest(null, false); @@ -856,6 +856,13 @@ public function testWithFalseBody(): void $this->assertNull($request->getBody()); } + public function testGetBodyWithZero(): void + { + $request = $this->createRequest(null, '0'); + + $this->assertSame('0', $request->getBody()); + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/3020 */