Skip to content

Commit

Permalink
Removes deprecations introduced in 2.16.x and fixes CS
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jan 9, 2022
1 parent 800d42e commit 6ec6cee
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/Helper/AbstractHtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Laminas\View\Helper;

use function assert;
use function json_encode;
use function str_replace;
use function strlen;
use function strpos;
Expand Down
16 changes: 5 additions & 11 deletions src/Helper/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use Laminas\Http\Response;

use function json_encode;
use function trigger_error;

use const E_USER_DEPRECATED;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;

Expand All @@ -18,15 +16,15 @@
*/
class Json extends AbstractHelper
{
/** @var Response */
/** @var Response|null */
protected $response;

/**
* Encode data as JSON and set response header
*
* @param mixed $data
* @param array $jsonOptions Options to pass to JsonFormatter::encode()
* @return string|void
* @return string
*/
public function __invoke($data, array $jsonOptions = [])
{
Expand All @@ -40,15 +38,11 @@ public function __invoke($data, array $jsonOptions = [])
return $data;
}

private function optionsToFlags(array $options = []) : int
private function optionsToFlags(array $options = []): int
{
$prettyPrint = $options['prettyPrint'] ?? false;
$flags = JSON_THROW_ON_ERROR;
$flags |= $prettyPrint ? 0 : JSON_PRETTY_PRINT;
$enableExpr = $options['enableJsonExprFinder'] ?? false;
if ($enableExpr) {
trigger_error('Json Expression Finder options are no longer available', E_USER_DEPRECATED);
}
$flags = JSON_THROW_ON_ERROR;
$flags |= $prettyPrint ? 0 : JSON_PRETTY_PRINT;

return $flags;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/JsonModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function serialize()
$options = (bool) $this->getOption('prettyPrint', false) ? JSON_PRETTY_PRINT : 0;

if (null !== $this->jsonpCallback) {
return $this->jsonpCallback.'('.$this->jsonEncode($variables, $options).');';
return $this->jsonpCallback . '(' . $this->jsonEncode($variables, $options) . ');';
}
return $this->jsonEncode($variables, $options);
}
Expand Down
14 changes: 1 addition & 13 deletions test/Helper/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,12 @@ public function testJsonHelperSetsResponseHeader(): void

public function testJsonHelperReturnsJsonEncodedString(): void
{
$input = [
$input = [
'dory' => 'blue',
'nemo' => 'orange',
];
$expect = json_encode($input, JSON_THROW_ON_ERROR);

self::assertJsonStringEqualsJsonString($expect, ($this->helper)($input));
}

public function testThatADeprecationErrorIsTriggeredWhenExpressionFinderOptionIsUsed(): void
{
$this->expectDeprecation();
$this->helper->__invoke(['foo'], ['enableJsonExprFinder' => true]);
}

public function testThatADeprecationErrorIsNotTriggeredWhenExpressionFinderOptionIsNotUsed(): void
{
$this->expectNotToPerformAssertions();
$this->helper->__invoke(['foo'], ['enableJsonExprFinder' => 'anything other than true']);
}
}
5 changes: 3 additions & 2 deletions test/Model/JsonModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit\Framework\TestCase;

use function json_encode;
use function sprintf;

use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
Expand Down Expand Up @@ -45,15 +46,15 @@ public function testCanSerializeWithJsonpCallback(): void

public function testPrettyPrint(): void
{
$array = [
$array = [
'simple' => 'simple test string',
'stringwithjsonchars' => '\"[1,2]',
'complex' => [
'foo' => 'bar',
'far' => 'boo',
],
];
$model = new JsonModel($array, ['prettyPrint' => true]);
$model = new JsonModel($array, ['prettyPrint' => true]);
$expect = json_encode($array, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
$this->assertEquals($expect, $model->serialize());
}
Expand Down

0 comments on commit 6ec6cee

Please sign in to comment.