From 2d4f23af38795b70e17ca8863ec50c9e665cd17b Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Wed, 17 Dec 2014 20:47:57 -0500 Subject: [PATCH] various cleanups - added Scrutinizer - added EditorConfig - removed closing php tags - proper usage of require_once - cleaned extra spacing --- .editorconfig | 16 ++++++++++ .scrutinizer.yml | 13 ++++++++ lib/Unirest.php | 8 ++--- lib/Unirest/HttpMethod.php | 14 ++++----- lib/Unirest/HttpResponse.php | 5 +-- lib/Unirest/Unirest.php | 59 ++++++++++++++++-------------------- 6 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 .editorconfig create mode 100644 .scrutinizer.yml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..09459e5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +trim_trailing_whitespace = false diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..8edf04d --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,13 @@ +tools: + php_sim: true + php_pdepend: true + php_analyzer: true + php_mess_detector: true + php_changetracking: true + php_code_sniffer: true + php_cs_fixer: true + sensiolabs_security_checker: true + +filter: + excluded_paths: + - 'test/*' diff --git a/lib/Unirest.php b/lib/Unirest.php index ca05faa..49a8adf 100644 --- a/lib/Unirest.php +++ b/lib/Unirest.php @@ -1,7 +1,5 @@ \ No newline at end of file +require_once dirname(__FILE__) . '/Unirest/HttpMethod.php'; +require_once dirname(__FILE__) . '/Unirest/HttpResponse.php'; +require_once dirname(__FILE__) . '/Unirest/Unirest.php'; diff --git a/lib/Unirest/HttpMethod.php b/lib/Unirest/HttpMethod.php index 9252405..57d03b8 100644 --- a/lib/Unirest/HttpMethod.php +++ b/lib/Unirest/HttpMethod.php @@ -4,11 +4,9 @@ namespace Unirest; interface HttpMethod { - - const DELETE = "DELETE"; - const GET = "GET"; - const POST = "POST"; - const PUT = "PUT"; - const PATCH = "PATCH"; - -} \ No newline at end of file + const DELETE = 'DELETE'; + const GET = 'GET'; + const PATCH = 'PATCH'; + const POST = 'POST'; + const PUT = 'PUT'; +} diff --git a/lib/Unirest/HttpResponse.php b/lib/Unirest/HttpResponse.php index 93a78e8..0346064 100644 --- a/lib/Unirest/HttpResponse.php +++ b/lib/Unirest/HttpResponse.php @@ -23,7 +23,7 @@ class HttpResponse $this->body = $raw_body; $json = json_decode($raw_body, $json_decode_assoc); - if (json_last_error() == JSON_ERROR_NONE) { + if (json_last_error() === JSON_ERROR_NONE) { $this->body = $json; } } @@ -50,6 +50,7 @@ class HttpResponse if (property_exists($this, $property)) { $this->$property = $value; } + return $this; } @@ -61,6 +62,7 @@ class HttpResponse */ private function get_headers_from_curl_response($headers) { + $result = array(); $headers = explode("\r\n", $headers); array_shift($headers); @@ -73,5 +75,4 @@ class HttpResponse return $result; } - } diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index ff96d7f..b2f44b8 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -5,7 +5,6 @@ use Unirest\HttpResponse; class Unirest { - private static $verifyPeer = true; private static $socketTimeout = null; private static $defaultHeaders = array(); @@ -16,7 +15,7 @@ class Unirest */ public static function verifyPeer($enabled) { - Unirest::$verifyPeer = $enabled; + self::$verifyPeer = $enabled; } /** @@ -25,7 +24,7 @@ class Unirest */ public static function timeout($seconds) { - Unirest::$socketTimeout = $seconds; + self::$socketTimeout = $seconds; } /** @@ -35,7 +34,7 @@ class Unirest */ public static function defaultHeader($name, $value) { - Unirest::$defaultHeaders[$name] = $value; + self::$defaultHeaders[$name] = $value; } /** @@ -43,7 +42,7 @@ class Unirest */ public static function clearDefaultHeaders() { - Unirest::$defaultHeaders = array(); + self::$defaultHeaders = array(); } /** @@ -57,7 +56,7 @@ class Unirest */ public static function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL) { - return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password); + return self::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password); } /** @@ -71,7 +70,7 @@ class Unirest */ public static function post($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) { - return Unirest::request(HttpMethod::POST, $url, $body, $headers, $username, $password); + return self::request(HttpMethod::POST, $url, $body, $headers, $username, $password); } /** @@ -85,7 +84,7 @@ class Unirest */ public static function delete($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) { - return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password); + return self::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password); } /** @@ -99,7 +98,7 @@ class Unirest */ public static function put($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) { - return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password); + return self::request(HttpMethod::PUT, $url, $body, $headers, $username, $password); } /** @@ -113,7 +112,7 @@ class Unirest */ public static function patch($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) { - return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); + return self::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); } /** @@ -142,7 +141,7 @@ class Unirest foreach ($arrays AS $key => $value) { $k = isset($prefix) ? $prefix . '[' . $key . ']' : $key; if (!$value instanceof \CURLFile AND (is_array($value) OR is_object($value))) { - Unirest::http_build_query_for_curl($value, $new, $k); + self::http_build_query_for_curl($value, $new, $k); } else { $new[$k] = $value; } @@ -168,22 +167,26 @@ class Unirest $lowercaseHeaders = array(); $finalHeaders = array_merge($headers, Unirest::$defaultHeaders); foreach ($finalHeaders as $key => $val) { - $lowercaseHeaders[] = Unirest::getHeader($key, $val); + $lowercaseHeaders[] = self::getHeader($key, $val); } $lowerCaseFinalHeaders = array_change_key_case($finalHeaders); + if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) { $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; } + if (!array_key_exists("expect", $lowerCaseFinalHeaders)) { $lowercaseHeaders[] = "expect:"; } $ch = curl_init(); + if ($httpMethod != HttpMethod::GET) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + if (is_array($body) || $body instanceof Traversable) { - Unirest::http_build_query_for_curl($body, $postBody); + self::http_build_query_for_curl($body, $postBody); curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $body); @@ -194,21 +197,24 @@ class Unirest } else { $url .= "?"; } - Unirest::http_build_query_for_curl($body, $postBody); + + self::http_build_query_for_curl($body, $postBody); $url .= urldecode(http_build_query($postBody)); } - curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url)); + curl_setopt($ch, CURLOPT_URL, self::encodeUrl($url)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders); curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Unirest::$verifyPeer); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer); curl_setopt($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent. - if (Unirest::$socketTimeout != null) { - curl_setopt($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout); + + if (self::$socketTimeout != null) { + curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout); } + if (!empty($username)) { curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password)); } @@ -258,7 +264,7 @@ class Unirest $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); if ($query != null) { - $query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query'])); + $query = '?' . http_build_query(self::getArrayFromQuerystring($url_parsed['query'])); } if ($port && $port[0] != ":") @@ -290,7 +296,7 @@ if (!function_exists('http_chunked_decode')) { while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) { - if (!is_hex($chunkLenHex)) { + if (!ctype_xdigit($chunkLenHex)) { trigger_error('Value is not properly chunk encoded', E_USER_WARNING); return $chunk; } @@ -304,16 +310,3 @@ if (!function_exists('http_chunked_decode')) { return $dechunk; } } - -/** - * determine if a string can represent a number in hexadecimal - * @link http://uk1.php.net/ctype_xdigit - * @param string $hex - * @return boolean true if the string is a hex, otherwise false - */ -function is_hex($hex) -{ - return ctype_xdigit($hex); -} - -?>