Skip to content

Commit

Permalink
Merge pull request #8615 from cakephp/http-response-json
Browse files Browse the repository at this point in the history
Since return type of json_decode() is mixed, no need to check return …
  • Loading branch information
markstory committed Apr 8, 2016
2 parents d5d9a03 + 6464d3e commit 7474676
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/Network/Http/Response.php
Expand Up @@ -380,19 +380,14 @@ public function body($parser = null)
/**
* Get the response body as JSON decoded data.
*
* @return null|array
* @return mixed
*/
protected function _getJson()
{
if (!empty($this->_json)) {
return $this->_json;
}
$data = json_decode($this->_body, true);
if ($data) {
$this->_json = $data;
return $this->_json;
}
return null;
return $this->_json = json_decode($this->_body, true);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion tests/TestCase/Network/Http/ResponseTest.php
Expand Up @@ -100,7 +100,23 @@ public function testBodyJson()

$data = '';
$response = new Response([], $data);
$this->assertFalse(isset($response->json));
$this->assertNull($response->json);

$data = json_encode([]);
$response = new Response([], $data);
$this->assertTrue(is_array($response->json));

$data = json_encode(null);
$response = new Response([], $data);
$this->assertNull($response->json);

$data = json_encode(false);
$response = new Response([], $data);
$this->assertFalse($response->json);

$data = json_encode('');
$response = new Response([], $data);
$this->assertSame('', $response->json);
}

/**
Expand Down

0 comments on commit 7474676

Please sign in to comment.