WIP allow deprecations to be logged
authorgggeek <giunta.gaetano@gmail.com>
Mon, 30 Jan 2023 18:11:13 +0000 (18:11 +0000)
committergggeek <giunta.gaetano@gmail.com>
Mon, 30 Jan 2023 18:11:13 +0000 (18:11 +0000)
12 files changed:
NEWS.md
src/Client.php
src/Encoder.php
src/Helper/Charset.php
src/Helper/Logger.php
src/Helper/XMLParser.php
src/PhpXmlRpc.php
src/Request.php
src/Response.php
src/Server.php
src/Traits/DeprecationLogger.php [new file with mode: 0644]
src/Value.php

diff --git a/NEWS.md b/NEWS.md
index c05a664..2c711fe 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
     objects for the cases in which it previously returned a string
   - if you subclassed the `Client` class, take care of new static variables `$requestClass` and `$responseClass`,
     which should be used to instantiate requests and responses
-  - if you replaced the Logger class, take care that you will have to implement methods `error` and `debug` (all is ok
-    if you subclassed id)
+  - 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)
   - traits have been introduced for all classes dealing with Logger, XMLParser and CharsetEncoder; method `setCharsetEncoder`
     is now static
   - exception `\PhpXmlRpc\Exception\PhpXmlRpcException` is deprecated. Use `\PhpXmlRpc\Exception` instead
index e1230fd..da15e65 100644 (file)
@@ -5,14 +5,14 @@ namespace PhpXmlRpc;
 //use PhpXmlRpc\Helper\Charset;
 use PhpXmlRpc\Exception\ValueErrorException;
 use PhpXmlRpc\Helper\XMLParser;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 
 /**
  * Used to represent a client of an XML-RPC server.
  */
 class Client
 {
-    use LoggerAware;
+    use DeprecationLogger;
 
     const USE_CURL_NEVER = 0;
     const USE_CURL_ALWAYS = 1;
@@ -946,9 +946,9 @@ class Client
      */
     public function send($req, $timeout = 0, $method = '')
     {
-        //if ($method !== '' || $timeout !== 0) {
-        //    trigger_error("Using non-default values for arguments 'method' and 'timeout' when calling method " . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-        //}
+        if ($method !== '' || $timeout !== 0) {
+            $this->logDeprecation("Using non-default values for arguments 'method' and 'timeout' when calling method " . __METHOD__ . ' is deprecated');
+        }
 
         // if user does not specify http protocol, use native method of this client
         // (i.e. method set during call to constructor)
@@ -1054,7 +1054,7 @@ class Client
         $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1,
         $method='http')
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return $this->sendPayloadSocket($req, $server, $port, $timeout, $username, $password, $authType, null, null,
             null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, $method);
@@ -1090,7 +1090,7 @@ class Client
         $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '',
         $sslVersion = 0)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return $this->sendPayloadCURL($req, $server, $port, $timeout, $username,
             $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort,
index 0491447..b897af5 100644 (file)
@@ -335,7 +335,7 @@ class Encoder
         if ($xmlRpcParser->_xh['isf'] > 1) {
             // test that $xmlrpc->_xh['value'] is an obj, too???
 
-            $this->getLogger()->error($xmlRpcParser->_xh['isf_reason']);
+            $this->getLogger()->error('XML-RPC: ' . $xmlRpcParser->_xh['isf_reason']);
 
             return false;
         }
index 5488561..d683140 100644 (file)
@@ -4,14 +4,14 @@ namespace PhpXmlRpc\Helper;
 
 use PhpXmlRpc\Exception\ValueErrorException;
 use PhpXmlRpc\PhpXmlRpc;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 
 /**
  * @todo implement an interface
  */
 class Charset
 {
-    use LoggerAware;
+    use DeprecationLogger;
 
     // tables used for transcoding different charsets into us-ascii xml
     protected $xml_iso88591_Entities = array("in" => array(), "out" => array());
@@ -347,7 +347,7 @@ class Charset
      */
     public function isValidCharset($encoding, $validList)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         if (is_string($validList)) {
             $validList = explode(',', $validList);
@@ -377,7 +377,7 @@ class Charset
      */
     public function getEntities($charset)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         switch ($charset)
         {
index a4056e5..a934611 100644 (file)
@@ -42,6 +42,18 @@ class Logger
         }
     }
 
+    /**
+     * Following the general principle of 'never break stdout', the default behaviour
+     *
+     * @param string $message
+     * @param $context
+     * @return void
+     */
+    public function warning($message, $context = array())
+    {
+        $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message));
+    }
+
     /**
      * Triggers the writing of a message to php's error log
      *
@@ -51,7 +63,7 @@ class Logger
      */
     public function error($message, $context = array())
     {
-        $this->errorLog($message);
+        $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message));
     }
 
     // BC interface
index 839ffd2..4e2d187 100644 (file)
@@ -3,7 +3,7 @@
 namespace PhpXmlRpc\Helper;
 
 use PhpXmlRpc\PhpXmlRpc;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 use PhpXmlRpc\Value;
 
 /**
@@ -20,7 +20,7 @@ use PhpXmlRpc\Value;
  */
 class XMLParser
 {
-    use LoggerAware;
+    use DeprecationLogger;
 
     const RETURN_XMLRPCVALS = 'xmlrpcvals';
     const RETURN_EPIVALS = 'epivals';
@@ -1012,14 +1012,14 @@ class XMLParser
 
     public function __set($name, $value)
     {
-        //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
-
         switch ($name) {
             // this should only ever be called by subclasses which overtook `parse()`
             case 'accept':
+                $this->logDeprecation('Setting property XMLParser::' . $name . ' is deprecated');
                 $this->current_parsing_options['accept'] = $value;
                 break;
             default:
+                /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
                 trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
         }
@@ -1027,10 +1027,9 @@ class XMLParser
 
     public function __isset($name)
     {
-        //trigger_error('checking property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
-
         switch ($name) {
             case 'accept':
+                $this->logDeprecation('Checking property XMLParser::' . $name . ' is deprecated');
                 return isset($this->current_parsing_options['accept']);
             default:
                 return false;
@@ -1042,9 +1041,11 @@ class XMLParser
         switch ($name) {
             // q: does this make sense at all?
             case 'accept':
+                $this->logDeprecation('Unsetting property XMLParser::' . $name . ' is deprecated');
                 unset($this->current_parsing_options['accept']);
                 break;
             default:
+                /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
                 trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
         }
index 66d9513..e8b8035 100644 (file)
@@ -93,7 +93,6 @@ class PhpXmlRpc
      * received charset cannot be determined and mbstring extension is not enabled.
      */
     public static $xmlrpc_defencoding = "UTF-8";
-
     /**
      * @var string[]
      * The list of preferred encodings used by the server for requests and by the client for responses to detect the
@@ -102,7 +101,6 @@ class PhpXmlRpc
      * - mbstring extension is enabled
      */
     public static $xmlrpc_detectencodings = array();
-
     /**
      * @var string
      * The encoding used internally by PHP.
@@ -209,10 +207,18 @@ class PhpXmlRpc
      */
     public static $xmlrpc_methodname_format = '|^[ \t]*[a-zA-Z0-9_.:/]+[ \t]*$|';
 
+    /**
+     * @var bool
+     * Set this to false to have a warning added to the log whenever user code uses a deprecated method/parameter/property
+     */
+    public static $xmlrpc_silence_deprecations = true;
+
     /**
      * A function to be used for compatibility with legacy code: it creates all global variables which used to be declared,
      * such as library version etc...
      * @return void
+     *
+     * @deprecated
      */
     public static function exportGlobals()
     {
@@ -255,6 +261,8 @@ class PhpXmlRpc
      * 4. run your own code.
      *
      * @return void
+     *
+     * @deprecated
      */
     public static function importGlobals()
     {
index 4a68454..2dbcf18 100644 (file)
@@ -6,7 +6,7 @@ use PhpXmlRpc\Exception\HttpException;
 use PhpXmlRpc\Helper\Http;
 use PhpXmlRpc\Helper\XMLParser;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 use PhpXmlRpc\Traits\ParserAware;
 
 /**
@@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\ParserAware;
 class Request
 {
     use CharsetEncoderAware;
-    use LoggerAware;
+    use DeprecationLogger;
     use ParserAware;
 
     /// @todo: do these need to be public?
index 210f8b5..d577bea 100644 (file)
@@ -4,7 +4,7 @@ namespace PhpXmlRpc;
 
 use PhpXmlRpc\Exception\StateErrorException;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 
 /**
  * This class provides the representation of the response of an XML-RPC server.
@@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\LoggerAware;
 class Response
 {
     use CharsetEncoderAware;
-    use LoggerAware;
+    use DeprecationLogger;
 
     /// @todo: do these need to be public?
     /** @internal */
@@ -194,16 +194,18 @@ class Response
     // we have to make this return by ref in order to allow calls such as `$resp->_cookies['name'] = ['value' => 'something'];`
     public function &__get($name)
     {
-        //trigger_error('getting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
-
         switch ($name) {
             case 'hdrs':
+                $this->logDeprecation('Getting property Response::' . $name . ' is deprecated');
                 return $this->httpResponse['headers'];
             case '_cookies':
+                $this->logDeprecation('Getting property Response::' . $name . ' is deprecated');
                 return $this->httpResponse['cookies'];
             case 'raw_data':
+                $this->logDeprecation('Getting property Response::' . $name . ' is deprecated');
                 return $this->httpResponse['raw_data'];
             default:
+                /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
                 trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
                 return null;
@@ -212,19 +214,21 @@ class Response
 
     public function __set($name, $value)
     {
-        //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
-
         switch ($name) {
             case 'hdrs':
+                $this->logDeprecation('Setting property Response::' . $name . ' is deprecated');
                 $this->httpResponse['headers'] = $value;
                 break;
             case '_cookies':
+                $this->logDeprecation('Setting property Response::' . $name . ' is deprecated');
                 $this->httpResponse['cookies'] = $value;
                 break;
             case 'raw_data':
+                $this->logDeprecation('Setting property Response::' . $name . ' is deprecated');
                 $this->httpResponse['raw_data'] = $value;
                 break;
             default:
+                /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
                 trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
         }
@@ -232,14 +236,15 @@ class Response
 
     public function __isset($name)
     {
-        //trigger_error('checking property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
-
         switch ($name) {
             case 'hdrs':
+                $this->logDeprecation('Checking property Response::' . $name . ' is deprecated');
                 return isset($this->httpResponse['headers']);
             case '_cookies':
+                $this->logDeprecation('Checking property Response::' . $name . ' is deprecated');
                 return isset($this->httpResponse['cookies']);
             case 'raw_data':
+                $this->logDeprecation('Checking property Response::' . $name . ' is deprecated');
                 return isset($this->httpResponse['raw_data']);
             default:
                 return false;
@@ -250,15 +255,19 @@ class Response
     {
         switch ($name) {
             case 'hdrs':
+                $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated');
                 unset($this->httpResponse['headers']);
                 break;
             case '_cookies':
+                $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated');
                 unset($this->httpResponse['cookies']);
                 break;
             case 'raw_data':
+                $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated');
                 unset($this->httpResponse['raw_data']);
                 break;
             default:
+                /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
                 trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
         }
index a9f35fa..85fbaa4 100644 (file)
@@ -9,7 +9,7 @@ use PhpXmlRpc\Helper\Interop;
 use PhpXmlRpc\Helper\Logger;
 use PhpXmlRpc\Helper\XMLParser;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 use PhpXmlRpc\Traits\ParserAware;
 
 /**
@@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\ParserAware;
 class Server
 {
     use CharsetEncoderAware;
-    use LoggerAware;
+    use DeprecationLogger;
     use ParserAware;
 
     const OPT_ACCEPTED_COMPRESSION = 'accepted_compression';
diff --git a/src/Traits/DeprecationLogger.php b/src/Traits/DeprecationLogger.php
new file mode 100644 (file)
index 0000000..a499789
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace PhpXmlRpc\Traits;
+
+use PhpXmlRpc\PhpXmlRpc;
+
+trait DeprecationLogger
+{
+    use LoggerAware;
+
+    protected function logDeprecation($message)
+    {
+        if (PhpXmlRpc::$xmlrpc_silence_deprecations) {
+            return;
+        }
+
+        $this->getLogger()->warning('XML-RPC Deprecated: ' . $message);
+    }
+}
index 1116797..9ec2cfc 100644 (file)
@@ -6,7 +6,7 @@ use PhpXmlRpc\Exception\StateErrorException;
 use PhpXmlRpc\Exception\TypeErrorException;
 use PhpXmlRpc\Exception\ValueErrorException;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
-use PhpXmlRpc\Traits\LoggerAware;
+use PhpXmlRpc\Traits\DeprecationLogger;
 
 /**
  * This class enables the creation of values for XML-RPC, by encapsulating plain php values.
@@ -14,7 +14,7 @@ use PhpXmlRpc\Traits\LoggerAware;
 class Value implements \Countable, \IteratorAggregate, \ArrayAccess
 {
     use CharsetEncoderAware;
-    use LoggerAware;
+    use DeprecationLogger;
 
     public static $xmlrpcI4 = "i4";
     public static $xmlrpcI8 = "i8";
@@ -373,7 +373,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function structMemExists($key)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return array_key_exists($key, $this->me['struct']);
     }
@@ -389,7 +389,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function structMem($key)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return $this->me['struct'][$key];
     }
@@ -402,7 +402,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function structReset()
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         reset($this->me['struct']);
     }
@@ -417,7 +417,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function structEach()
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return @each($this->me['struct']);
     }
@@ -462,7 +462,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function arrayMem($key)
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return $this->me['array'][$key];
     }
@@ -476,7 +476,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function arraySize()
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return count($this->me['array']);
     }
@@ -490,7 +490,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
      */
     public function structSize()
     {
-        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
 
         return count($this->me['struct']);
     }