From 21a59cdc129a89fa2150d10be759a849e74db6e6 Mon Sep 17 00:00:00 2001 From: gggeek Date: Wed, 1 Feb 2023 11:45:13 +0000 Subject: [PATCH] add more deprecation warnings on use of internal-only methods --- NEWS.md | 1 + src/Client.php | 23 ++-- src/Traits/DeprecationLogger.php | 21 +++ src/Value.php | 214 ++++++++++++++++--------------- 4 files changed, 145 insertions(+), 114 deletions(-) diff --git a/NEWS.md b/NEWS.md index 84b04d46..2c45e041 100644 --- a/NEWS.md +++ b/NEWS.md @@ -184,6 +184,7 @@ objects for the cases in which it previously returned a string - if you replaced the Logger class, take care that you will have to implement methods `error`, `warning` and `debug` (all is ok if you subclassed it) + - calling method `Value::serializeData` is now deprecated - traits have been introduced for all classes dealing with Logger, XMLParser and CharsetEncoder; method `setCharsetEncoder` is now static - new methods in helper classes: `Charset::knownCharsets`, `Http::parseAcceptHeader`, `XMLParser::truncateValueForLog` diff --git a/src/Client.php b/src/Client.php index 88a9dd6c..a3b7eeec 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1009,7 +1009,7 @@ class Client $this->proxy_user, $this->proxy_pass, $this->proxy_authtype, - // bc + // BC $method == 'http11' ? 'http' : $method, $this->keepalive, $this->key, @@ -1764,10 +1764,11 @@ class Client return $response; } - // BC layer - + // *** BC layer *** /** + * @deprecated + * * @param Request $req * @param string $server * @param int $port @@ -1782,8 +1783,6 @@ class Client * @param int $proxyAuthType * @param string $method * @return Response - * @deprecated - * */ protected function sendPayloadHTTP10($req, $server, $port, $timeout = 0, $username = '', $password = '', $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, @@ -1796,6 +1795,8 @@ class Client } /** + * @deprecated + * * @param Request $req * @param string $server * @param int $port @@ -1817,8 +1818,6 @@ class Client * @param string $keyPass * @param int $sslVersion * @return Response - * @deprecated - * */ protected function sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '', $password = '', $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, @@ -1862,6 +1861,8 @@ class Client $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'http', $key = '', $keyPass = '', $sslVersion = 0) { + $this->logDeprecationUnlessCalledBy('send'); + return $this->sendViaSocket($req, $method, $server, $port, $this->path, array( 'accepted_charset_encodings' => $this->accepted_charset_encodings, 'accepted_compression' => $this->accepted_compression, @@ -1897,6 +1898,8 @@ class Client } /** + * @deprecated + * * @param Request $req * @param string $server * @param int $port @@ -1919,14 +1922,14 @@ class Client * @param string $keyPass * @param int $sslVersion * @return Response - * @deprecated - * */ protected function sendPayloadCURL($req, $server, $port, $timeout = 0, $username = '', $password = '', $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '', $keyPass = '', $sslVersion = 0) { + $this->logDeprecationUnlessCalledBy('send'); + return $this->sendViaCURL($req, $method, $server, $port, $this->path, array( 'accepted_charset_encodings' => $this->accepted_charset_encodings, 'accepted_compression' => $this->accepted_compression, @@ -1992,6 +1995,8 @@ class Client $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '', $keyPass = '', $sslVersion = 0) { + $this->logDeprecationUnlessCalledBy('sendViaCURL'); + return $this->createCurlHandle($req, $method, $server, $port, $this->path, array( 'accepted_charset_encodings' => $this->accepted_charset_encodings, 'accepted_compression' => $this->accepted_compression, diff --git a/src/Traits/DeprecationLogger.php b/src/Traits/DeprecationLogger.php index a499789a..ba971e7c 100644 --- a/src/Traits/DeprecationLogger.php +++ b/src/Traits/DeprecationLogger.php @@ -16,4 +16,25 @@ trait DeprecationLogger $this->getLogger()->warning('XML-RPC Deprecated: ' . $message); } + + /** + * @param string $callee + * @param string $expectedCaller atm only the method name is supported + * @return void + */ + protected function logDeprecationUnlessCalledBy($expectedCaller) + { + if (PhpXmlRpc::$xmlrpc_silence_deprecations) { + return; + } + + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); + /// @todo we should check as well $trace[2]['class'], and make sure that it is a descendent of the class passed in in $expectedCaller + if ($trace[2]['function'] === $expectedCaller) { + return; + } + + $this->getLogger()->warning('XML-RPC Deprecated: ' . $trace[1]['class'] . '::' . $trace[1]['function'] . + ' is only supposed to be called by ' . $expectedCaller); + } } diff --git a/src/Value.php b/src/Value.php index 9ec2cfc4..0a5c7cb3 100644 --- a/src/Value.php +++ b/src/Value.php @@ -255,6 +255,8 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ protected function serializeData($typ, $val, $charsetEncoding = '') { + $this->logDeprecationUnlessCalledBy('serialize'); + $rs = ''; if (!isset(static::$xmlrpcTypes[$typ])) { @@ -361,67 +363,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess return '' . $this->serializeData($typ, $val, $charsetEncoding) . "\n"; } - /** - * Checks whether a struct member with a given name is present. - * - * Works only on xml-rpc values of type struct. - * - * @param string $key the name of the struct member to be looked up - * @return boolean - * - * @deprecated use array access, e.g. isset($val[$key]) - */ - public function structMemExists($key) - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return array_key_exists($key, $this->me['struct']); - } - - /** - * Returns the value of a given struct member (an xml-rpc value object in itself). - * Will raise a php warning if struct member of given name does not exist. - * - * @param string $key the name of the struct member to be looked up - * @return Value - * - * @deprecated use array access, e.g. $val[$key] - */ - public function structMem($key) - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return $this->me['struct'][$key]; - } - - /** - * Reset internal pointer for xml-rpc values of type struct. - * @return void - * - * @deprecated iterate directly over the object using foreach instead - */ - public function structReset() - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - reset($this->me['struct']); - } - - /** - * Return next member element for xml-rpc values of type struct. - * - * @return Value - * @throws \Error starting with php 8.0, this function should not be used, as it will always throw - * - * @deprecated iterate directly over the object using foreach instead - */ - public function structEach() - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return @each($this->me['struct']); - } - /** * Returns the value of a scalar xml-rpc value (base 64 decoding is automatically handled here) * @@ -451,50 +392,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess return $a; } - /** - * Returns the m-th member of an xml-rpc value of array type. - * - * @param integer $key the index of the value to be retrieved (zero based) - * - * @return Value - * - * @deprecated use array access, e.g. $val[$key] - */ - public function arrayMem($key) - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return $this->me['array'][$key]; - } - - /** - * Returns the number of members in an xml-rpc value of array type. - * - * @return integer - * - * @deprecated use count() instead - */ - public function arraySize() - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return count($this->me['array']); - } - - /** - * Returns the number of members in an xml-rpc value of struct type. - * - * @return integer - * - * @deprecated use count() instead - */ - public function structSize() - { - $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); - - return count($this->me['struct']); - } - /** * Returns the number of members in an xml-rpc value: * - 0 for uninitialized values @@ -670,4 +567,111 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess throw new StateErrorException("XML-RPC Value is of type 'undef' and can not be accessed using array index"); } } + + // *** BC layer *** + + /** + * Checks whether a struct member with a given name is present. + * + * Works only on xml-rpc values of type struct. + * + * @param string $key the name of the struct member to be looked up + * @return boolean + * + * @deprecated use array access, e.g. isset($val[$key]) + */ + public function structMemExists($key) + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return array_key_exists($key, $this->me['struct']); + } + + /** + * Returns the value of a given struct member (an xml-rpc value object in itself). + * Will raise a php warning if struct member of given name does not exist. + * + * @param string $key the name of the struct member to be looked up + * @return Value + * + * @deprecated use array access, e.g. $val[$key] + */ + public function structMem($key) + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return $this->me['struct'][$key]; + } + + /** + * Reset internal pointer for xml-rpc values of type struct. + * @return void + * + * @deprecated iterate directly over the object using foreach instead + */ + public function structReset() + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + reset($this->me['struct']); + } + + /** + * Return next member element for xml-rpc values of type struct. + * + * @return Value + * @throws \Error starting with php 8.0, this function should not be used, as it will always throw + * + * @deprecated iterate directly over the object using foreach instead + */ + public function structEach() + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return @each($this->me['struct']); + } + + /** + * Returns the m-th member of an xml-rpc value of array type. + * + * @param integer $key the index of the value to be retrieved (zero based) + * + * @return Value + * + * @deprecated use array access, e.g. $val[$key] + */ + public function arrayMem($key) + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return $this->me['array'][$key]; + } + + /** + * Returns the number of members in an xml-rpc value of array type. + * + * @return integer + * + * @deprecated use count() instead + */ + public function arraySize() + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return count($this->me['array']); + } + + /** + * Returns the number of members in an xml-rpc value of struct type. + * + * @return integer + * + * @deprecated use count() instead + */ + public function structSize() + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + return count($this->me['struct']); + } } -- 2.47.0