Skip to content

Commit 0708fd7

Browse files
authored
Merge pull request #8161 from kenjis/fix-Request-body-0
fix: when request body is `0`, $body will be null
2 parents 17dab6a + 42be01d commit 0708fd7

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

phpstan-baseline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@
21482148
];
21492149
$ignoreErrors[] = [
21502150
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
2151-
'count' => 6,
2151+
'count' => 5,
21522152
'path' => __DIR__ . '/system/HTTP/IncomingRequest.php',
21532153
];
21542154
$ignoreErrors[] = [

system/HTTP/IncomingRequest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,14 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
169169
$body = file_get_contents('php://input');
170170
}
171171

172+
// If file_get_contents() returns false or empty string, set null.
173+
if ($body === false || $body === '') {
174+
$body = null;
175+
}
176+
172177
$this->config = $config;
173178
$this->uri = $uri;
174-
$this->body = ! empty($body) ? $body : null;
179+
$this->body = $body;
175180
$this->userAgent = $userAgent;
176181
$this->validLocales = $config->supportedLocales;
177182

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public function testGetJsonVarReturnsNullFromNullBody(): void
518518
$this->assertNull($request->getJsonVar('myKey'));
519519
}
520520

521-
public function testgetJSONReturnsNullFromNullBody(): void
521+
public function testGetJSONReturnsNullFromNullBody(): void
522522
{
523523
$config = new App();
524524
$config->baseURL = 'http://example.com/';
@@ -847,7 +847,7 @@ public function testGetPostSecondStreams(): void
847847
$this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost());
848848
}
849849

850-
public function testWithFalseBody(): void
850+
public function testGetBodyWithFalseBody(): void
851851
{
852852
// Use `false` here to simulate file_get_contents returning a false value
853853
$request = $this->createRequest(null, false);
@@ -856,6 +856,13 @@ public function testWithFalseBody(): void
856856
$this->assertNull($request->getBody());
857857
}
858858

859+
public function testGetBodyWithZero(): void
860+
{
861+
$request = $this->createRequest(null, '0');
862+
863+
$this->assertSame('0', $request->getBody());
864+
}
865+
859866
/**
860867
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3020
861868
*/

0 commit comments

Comments
 (0)