From b9f33c20258a329db5e00c43bb75cc4c54cc62e7 Mon Sep 17 00:00:00 2001 From: lsolesen Date: Sun, 13 Mar 2016 19:24:22 +0000 Subject: [PATCH] Fixed a bunch of coding standard issues after running PHP CodeSniffer --- QuickPay/API/Client.php | 97 +++++----- QuickPay/API/Constants.php | 14 +- QuickPay/API/Exception.php | 34 ++-- QuickPay/API/Request.php | 255 +++++++++++++-------------- QuickPay/API/Response.php | 170 +++++++++--------- QuickPay/QuickPay.php | 29 +-- QuickPay/Tests/api/ExceptionTest.php | 6 +- QuickPay/Tests/api/RequestTest.php | 4 +- QuickPay/Tests/api/ResponseTest.php | 42 ++--- composer.json | 3 +- 10 files changed, 317 insertions(+), 337 deletions(-) diff --git a/QuickPay/API/Client.php b/QuickPay/API/Client.php index 7c45452..1874a78 100644 --- a/QuickPay/API/Client.php +++ b/QuickPay/API/Client.php @@ -1,85 +1,82 @@ auth_string = $auth_string; - + // Instantiate cURL object $this->authenticate(); } - - /** - * shutdown function. - * - * Closes the current cURL connection - * - * @access public - */ + + /** + * Shutdown function. + * + * Closes the current cURL connection + * + * @access public + */ public function shutdown() { - if( ! empty( $this->ch ) ) - { - curl_close( $this->ch ); + if (!empty($this->ch) ) { + curl_close($this->ch); } } - - - /** - * authenticate function. - * - * Create a cURL instance with authentication headers - * - * @access public - */ + + /** + * authenticate function. + * + * Create a cURL instance with authentication headers + * + * @access public + */ protected function authenticate() { $this->ch = curl_init(); - + $headers = array( 'Accept-Version: v10', 'Accept: application/json', ); - if( ! empty($this->auth_string)) - { - $headers[] = 'Authorization: Basic ' . base64_encode( $this->auth_string ); + if (!empty($this->auth_string)) { + $headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string); } $options = array( @@ -87,8 +84,8 @@ protected function authenticate() CURLOPT_SSL_VERIFYPEER => true, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_HTTPHEADER => $headers - ); - - curl_setopt_array( $this->ch, $options ); + ); + + curl_setopt_array($this->ch, $options); } } diff --git a/QuickPay/API/Constants.php b/QuickPay/API/Constants.php index 8636ad3..341be63 100644 --- a/QuickPay/API/Constants.php +++ b/QuickPay/API/Constants.php @@ -1,18 +1,18 @@ client = $client; } - /** - * get function. - * - * Performs an API GET request - * - * @access public - * @param string $path - * @param array $query - * @return Response - */ + * GET function. + * + * Performs an API GET request + * + * @access public + * @param string $path + * @param array $query + * @return Response + */ public function get( $path, $query = array() ) { - // Add query parameters to $path? - if ( $query ) - { - if (strpos($path, '?') === false ) - { - $path .= '?' . http_build_query($query); - } - else - { - $path .= ini_get('arg_separator.output') . http_build_query($query); - } - } + // Add query parameters to $path? + if ($query) { + if (strpos($path, '?') === false ) { + $path .= '?' . http_build_query($query); + } else { + $path .= ini_get('arg_separator.output') . http_build_query($query); + } + } // Set the request params - $this->set_url( $path ); + $this->set_url($path); // Start the request and return the response return $this->execute('GET'); } - - /** - * post function. - * - * Performs an API POST request - * - * @access public - * @return Response - */ + /** + * POST function. + * + * Performs an API POST request + * + * @access public + * @return Response + */ public function post( $path, $form = array() ) { // Set the request params - $this->set_url( $path ); + $this->set_url($path); // Start the request and return the response return $this->execute('POST', $form); } - - /** - * put function. - * - * Performs an API PUT request - * - * @access public - * @return Response - */ + /** + * PUT function. + * + * Performs an API PUT request + * + * @access public + * @return Response + */ public function put( $path, $form = array() ) { // Set the request params - $this->set_url( $path ); + $this->set_url($path); // Start the request and return the response return $this->execute('PUT', $form); } - - /** - * patch function. - * - * Performs an API PATCH request - * - * @access public - * @return Response - */ + /** + * PATCH function. + * + * Performs an API PATCH request + * + * @access public + * @return Response + */ public function patch( $path, $form = array() ) { // Set the request params - $this->set_url( $path ); + $this->set_url($path); // Start the request and return the response return $this->execute('PATCH', $form); } - - /** - * delete function. - * - * Performs an API DELETE request - * - * @access public - * @return Response - */ + /** + * DELETE function. + * + * Performs an API DELETE request + * + * @access public + * @return Response + */ public function delete( $path, $form = array() ) { // Set the request params - $this->set_url( $path ); + $this->set_url($path); // Start the request and return the response return $this->execute('DELETE', $form); } - - /** - * set_url function. - * - * Takes an API request string and appends it to the API url - * - * @access protected - * @return void - */ + /** + * set_url function. + * + * Takes an API request string and appends it to the API url + * + * @access protected + * @return void + */ protected function set_url( $params ) { - curl_setopt( $this->client->ch, CURLOPT_URL, Constants::API_URL . trim( $params, '/' ) ); + curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL . trim($params, '/')); } - - /** - * execute function. - * - * Performs the prepared API request - * - * @access protected - * @param string $request_type - * @param array $form - * @return Response - */ - protected function execute( $request_type, $form = array() ) - { - // Set the HTTP request type - curl_setopt( $this->client->ch, CURLOPT_CUSTOMREQUEST, $request_type ); - - // If additional data is delivered, we will send it along with the API request - if( is_array( $form ) && ! empty( $form ) ) - { - curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form) ); - } - - // Store received headers in temporary memory file, remember sent headers - $fh_header = fopen('php://memory', 'w+'); - curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); - curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); - - // Execute the request - $response_data = curl_exec( $this->client->ch ); - - if (curl_errno($this->client->ch) !== 0) { - //An error occurred - fclose($fh_header); - throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); - } - - // Grab the headers - $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); - rewind($fh_header); - $received_headers = stream_get_contents($fh_header); - fclose($fh_header); - - // Retrieve the HTTP response code - $response_code = (int) curl_getinfo( $this->client->ch, CURLINFO_HTTP_CODE ); - - // Return the response object. - return new Response( $response_code, $sent_headers, $received_headers, $response_data ); - } + /** + * EXECUTE function. + * + * Performs the prepared API request + * + * @access protected + * @param string $request_type + * @param array $form + * @return Response + */ + protected function execute( $request_type, $form = array() ) + { + // Set the HTTP request type + curl_setopt($this->client->ch, CURLOPT_CUSTOMREQUEST, $request_type); + + // If additional data is delivered, we will send it along with the API request + if (is_array($form) && ! empty($form)) { + curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form)); + } + + // Store received headers in temporary memory file, remember sent headers + $fh_header = fopen('php://memory', 'w+'); + curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); + curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); + + // Execute the request + $response_data = curl_exec($this->client->ch); + + if (curl_errno($this->client->ch) !== 0) { + //An error occurred + fclose($fh_header); + throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); + } + + // Grab the headers + $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); + rewind($fh_header); + $received_headers = stream_get_contents($fh_header); + fclose($fh_header); + + // Retrieve the HTTP response code + $response_code = (int) curl_getinfo($this->client->ch, CURLINFO_HTTP_CODE); + + // Return the response object. + return new Response($response_code, $sent_headers, $received_headers, $response_data); + } } diff --git a/QuickPay/API/Response.php b/QuickPay/API/Response.php index 56df190..7ab38f9 100644 --- a/QuickPay/API/Response.php +++ b/QuickPay/API/Response.php @@ -1,52 +1,52 @@ received_headers = $received_headers; $this->response_data = $response_data; } - - + + /** * as_raw - * + * * Returns the HTTP status code, headers and response body. - * Usage: list($status_code, $headers, $response_body) = $response->as_raw(). - * - * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. - * @return array [integer, string[], string] - */ + * Usage: list($status_code, $headers, $response_body) = $response->as_raw(). + * + * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. + * @return array [integer, string[], string] + */ public function as_raw( $keep_authorization_value = false ) { - // To avoid unintentional logging of credentials the default is to mask the value of the Authorization: header - if ($keep_authorization_value ) - { - $sent_headers = $this->sent_headers; - } - else - { - // Avoid dependency on mbstring - $lines = explode( "\n", $this->sent_headers ); - foreach ( $lines as &$line ) - { - if (strpos($line, 'Authorization: ') === 0) { - $line = 'Authorization: '; - } - } - $sent_headers = implode("\n", $lines); - } + // To avoid unintentional logging of credentials the default is to mask the value of the Authorization: header + if ($keep_authorization_value ) { + $sent_headers = $this->sent_headers; + } else { + // Avoid dependency on mbstring + $lines = explode("\n", $this->sent_headers); + foreach ($lines as &$line) { + if (strpos($line, 'Authorization: ') === 0) { + $line = 'Authorization: '; + } + } + $sent_headers = implode("\n", $lines); + } return [ - $this->status_code, - [ 'sent' => $sent_headers, - 'received' => $this->received_headers, - ], - $this->response_data, - ]; + $this->status_code, + ['sent' => $sent_headers, + 'received' => $this->received_headers, + ], + $this->response_data, + ]; } - + /** * as_array - * + * * Returns the response body as an array - * + * * @return array - */ + */ public function as_array() { - if( $response = json_decode( $this->response_data, TRUE ) ) - { + if ($response = json_decode($this->response_data, true) ) { return $response; } return array(); } - /** * as_object - * + * * Returns the response body as an array - * + * * @return array - */ + */ public function as_object() { - if( $response = json_decode( $this->response_data ) ) - { + if ($response = json_decode($this->response_data) ) { return $response; } return new \stdClass; } - - + /** * http_status - * + * * Returns the http_status code - * + * * @return int - */ + */ public function http_status() { return $this->status_code; } - - + /** * is_success - * + * * Checks if the http status code indicates a succesful or an error response. - * + * * @return boolean - */ + */ public function is_success() { - if( $this->status_code > 299 ) - { + if ($this->status_code > 299 ) { return false; } - + return true; } } diff --git a/QuickPay/QuickPay.php b/QuickPay/QuickPay.php index 9decef0..5462af5 100644 --- a/QuickPay/QuickPay.php +++ b/QuickPay/QuickPay.php @@ -1,11 +1,11 @@ request = new Request( $client ); + $client = new Client($auth_string); + $this->request = new Request($client); } } \ No newline at end of file diff --git a/QuickPay/Tests/api/ExceptionTest.php b/QuickPay/Tests/api/ExceptionTest.php index 07ba11f..eb14927 100644 --- a/QuickPay/Tests/api/ExceptionTest.php +++ b/QuickPay/Tests/api/ExceptionTest.php @@ -11,14 +11,14 @@ class ExceptionTest extends \PHPUnit_Framework_TestCase public function testThrownExceptionValues() { - try + try { throw new Exception($this->testMessage, $this->testCode); } - catch(Exception $e) + catch(Exception $e) { $this->assertEquals($e->getMessage(), $this->testMessage); $this->assertEquals($e->getCode(), $this->testCode); } } -} \ No newline at end of file +} diff --git a/QuickPay/Tests/api/RequestTest.php b/QuickPay/Tests/api/RequestTest.php index 3d50df5..19f6789 100644 --- a/QuickPay/Tests/api/RequestTest.php +++ b/QuickPay/Tests/api/RequestTest.php @@ -23,7 +23,7 @@ public function testResponseInstance() { $pingResponse = $this->request->get('/ping'); - $this->assertTrue( ($pingResponse instanceof Response) ); + $this->assertTrue(($pingResponse instanceof Response)); } public function testBadAuthentication() @@ -64,4 +64,4 @@ public function testFailedPostResponse() $this->assertFalse($pingResponse->is_success()); } -} \ No newline at end of file +} diff --git a/QuickPay/Tests/api/ResponseTest.php b/QuickPay/Tests/api/ResponseTest.php index 7a7f653..2e35daf 100644 --- a/QuickPay/Tests/api/ResponseTest.php +++ b/QuickPay/Tests/api/ResponseTest.php @@ -10,33 +10,33 @@ class ResponseTest extends \PHPUnit_Framework_TestCase private $responseTestData = '{ "key1": "value1", "key2": "value2" }'; /** - * @param string $httpCode The HTTP code we want to test + * @param string $httpCode The HTTP code we want to test * @param string $expectedResult What we expect the result to be * * @dataProvider providerTestSuccessResponseHTTPCodes */ - public function testSuccessResponseHTTPCodes($httpCode, $expectedResult) + public function testSuccessResponseHTTPCodes($httpCode, $expectedResult) { $response = new Response($httpCode, '', '', ''); - + $result = $response->is_success(); - + $this->assertEquals($result, $expectedResult); } public function providerTestSuccessResponseHTTPCodes() { - return array( - array(200, true), - array(255, true), + return array( + array(200, true), + array(255, true), array(299, true), array(300, false), - array(400, false) - ); + array(400, false) + ); } /** - * @param string $httpCode The HTTP code we want to test + * @param string $httpCode The HTTP code we want to test * @param string $expectedCode What we expect the result to be * * @dataProvider providerTestReturnOfHTTPStatusCodes @@ -44,9 +44,9 @@ public function providerTestSuccessResponseHTTPCodes() public function testReturnOfHTTPStatusCodes($httpCode, $expectedCode) { $response = new Response($httpCode, '', '', ''); - + $statusCode = $response->http_status(); - + $this->assertEquals($statusCode, $expectedCode); } @@ -65,7 +65,7 @@ public function testReturnOfResponseDataAsArray() $responseArray = $response->as_array(); - $this->assertTrue( is_array($responseArray) ); + $this->assertTrue(is_array($responseArray)); } public function testReturnOfEmptyResponseDataAsArray() @@ -74,7 +74,7 @@ public function testReturnOfEmptyResponseDataAsArray() $responseArray = $response->as_array(); - $this->assertTrue( is_array($responseArray) ); + $this->assertTrue(is_array($responseArray)); } public function testReturnOfResponseDataAsObject() @@ -83,7 +83,7 @@ public function testReturnOfResponseDataAsObject() $responseObject = $response->as_object(); - $this->assertTrue( is_object($responseObject) ); + $this->assertTrue(is_object($responseObject)); } public function testReturnOfEmptyResponseDataAsObject() @@ -92,18 +92,18 @@ public function testReturnOfEmptyResponseDataAsObject() $responseObject = $response->as_object(); - $this->assertTrue( is_object($responseObject) ); + $this->assertTrue(is_object($responseObject)); } public function testReturnOfResponseDataAsRaw() { $response = new Response(200, '', '', $this->responseTestData); - list($statusCode, $headers, $responseRaw) = $response->as_raw(); + list($statusCode, $headers, $responseRaw) = $response->as_raw(); - $this->assertTrue( is_int($statusCode) ); - $this->assertTrue( is_array($headers) ); - $this->assertTrue( is_string($responseRaw) ); + $this->assertTrue(is_int($statusCode)); + $this->assertTrue(is_array($headers)); + $this->assertTrue(is_string($responseRaw)); } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 3d1d90a..a21677a 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "lib/curl": "dev-master" }, "require-dev" : { - "phpunit/phpunit": "4.8.*@dev" + "phpunit/phpunit": "4.8.*@dev", + "squizlabs/php_codesniffer": "2.*" }, "autoload": { "psr-4": {