From af2c581659dc33e275742b21ac6d92c289cb426e Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 2 Feb 2023 08:37:03 +0000 Subject: [PATCH] minor optimization --- src/Value.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Value.php b/src/Value.php index 5d13ea13..3cb9c456 100644 --- a/src/Value.php +++ b/src/Value.php @@ -257,29 +257,27 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess { $this->logDeprecationUnlessCalledBy('serialize'); - $rs = ''; - if (!isset(static::$xmlrpcTypes[$typ])) { - return $rs; + return ''; } switch (static::$xmlrpcTypes[$typ]) { case 1: switch ($typ) { case static::$xmlrpcBase64: - $rs .= "<{$typ}>" . base64_encode($val) . ""; + $rs = "<{$typ}>" . base64_encode($val) . ""; break; case static::$xmlrpcBoolean: - $rs .= "<{$typ}>" . ($val ? '1' : '0') . ""; + $rs = "<{$typ}>" . ($val ? '1' : '0') . ""; break; case static::$xmlrpcString: // Do NOT use htmlentities, since it will produce named html entities, which are invalid xml - $rs .= "<{$typ}>" . $this->getCharsetEncoder()->encodeEntities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . ""; + $rs = "<{$typ}>" . $this->getCharsetEncoder()->encodeEntities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . ""; break; case static::$xmlrpcInt: case static::$xmlrpcI4: case static::$xmlrpcI8: - $rs .= "<{$typ}>" . (int)$val . ""; + $rs = "<{$typ}>" . (int)$val . ""; break; case static::$xmlrpcDouble: // avoid using standard conversion of float to string because it is locale-dependent, @@ -287,46 +285,45 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess // sprintf('%F') could be most likely ok, but it fails e.g. on 2e-14. // The code below tries its best at keeping max precision while avoiding exp notation, // but there is of course no limit in the number of decimal places to be used... - $rs .= "<{$typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, PhpXmlRpc::$xmlpc_double_precision, '.', '')) . ""; + $rs = "<{$typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, PhpXmlRpc::$xmlpc_double_precision, '.', '')) . ""; break; case static::$xmlrpcDateTime: if (is_string($val)) { - $rs .= "<{$typ}>{$val}"; + $rs = "<{$typ}>{$val}"; // DateTimeInterface is not present in php 5.4... } elseif (is_a($val, 'DateTimeInterface') || is_a($val, 'DateTime')) { - $rs .= "<{$typ}>" . $val->format('Ymd\TH:i:s') . ""; + $rs = "<{$typ}>" . $val->format('Ymd\TH:i:s') . ""; } elseif (is_int($val)) { - $rs .= "<{$typ}>" . date('Ymd\TH:i:s', $val) . ""; + $rs = "<{$typ}>" . date('Ymd\TH:i:s', $val) . ""; } else { // not really a good idea here: but what should we output anyway? left for backward compat... - $rs .= "<{$typ}>{$val}"; + $rs = "<{$typ}>{$val}"; } break; case static::$xmlrpcNull: if (PhpXmlRpc::$xmlrpc_null_apache_encoding) { - $rs .= ""; + $rs = ""; } else { - $rs .= ""; + $rs = ""; } break; default: // no standard type value should arrive here, but provide a possibility // for xml-rpc values of unknown type... - $rs .= "<{$typ}>{$val}"; + $rs = "<{$typ}>{$val}"; } break; case 3: // struct if ($this->_php_class) { - $rs .= '\n"; + $rs = '\n"; } else { - $rs .= "\n"; + $rs = "\n"; } $charsetEncoder = $this->getCharsetEncoder(); /** @var Value $val2 */ foreach ($val as $key2 => $val2) { $rs .= '' . $charsetEncoder->encodeEntities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n"; - //$rs.=$this->serializeval($val2); $rs .= $val2->serialize($charsetEncoding); $rs .= "\n"; } @@ -334,15 +331,16 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess break; case 2: // array - $rs .= "\n\n"; + $rs = "\n\n"; /** @var Value $element */ foreach ($val as $element) { - //$rs.=$this->serializeval($val[$i]); $rs .= $element->serialize($charsetEncoding); } $rs .= "\n"; break; default: + /// @todo log a warning? + $rs = ''; break; } -- 2.47.0