From 86fa973d171b566ed697924863d46a4d9a78fc85 Mon Sep 17 00:00:00 2001 From: Marcus Bointon Date: Wed, 19 Oct 2016 10:57:37 +0200 Subject: [PATCH] PSR-2 coding standard changes from php-cs-fixer, see #46 --- examples/partner.php | 9 ++--- examples/public.php | 21 ++++++++---- generator/objects/API.php | 6 ---- generator/objects/ParsedObjectInterface.php | 2 +- src/XeroPHP/Application.php | 8 +++-- src/XeroPHP/Helpers.php | 4 +-- src/XeroPHP/Remote/Exception.php | 2 +- src/XeroPHP/Remote/OAuth/Client.php | 33 +++++++++++++++---- src/XeroPHP/Remote/OAuth/Exception.php | 2 +- .../Remote/OAuth/SignatureMethod/HMACSHA1.php | 2 +- src/XeroPHP/Remote/Object.php | 28 +++++++++++----- src/XeroPHP/Remote/ObjectInterface.php | 14 ++++---- src/XeroPHP/Remote/Response.php | 9 +++-- src/XeroPHP/Traits/AttachmentTrait.php | 11 +++++-- tests/Application/PartnerApplicationTest.php | 2 +- tests/Application/PrivateApplicationTest.php | 2 +- tests/Application/PublicApplicationTest.php | 2 +- tests/ApplicationTest.php | 2 +- tests/Remote/ObjectTest.php | 16 +++++---- 19 files changed, 109 insertions(+), 66 deletions(-) diff --git a/examples/partner.php b/examples/partner.php index c5632f20..cbf59e38 100644 --- a/examples/partner.php +++ b/examples/partner.php @@ -30,7 +30,6 @@ // If no session found if ($oauth_session === null) { - $url = new URL($xero, URL::OAUTH_REQUEST_TOKEN); $request = new Request($xero, $url); @@ -53,7 +52,6 @@ printf('Click here to Authorize', $xero->getAuthorizeURL($oauth_response['oauth_token'])); exit; - } elseif (isset($oauth_session['session_handle']) && !isset($oauth_session['expires'])) { // If session is expired refresh the token $url = new URL($xero, URL::OAUTH_ACCESS_TOKEN); @@ -77,9 +75,7 @@ $xero->getOAuthClient() ->setToken($oauth_response['oauth_token']) ->setTokenSecret($oauth_response['oauth_token_secret']); - } else { - $xero->getOAuthClient() ->setToken($oauth_session['token']) ->setTokenSecret($oauth_session['token_secret']); @@ -108,7 +104,6 @@ //Just for demo purposes header(sprintf('Location: http%s://%s%s', (isset($_SERVER['HTTPS']) ? 's' : ''), $_SERVER['HTTP_HOST'], $uri_parts[0])); exit; - } } @@ -135,7 +130,9 @@ function getOAuthSession() } // If the session is expired or expiring, unset the expires key - if ($_SESSION['oauth']['expires'] !== null && $_SESSION['oauth']['expires'] <= (time() + 100)) { + if ($_SESSION['oauth']['expires'] !== null + && $_SESSION['oauth']['expires'] <= (time() + 100) + ) { unset($_SESSION['oauth']['expires']); } diff --git a/examples/public.php b/examples/public.php index 8bf1a965..9cb55532 100644 --- a/examples/public.php +++ b/examples/public.php @@ -44,9 +44,11 @@ $oauth_response['oauth_token_secret'] ); - printf('Click here to Authorize', $xero->getAuthorizeURL($oauth_response['oauth_token'])); + printf( + 'Click here to Authorize', + $xero->getAuthorizeURL($oauth_response['oauth_token']) + ); exit; - } else { $xero->getOAuthClient() ->setToken($oauth_session['token']) @@ -71,7 +73,14 @@ $uri_parts = explode('?', $_SERVER['REQUEST_URI']); //Just for demo purposes - header(sprintf('Location: http%s://%s%s', (isset($_SERVER['HTTPS']) ? 's' : ''), $_SERVER['HTTP_HOST'], $uri_parts[0])); + header( + sprintf( + 'Location: http%s://%s%s', + (isset($_SERVER['HTTPS']) ? 's' : ''), + $_SERVER['HTTP_HOST'], + $uri_parts[0] + ) + ); exit; } } @@ -98,9 +107,9 @@ function setOAuthSession($token, $secret, $expires = null) function getOAuthSession() { //If it doesn't exist or is expired, return null - if (!isset($_SESSION['oauth']) || - ($_SESSION['oauth']['expires'] !== null && - $_SESSION['oauth']['expires'] <= time()) + if (!isset($_SESSION['oauth']) + || ($_SESSION['oauth']['expires'] !== null + && $_SESSION['oauth']['expires'] <= time()) ) { return null; } diff --git a/generator/objects/API.php b/generator/objects/API.php index 5a7dba9e..5a7e0f28 100644 --- a/generator/objects/API.php +++ b/generator/objects/API.php @@ -241,22 +241,16 @@ public function searchByKey($key, $namespace_hint = '') if (isset($this->search_keys[$ns_key])) { return $this->search_keys[$ns_key]; - } elseif (isset($this->search_keys[$plural_ns_key])) { return $this->search_keys[$plural_ns_key]; - } elseif (isset($this->search_keys[$singular_ns_key])) { return $this->search_keys[$singular_ns_key]; - } elseif (isset($this->search_keys[$key])) { return $this->search_keys[$key]; - } elseif (isset($this->search_keys[$plural_key])) { return $this->search_keys[$plural_key]; - } elseif (isset($this->search_keys[$singular_key])) { return $this->search_keys[$singular_key]; - } else { return null; } diff --git a/generator/objects/ParsedObjectInterface.php b/generator/objects/ParsedObjectInterface.php index 3bf65b30..3373ab4f 100644 --- a/generator/objects/ParsedObjectInterface.php +++ b/generator/objects/ParsedObjectInterface.php @@ -4,4 +4,4 @@ interface ParsedObjectInterface { public function getName(); public function setName($name); -} \ No newline at end of file +} diff --git a/src/XeroPHP/Application.php b/src/XeroPHP/Application.php index 1cf055a1..3a944b0c 100644 --- a/src/XeroPHP/Application.php +++ b/src/XeroPHP/Application.php @@ -9,7 +9,6 @@ use XeroPHP\Remote\Request; use XeroPHP\Remote\URL; - abstract class Application { protected static $_config_defaults = [ @@ -335,7 +334,12 @@ private function savePropertiesDirectly(Remote\Object $object) public function delete(Remote\Object $object) { if (!$object::supportsMethod(Request::METHOD_DELETE)) { - throw new Exception(sprintf('%s doesn\'t support [DELETE] via the API', get_class($object))); + throw new Exception( + sprintf( + '%s doesn\'t support [DELETE] via the API', + get_class($object) + ) + ); } $uri = sprintf('%s/%s', $object::getResourceURI(), $object->getGUID()); diff --git a/src/XeroPHP/Helpers.php b/src/XeroPHP/Helpers.php index 8c5710c3..0429b857 100644 --- a/src/XeroPHP/Helpers.php +++ b/src/XeroPHP/Helpers.php @@ -37,7 +37,6 @@ public static function arrayToXML(array $array, $key_override = null) $element = self::arrayToXML($element, self::singularize($key)); } } - } else { //Element escaping for the http://www.w3.org/TR/REC-xml/#sec-predefined-ent //Full DOMDocument not really necessary as we don't use attributes (which are more strict) @@ -90,7 +89,8 @@ public static function XMLToArray(\SimpleXMLElement $sxml) /** * This function is based on Wave\Inflector::singularize(). - * It only contains a fraction of the rules from its predecessor, so only good for a quick basic singularisation. + * It only contains a fraction of the rules from its predecessor, + * so only good for a quick basic singularisation. * * @param $string * @return mixed diff --git a/src/XeroPHP/Remote/Exception.php b/src/XeroPHP/Remote/Exception.php index 53f2a2da..102390b8 100644 --- a/src/XeroPHP/Remote/Exception.php +++ b/src/XeroPHP/Remote/Exception.php @@ -4,4 +4,4 @@ class Exception extends \XeroPHP\Exception { -} \ No newline at end of file +} diff --git a/src/XeroPHP/Remote/OAuth/Client.php b/src/XeroPHP/Remote/OAuth/Client.php index 5afcadce..c181f8a1 100644 --- a/src/XeroPHP/Remote/OAuth/Client.php +++ b/src/XeroPHP/Remote/OAuth/Client.php @@ -87,7 +87,8 @@ public function sign(Request $request) throw new Exception('Invalid signing location specified.'); } - //Reset so this instance of the Client can sign subsequent requests. Mainly for the nonce. + //Reset so this instance of the Client can sign subsequent requests. + //Mainly for the nonce. $this->resetOAuthParams(); } @@ -143,16 +144,29 @@ private function getSignature() { switch ($this->getSignatureMethod()) { case self::SIGNATURE_RSA_SHA1: - $signature = RSASHA1::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret()); + $signature = RSASHA1::generateSignature( + $this->config, + $this->getSBS(), + $this->getSigningSecret() + ); break; case self::SIGNATURE_HMAC_SHA1: - $signature = HMACSHA1::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret()); + $signature = HMACSHA1::generateSignature( + $this->config, + $this->getSBS(), + $this->getSigningSecret() + ); break; case self::SIGNATURE_PLAINTEXT: - $signature = PLAINTEXT::generateSignature($this->config, $this->getSBS(), $this->getSigningSecret()); + $signature = PLAINTEXT::generateSignature( + $this->config, $this->getSBS(), + $this->getSigningSecret() + ); break; default: - throw new Exception("Invalid signature method [{$this->config['signature_method']}]"); + throw new Exception( + "Invalid signature method [{$this->config['signature_method']}]" + ); } return $signature; @@ -179,7 +193,12 @@ public function getSBS() $url = $this->request->getUrl()->getFullURL(); //Every second thing seems to need escaping! - return sprintf('%s&%s&%s', $this->request->getMethod(), Helpers::escape($url), Helpers::escape($sbs_string)); + return sprintf( + '%s&%s&%s', + $this->request->getMethod(), + Helpers::escape($url), + Helpers::escape($sbs_string) + ); } /** @@ -212,7 +231,7 @@ private function getNonce($length = 20) $parts = explode('.', microtime(true)); $nonce = base_convert($parts[1], 10, 36); - for($i = 0; $i < $length - strlen($nonce); $i++) { + for ($i = 0; $i < $length - strlen($nonce); $i++) { $nonce .= base_convert(mt_rand(0, 35), 10, 36); } diff --git a/src/XeroPHP/Remote/OAuth/Exception.php b/src/XeroPHP/Remote/OAuth/Exception.php index 88c4291f..92ba4abf 100644 --- a/src/XeroPHP/Remote/OAuth/Exception.php +++ b/src/XeroPHP/Remote/OAuth/Exception.php @@ -4,4 +4,4 @@ class Exception extends \XeroPHP\Exception { -} \ No newline at end of file +} diff --git a/src/XeroPHP/Remote/OAuth/SignatureMethod/HMACSHA1.php b/src/XeroPHP/Remote/OAuth/SignatureMethod/HMACSHA1.php index 53fad410..865b5b2c 100644 --- a/src/XeroPHP/Remote/OAuth/SignatureMethod/HMACSHA1.php +++ b/src/XeroPHP/Remote/OAuth/SignatureMethod/HMACSHA1.php @@ -8,4 +8,4 @@ public static function generateSignature(array $config, $sbs, $secret) { return base64_encode(hash_hmac('sha1', $sbs, $secret, true)); } -} \ No newline at end of file +} diff --git a/src/XeroPHP/Remote/Object.php b/src/XeroPHP/Remote/Object.php index e994b1bc..ce0591b5 100644 --- a/src/XeroPHP/Remote/Object.php +++ b/src/XeroPHP/Remote/Object.php @@ -58,7 +58,8 @@ abstract class Object implements ObjectInterface, \JsonSerializable, \ArrayAcces protected $_associated_objects; /** - * Holds a ref to the application that was used to load the object, enables shorthand $object->save(); + * Holds a ref to the application that was used to load the object, + * enables shorthand $object->save(); * * @var Application $_application */ @@ -73,7 +74,8 @@ public function __construct(Application $application = null) } /** - * This should be compulsory in the constructor int he future, but will have to be like this for BC until the next major version. + * This should be compulsory in the constructor int he future, + * but will have to be like this for BC until the next major version. * * @param Application $application */ @@ -177,7 +179,8 @@ public function fromStringArray($input_array, $replace_data = false) continue; } - //Fix for an earlier assumption that the API didn't return more than two levels of nested objects. + //Fix for an earlier assumption that the API didn't return more than + //two levels of nested objects. //Handles Invoice > Contact > Address etc. in one build. if (is_array($input_array[$property]) && Helpers::isAssoc($input_array[$property]) === false) { $collection = new Collection(); @@ -225,7 +228,6 @@ public function toStringArray() } else { $out[$property] = self::castToString($type, $this->_data[$property]); } - } return $out; } @@ -337,8 +339,13 @@ public function validate($check_children = true) if ($mandatory) { if (!isset($this->_data[$property]) || empty($this->_data[$property])) { - throw new Exception(sprintf('%s::$%s is mandatory and is either missing or empty.', - get_class($this), $property)); + throw new Exception( + sprintf( + '%s::$%s is mandatory and is either missing or empty.', + get_class($this), + $property + ) + ); } if ($check_children) { @@ -347,7 +354,6 @@ public function validate($check_children = true) /** @var self $obj */ $obj = $this->_data[$property]; $obj->validate(); - } elseif ($this->_data[$property] instanceof Collection) { foreach ($this->_data[$property] as $element) { if ($element instanceof Object) { @@ -371,7 +377,9 @@ public function validate($check_children = true) public function save() { if ($this->_application === null) { - throw new Exception('->save() is only available on objects that have an injected application context.'); + throw new Exception( + '->save() is only available on objects that have an injected application context.' + ); } $this->_application->save($this); } @@ -384,7 +392,9 @@ public function save() public function delete() { if ($this->_application === null) { - throw new Exception('->delete() is only available on objects that have an injected application context.'); + throw new Exception( + '->delete() is only available on objects that have an injected application context.' + ); } $this->_application->delete($this); } diff --git a/src/XeroPHP/Remote/ObjectInterface.php b/src/XeroPHP/Remote/ObjectInterface.php index 72b5bbc2..898eb8f6 100644 --- a/src/XeroPHP/Remote/ObjectInterface.php +++ b/src/XeroPHP/Remote/ObjectInterface.php @@ -10,32 +10,32 @@ interface ObjectInterface * * @return string|null */ - static function getGUIDProperty(); + public static function getGUIDProperty(); /** * Get a list of properties * * @return array */ - static function getProperties(); + public static function getProperties(); /** * Get a list of the supported HTTP Methods * * @return array */ - static function getSupportedMethods(); + public static function getSupportedMethods(); /** * return the URI of the resource (if any) * * @return string */ - static function getResourceURI(); + public static function getResourceURI(); - static function getRootNodeName(); + public static function getRootNodeName(); - static function getAPIStem(); + public static function getAPIStem(); - static function isPageable(); + public static function isPageable(); } diff --git a/src/XeroPHP/Remote/Response.php b/src/XeroPHP/Remote/Response.php index a5f72b03..db3c0571 100644 --- a/src/XeroPHP/Remote/Response.php +++ b/src/XeroPHP/Remote/Response.php @@ -209,14 +209,16 @@ public function findElementErrors($element, $element_index) switch ((string) $property) { case 'ValidationErrors': if (is_array($value)) { - foreach ($value as $error) + foreach ($value as $error) { $this->element_errors[$element_index] = trim($error['Message'], '.'); + } } break; case 'Warnings': if (is_array($value)) { - foreach ($value as $warning) + foreach ($value as $warning) { $this->element_warnings[$element_index] = trim($warning['Message'], '.'); + } } break; @@ -284,7 +286,8 @@ public function parseJSON() break; default: - //Happy to make the assumption that there will only be one root node with > than 2D children. + //Happy to make the assumption that there will only be one + //root node with > than 2D children. if (is_array($root_child)) { foreach ($root_child as $element) { $this->elements[] = $element; diff --git a/src/XeroPHP/Traits/AttachmentTrait.php b/src/XeroPHP/Traits/AttachmentTrait.php index 29a9f57d..d4bab64f 100644 --- a/src/XeroPHP/Traits/AttachmentTrait.php +++ b/src/XeroPHP/Traits/AttachmentTrait.php @@ -9,7 +9,6 @@ trait AttachmentTrait { - public function addAttachment(Attachment $attachment) { /** @@ -38,10 +37,16 @@ public function getAttachments() * @var Object $this */ if ($this->hasGUID() === false) { - throw new Exception('Attachments are only available to objects that exist remotely.'); + throw new Exception( + 'Attachments are only available to objects that exist remotely.' + ); } - $uri = sprintf('%s/%s/Attachments', $this::getResourceURI(), $this->getGUID()); + $uri = sprintf( + '%s/%s/Attachments', + $this::getResourceURI(), + $this->getGUID() + ); $url = new URL($this->_application, $uri); $request = new Request($this->_application, $url, Request::METHOD_GET); diff --git a/tests/Application/PartnerApplicationTest.php b/tests/Application/PartnerApplicationTest.php index fc672b20..3cf7a147 100644 --- a/tests/Application/PartnerApplicationTest.php +++ b/tests/Application/PartnerApplicationTest.php @@ -1,6 +1,6 @@ _data['test'] = $test; } - static function getRootNodeName() { + public static function getRootNodeName() + { return 'test'; } }