Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Apply PHP 8.0 Syntax and constructor promotion #17

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 38 additions & 65 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,13 @@ protected function __construct($source, $decodeType)
$this->token = self::EOF;
$this->offset = 0;

switch ($decodeType) {
case Json::TYPE_ARRAY:
case Json::TYPE_OBJECT:
$this->decodeType = $decodeType;
break;
default:
throw new InvalidArgumentException(sprintf(
'Unknown decode type "%s", please use one of the Json::TYPE_* constants',
$decodeType
));
}
$this->decodeType = match ($decodeType) {
Json::TYPE_ARRAY, Json::TYPE_OBJECT => $decodeType,
default => throw new InvalidArgumentException(sprintf(
'Unknown decode type "%s", please use one of the Json::TYPE_* constants',
$decodeType
)),
};

// Set pointer at first token
$this->getNextToken();
Expand Down Expand Up @@ -420,37 +416,18 @@ protected function getNextToken()
}

$chr = $str[$i];
switch ($chr) {
case '"':
$result .= '"';
break;
case '\\':
$result .= '\\';
break;
case '/':
$result .= '/';
break;
case 'b':
$result .= "\x08";
break;
case 'f':
$result .= "\x0c";
break;
case 'n':
$result .= "\x0a";
break;
case 'r':
$result .= "\x0d";
break;
case 't':
$result .= "\x09";
break;
case '\'':
$result .= '\'';
break;
default:
throw new RuntimeException(sprintf('Illegal escape sequence "%s"', $chr));
}
match ($chr) {
'"' => $result .= '"',
'\\' => $result .= '\\',
'/' => $result .= '/',
'b' => $result .= "\x08",
'f' => $result .= "\x0c",
'n' => $result .= "\x0a",
'r' => $result .= "\x0d",
't' => $result .= "\x09",
'\'' => $result .= '\'',
default => throw new RuntimeException(sprintf('Illegal escape sequence "%s"', $chr)),
};
} while ($i < $strLength);

$this->token = self::DATUM;
Expand Down Expand Up @@ -538,28 +515,24 @@ protected static function utf162utf8($utf16)
}

$bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);

switch (true) {
case (0x7F & $bytes) === $bytes:
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x7F & $bytes);

case (0x07FF & $bytes) === $bytes:
// Return a 2-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xC0 | (($bytes >> 6) & 0x1F))
. chr(0x80 | ($bytes & 0x3F));

case (0xFFFF & $bytes) === $bytes:
// Return a 3-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xE0 | (($bytes >> 12) & 0x0F))
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F));
}

// ignoring UTF-32 for now, sorry
return '';
return match (true) {
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
(0x7F & $bytes) === $bytes => chr(0x7F & $bytes),

// Return a 2-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
(0x07FF & $bytes) === $bytes => chr(0xC0 | (($bytes >> 6) & 0x1F))
. chr(0x80 | ($bytes & 0x3F)),

// Return a 3-byte UTF-8 character;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
(0xFFFF & $bytes) === $bytes => chr(0xE0 | (($bytes >> 12) & 0x0F))
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F)),

// ignoring UTF-32 for now, sorry
default => '',
};
}
}
68 changes: 23 additions & 45 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@
*/
class Encoder
{
/**
* Whether or not to check for possible cycling.
*
* @var bool
*/
protected $cycleCheck;

/**
* Additional options used during encoding.
*
* @var array
*/
protected $options = [];

/**
* Array of visited objects; used to prevent cycling.
*
Expand All @@ -64,10 +50,8 @@ class Encoder
* @param bool $cycleCheck Whether or not to check for recursion when encoding.
* @param array $options Additional options used during encoding.
*/
protected function __construct($cycleCheck = false, array $options = [])
protected function __construct(protected $cycleCheck = false, protected array $options = [])
{
$this->cycleCheck = $cycleCheck;
$this->options = $options;
}

/**
Expand All @@ -78,7 +62,7 @@ protected function __construct($cycleCheck = false, array $options = [])
* @param array $options Additional options used during encoding.
* @return string The encoded value.
*/
public static function encode($value, $cycleCheck = false, array $options = [])
public static function encode(mixed $value, $cycleCheck = false, array $options = [])
{
$encoder = new static($cycleCheck, $options);

Expand All @@ -103,7 +87,7 @@ public static function encode($value, $cycleCheck = false, array $options = [])
* @param mixed $value The value to be encoded.
* @return string Encoded value.
*/
protected function encodeValue(&$value)
protected function encodeValue(mixed &$value)
{
if (is_object($value)) {
return $this->encodeObject($value);
Expand Down Expand Up @@ -182,10 +166,9 @@ protected function encodeObject(&$value)
/**
* Determine if an object has been serialized already.
*
* @param mixed $value
* @return bool
*/
protected function wasVisited(&$value)
protected function wasVisited(mixed &$value)
{
if (in_array($value, $this->visited, true)) {
return true;
Expand Down Expand Up @@ -264,10 +247,9 @@ protected function encodeAssociativeArray($array)
* If value type is not a string, number, boolean, or null, the string
* 'null' is returned.
*
* @param mixed $value
* @return string
*/
protected function encodeDatum($value)
protected function encodeDatum(mixed $value)
{
if (is_int($value) || is_float($value)) {
return str_replace(',', '.', (string) $value);
Expand Down Expand Up @@ -594,27 +576,23 @@ protected static function utf82utf16($utf8)
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
}

switch (strlen($utf8)) {
case 1:
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return $utf8;

case 2:
// Return a UTF-16 character from a 2-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));

case 3:
// Return a UTF-16 character from a 3-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr((0xF0 & (ord($utf8[0]) << 4))
| (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6))
| (0x7F & ord($utf8[2])));
}

// ignoring UTF-32 for now, sorry
return '';
return match (strlen($utf8)) {
// This case should never be reached, because we are in ASCII range;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
1 => $utf8,

// Return a UTF-16 character from a 2-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
2 => chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1]))),

// Return a UTF-16 character from a 3-byte UTF-8 char;
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
3 => chr((0xF0 & (ord($utf8[0]) << 4))
| (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6))
| (0x7F & ord($utf8[2]))),

// ignoring UTF-32 for now, sorry
default => '',
};
}
}
6 changes: 4 additions & 2 deletions src/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laminas\Json;

use Stringable;

/**
* Encode a string to a native JavaScript expression.
*
Expand Down Expand Up @@ -33,7 +35,7 @@
* }
* </code>
*/
class Expr
class Expr implements Stringable
{
/**
* Storage for javascript expression.
Expand All @@ -55,7 +57,7 @@ public function __construct($expression)
*
* @return string holded javascript expression.
*/
public function __toString()
public function __toString(): string
{
return $this->expression;
}
Expand Down
14 changes: 5 additions & 9 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ public static function decode($encodedValue, $objectDecodeType = self::TYPE_OBJE
*
* @see Laminas\Json\Expr
*
* @param mixed $valueToEncode
* @param bool $cycleCheck Optional; whether or not to check for object recursion; off by default
* @param array $options Additional options used during encoding
* @return string JSON encoded object
*/
public static function encode($valueToEncode, $cycleCheck = false, array $options = [])
public static function encode(mixed $valueToEncode, $cycleCheck = false, array $options = [])
{
if (is_object($valueToEncode)) {
if (method_exists($valueToEncode, 'toJson')) {
Expand Down Expand Up @@ -143,7 +142,7 @@ public static function encode($valueToEncode, $cycleCheck = false, array $option
* @return mixed
*/
protected static function recursiveJsonExprFinder(
$value,
mixed $value,
SplQueue $javascriptExpressions,
$currentKey = null
) {
Expand Down Expand Up @@ -337,13 +336,12 @@ private static function decodeViaPhpBuiltIn($encodedValue, $objectDecodeType)
* Encoder component, based on availability of the built-in and/or whether
* or not the component encoder is requested.
*
* @param mixed $valueToEncode
* @param bool $cycleCheck
* @param array $options
* @param bool $prettyPrint
* @return string
*/
private static function encodeValue($valueToEncode, $cycleCheck, array $options, $prettyPrint)
private static function encodeValue(mixed $valueToEncode, $cycleCheck, array $options, $prettyPrint)
{
if (function_exists('json_encode') && static::$useBuiltinEncoderDecoder !== true) {
return self::encodeViaPhpBuiltIn($valueToEncode, $prettyPrint);
Expand All @@ -364,12 +362,11 @@ private static function encodeValue($valueToEncode, $cycleCheck, array $options,
*
* If $prettyPrint is boolean true, also uses JSON_PRETTY_PRINT.
*
* @param mixed $valueToEncode
* @param bool $prettyPrint
* @return string|false Boolean false return value if json_encode is not
* available, or the $useBuiltinEncoderDecoder flag is enabled.
*/
private static function encodeViaPhpBuiltIn($valueToEncode, $prettyPrint = false)
private static function encodeViaPhpBuiltIn(mixed $valueToEncode, $prettyPrint = false)
{
if (! function_exists('json_encode') || static::$useBuiltinEncoderDecoder === true) {
return false;
Expand All @@ -393,13 +390,12 @@ private static function encodeViaPhpBuiltIn($valueToEncode, $prettyPrint = false
* and, if so, returns the result of that operation, otherwise returning
* the encoded value.
*
* @param mixed $valueToEncode
* @param bool $cycleCheck
* @param array $options
* @param bool $prettyPrint
* @return string
*/
private static function encodeViaEncoder($valueToEncode, $cycleCheck, array $options, $prettyPrint)
private static function encodeViaEncoder(mixed $valueToEncode, $cycleCheck, array $options, $prettyPrint)
{
$encodedResult = Encoder::encode($valueToEncode, $cycleCheck, $options);

Expand Down
12 changes: 5 additions & 7 deletions test/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@

class JsonTest extends TestCase
{
/** @var bool */
private $originalUseBuiltinEncoderDecoderValue;
private bool $originalUseBuiltinEncoderDecoderValue;

public function setUp(): void
{
Expand All @@ -49,9 +48,9 @@ public function tearDown(): void
*/
public function assertEncodesToDecodable($values, $message = null): void
{
$message = $message ?: 'One or more values could not be decoded after encoding';
$values = $values ?? [null];
$values = is_scalar($values) ? [$values] : $values;
$message = $message ?: 'One or more values could not be decoded after encoding';
$values ??= [null];
$values = is_scalar($values) ? [$values] : $values;

foreach ($values as $value) {
$encoded = Json\Encoder::encode($value);
Expand Down Expand Up @@ -377,10 +376,9 @@ public function testDecodeObjectOfArrays()
*
* Casts objects to arrays for expectation comparisons.
*
* @param mixed $value
* @return mixed
*/
protected function toArray($value)
protected function toArray(mixed $value)
{
if (! is_array($value) || ! is_object($value)) {
return $value;
Expand Down
10 changes: 2 additions & 8 deletions test/TestAsset/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@

class Bar
{
/** @var mixed */
protected $val;

/** @param mixed $someval */
public function __construct($someval)
public function __construct(protected mixed $val)
{
$this->val = $someval;
}

/**
* Bar
*
* @param bool $one
* @param string $two
* @param mixed $three
* @return array
*/
public function foo($one, $two = 'two', $three = null)
public function foo($one, $two = 'two', mixed $three = null)
{
return [$one, $two, $three, $this->val];
}
Expand Down
Loading