From a6e94ae8bd00f9b032978d285ab62a21c046725d Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Thu, 27 Apr 2017 02:41:45 -0700 Subject: [PATCH 1/3] Remove use of anonymous functions for json decoder and xml decoder --- src/Curl/Curl.php | 89 +++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 7bf0aa1..9d6d0c0 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,6 +4,45 @@ namespace Curl; use Curl\ArrayUtil; + +/** + * Decode JSON + * + * @param $json + * @param $assoc + * @param $depth + * @param $options + */ +function decodeJson() { + $args = func_get_args(); + + // Call json_decode() without the $options parameter in PHP + // versions less than 5.4.0 as the $options parameter was added in + // PHP version 5.4.0. + if (version_compare(PHP_VERSION, '5.4.0', '<')) { + $args = array_slice($args, 0, 3); + } + + $response = call_user_func_array('json_decode', $args); + if ($response === null) { + $response = $args['0']; + } + return $response; +} + +/** + * Decode XML + * + * @param $response + */ +function decodeXml($response) { + $xml_obj = @simplexml_load_string($response); + if (!($xml_obj === false)) { + $response = $xml_obj; + } + return $response; +} + class Curl { const VERSION = '7.2.0'; @@ -44,9 +83,10 @@ class Curl private $headers = array(); private $options = array(); - private $jsonDecoder = null; + private $jsonDecoder = '\Curl\decodeJson'; + private $jsonDecoderArgs = array(); private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; - private $xmlDecoder = null; + private $xmlDecoder = '\Curl\decodeXml'; private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i'; private $defaultDecoder = null; @@ -106,8 +146,6 @@ class Curl $this->curl = curl_init(); $this->id = uniqid('', true); $this->setDefaultUserAgent(); - $this->setDefaultJsonDecoder(); - $this->setDefaultXmlDecoder(); $this->setDefaultTimeout(); $this->setOpt(CURLINFO_HEADER_OUT, true); $this->setOpt(CURLOPT_HEADERFUNCTION, array($this, 'headerCallback')); @@ -205,6 +243,7 @@ class Curl } $this->options = null; $this->jsonDecoder = null; + $this->jsonDecoderArgs = null; $this->xmlDecoder = null; $this->defaultDecoder = null; } @@ -886,23 +925,8 @@ class Curl */ public function setDefaultJsonDecoder() { - $args = func_get_args(); - $this->jsonDecoder = function ($response) use ($args) { - array_unshift($args, $response); - - // Call json_decode() without the $options parameter in PHP - // versions less than 5.4.0 as the $options parameter was added in - // PHP version 5.4.0. - if (version_compare(PHP_VERSION, '5.4.0', '<')) { - $args = array_slice($args, 0, 3); - } - - $json_obj = call_user_func_array('json_decode', $args); - if (!($json_obj === null)) { - $response = $json_obj; - } - return $response; - }; + $this->jsonDecoder = '\Curl\decodeJson'; + $this->jsonDecoderArgs = func_get_args(); } /** @@ -912,13 +936,7 @@ class Curl */ public function setDefaultXmlDecoder() { - $this->xmlDecoder = function ($response) { - $xml_obj = @simplexml_load_string($response); - if (!($xml_obj === false)) { - $response = $xml_obj; - } - return $response; - }; + $this->xmlDecoder = '\Curl\decodeXml'; } /** @@ -1014,6 +1032,7 @@ class Curl { if (is_callable($function)) { $this->jsonDecoder = $function; + $this->jsonDecoderArgs = func_get_args(); } } @@ -1342,18 +1361,20 @@ class Curl if (isset($response_headers['Content-Type'])) { if (preg_match($this->jsonPattern, $response_headers['Content-Type'])) { $json_decoder = $this->jsonDecoder; - if (is_callable($json_decoder)) { - $response = $json_decoder($response); + if ($this->jsonDecoder) { + $args = $this->jsonDecoderArgs; + array_unshift($args, $response); + $response = call_user_func_array($json_decoder, $args); } } elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) { $xml_decoder = $this->xmlDecoder; - if (is_callable($xml_decoder)) { + if ($xml_decoder) { $response = $xml_decoder($response); } } else { - $decoder = $this->defaultDecoder; - if (is_callable($decoder)) { - $response = $decoder($response); + $default_decoder = $this->defaultDecoder; + if ($default_decoder) { + $response = $default_decoder($response); } } } From 97319a51f886dc95123aaa9c56a209a9db20bc24 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Thu, 27 Apr 2017 23:51:21 -0700 Subject: [PATCH 2/3] Move decoder functions to separate class file --- src/Curl/Curl.php | 48 +++++--------------------------------------- src/Curl/Decoder.php | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 src/Curl/Decoder.php diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 9d6d0c0..ffcf71b 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -3,45 +3,7 @@ namespace Curl; use Curl\ArrayUtil; - - -/** - * Decode JSON - * - * @param $json - * @param $assoc - * @param $depth - * @param $options - */ -function decodeJson() { - $args = func_get_args(); - - // Call json_decode() without the $options parameter in PHP - // versions less than 5.4.0 as the $options parameter was added in - // PHP version 5.4.0. - if (version_compare(PHP_VERSION, '5.4.0', '<')) { - $args = array_slice($args, 0, 3); - } - - $response = call_user_func_array('json_decode', $args); - if ($response === null) { - $response = $args['0']; - } - return $response; -} - -/** - * Decode XML - * - * @param $response - */ -function decodeXml($response) { - $xml_obj = @simplexml_load_string($response); - if (!($xml_obj === false)) { - $response = $xml_obj; - } - return $response; -} +use Curl\Decoder; class Curl { @@ -83,10 +45,10 @@ class Curl private $headers = array(); private $options = array(); - private $jsonDecoder = '\Curl\decodeJson'; + private $jsonDecoder = '\Curl\Decoder::decodeJson'; private $jsonDecoderArgs = array(); private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; - private $xmlDecoder = '\Curl\decodeXml'; + private $xmlDecoder = '\Curl\Decoder::decodeXml'; private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i'; private $defaultDecoder = null; @@ -925,7 +887,7 @@ class Curl */ public function setDefaultJsonDecoder() { - $this->jsonDecoder = '\Curl\decodeJson'; + $this->jsonDecoder = '\Curl\Decoder::decodeJson'; $this->jsonDecoderArgs = func_get_args(); } @@ -936,7 +898,7 @@ class Curl */ public function setDefaultXmlDecoder() { - $this->xmlDecoder = '\Curl\decodeXml'; + $this->xmlDecoder = '\Curl\Decoder::decodeXml'; } /** diff --git a/src/Curl/Decoder.php b/src/Curl/Decoder.php new file mode 100644 index 0000000..60925be --- /dev/null +++ b/src/Curl/Decoder.php @@ -0,0 +1,46 @@ + Date: Fri, 28 Apr 2017 00:40:00 -0700 Subject: [PATCH 3/3] Replace variable function calls with call_user_func(). Fixes "Call to undefined function \Curl\Decoder::decodeXml()" in PHP 5.3, 5.4, 5.5, 5.6, hhvm. --- src/Curl/Curl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index ffcf71b..82d194c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -1331,12 +1331,12 @@ class Curl } elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) { $xml_decoder = $this->xmlDecoder; if ($xml_decoder) { - $response = $xml_decoder($response); + $response = call_user_func($xml_decoder, $response); } } else { $default_decoder = $this->defaultDecoder; if ($default_decoder) { - $response = $default_decoder($response); + $response = call_user_func($default_decoder, $response); } } }