From f648c08599e2e38e4e2e96f5be6ad79ffd8cf9e7 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 6 Jul 2017 07:18:31 -0700 Subject: [PATCH] Run php-cs-fixer on code --- src/Exception/AuthenticationException.php | 2 +- src/Exception/HttpException.php | 10 +- src/Exception/InvalidRequestException.php | 12 +- src/WebService/Client.php | 153 ++++++++++-------- src/WebService/Http/CurlRequest.php | 7 +- src/WebService/Http/Request.php | 5 +- src/WebService/Http/RequestFactory.php | 5 +- tests/MaxMind/Test/WebService/ClientTest.php | 92 ++++++----- .../Test/WebService/Http/CurlRequestTest.php | 17 +- 9 files changed, 162 insertions(+), 141 deletions(-) diff --git a/src/Exception/AuthenticationException.php b/src/Exception/AuthenticationException.php index 2c50752..ba42362 100644 --- a/src/Exception/AuthenticationException.php +++ b/src/Exception/AuthenticationException.php @@ -3,7 +3,7 @@ namespace MaxMind\Exception; /** - * This class represents an error authenticating + * This class represents an error authenticating. */ class AuthenticationException extends InvalidRequestException { diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index c02a191..f158944 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -8,15 +8,15 @@ class HttpException extends WebServiceException { /** - * The URI queried + * The URI queried. */ private $uri; /** - * @param string $message A message describing the error. - * @param int $httpStatus The HTTP status code of the response - * @param string $uri The URI used in the request. - * @param \Exception $previous The previous exception, if any. + * @param string $message a message describing the error + * @param int $httpStatus the HTTP status code of the response + * @param string $uri the URI used in the request + * @param \Exception $previous the previous exception, if any */ public function __construct( $message, diff --git a/src/Exception/InvalidRequestException.php b/src/Exception/InvalidRequestException.php index 66c35f8..354f4b6 100644 --- a/src/Exception/InvalidRequestException.php +++ b/src/Exception/InvalidRequestException.php @@ -8,16 +8,16 @@ class InvalidRequestException extends HttpException { /** - * The code returned by the MaxMind web service + * The code returned by the MaxMind web service. */ private $error; /** - * @param string $message The exception message - * @param int $error The error code returned by the MaxMind web service - * @param int $httpStatus The HTTP status code of the response - * @param string $uri The URI queries - * @param \Exception $previous The previous exception, if any. + * @param string $message the exception message + * @param int $error the error code returned by the MaxMind web service + * @param int $httpStatus the HTTP status code of the response + * @param string $uri the URI queries + * @param \Exception $previous the previous exception, if any */ public function __construct( $message, diff --git a/src/WebService/Client.php b/src/WebService/Client.php index 748fcef..1f1d2f6 100644 --- a/src/WebService/Client.php +++ b/src/WebService/Client.php @@ -17,7 +17,7 @@ * This class is not intended to be used directly by an end-user of a * MaxMind web service. Please use the appropriate client API for the service * that you are using. - * @package MaxMind\WebService + * * @internal */ class Client @@ -35,22 +35,21 @@ class Client private $userId; /** - * @param int $userId Your MaxMind user ID - * @param string $licenseKey Your MaxMind license key - * @param array $options An array of options. Possible keys: - * - * * `host` - The host to use when connecting to the web service. - * * `userAgent` - The prefix of the User-Agent to use in the request. - * * `caBundle` - The bundle of CA root certificates to use in the request. - * * `connectTimeout` - The connect timeout to use for the request. - * * `timeout` - The timeout to use for the request. - * * `proxy` - The HTTP proxy to use. May include a schema, port, - * username, and password, e.g., `http://username:password@127.0.0.1:10`. + * @param int $userId your MaxMind user ID + * @param string $licenseKey your MaxMind license key + * @param array $options an array of options. Possible keys: + * * `host` - The host to use when connecting to the web service. + * * `userAgent` - The prefix of the User-Agent to use in the request. + * * `caBundle` - The bundle of CA root certificates to use in the request. + * * `connectTimeout` - The connect timeout to use for the request. + * * `timeout` - The timeout to use for the request. + * * `proxy` - The HTTP proxy to use. May include a schema, port, + * username, and password, e.g., `http://username:password@127.0.0.1:10`. */ public function __construct( $userId, $licenseKey, - $options = array() + $options = [] ) { $this->userId = $userId; $this->licenseKey = $licenseKey; @@ -83,19 +82,21 @@ public function __construct( /** * @param string $service name of the service querying - * @param string $path the URI path to use - * @param array $input the data to be posted as JSON + * @param string $path the URI path to use + * @param array $input the data to be posted as JSON + * + * @throws InvalidInputException when the request has missing or invalid + * data + * @throws AuthenticationException when there is an issue authenticating the + * request + * @throws InsufficientFundsException when your account is out of funds + * @throws InvalidRequestException when the request is invalid for some + * other reason, e.g., invalid JSON in the POST. + * @throws HttpException when an unexpected HTTP error occurs + * @throws WebServiceException when some other error occurs. This also + * serves as the base class for the above exceptions. + * * @return array The decoded content of a successful response - * @throws InvalidInputException when the request has missing or invalid - * data. - * @throws AuthenticationException when there is an issue authenticating the - * request. - * @throws InsufficientFundsException when your account is out of funds. - * @throws InvalidRequestException when the request is invalid for some - * other reason, e.g., invalid JSON in the POST. - * @throws HttpException when an unexpected HTTP error occurs. - * @throws WebServiceException when some other error occurs. This also - * serves as the base class for the above exceptions. */ public function post($service, $path, $input) { @@ -109,10 +110,11 @@ public function post($service, $path, $input) $request = $this->createRequest( $path, - array('Content-Type: application/json') + ['Content-Type: application/json'] ); list($statusCode, $contentType, $body) = $request->post($body); + return $this->handleResponse( $statusCode, $contentType, @@ -137,15 +139,15 @@ public function get($service, $path) ); } - private function userAgent() { $curlVersion = curl_version(); - return $this->userAgentPrefix . 'MaxMind-WS-API/' . Client::VERSION . ' PHP/' . PHP_VERSION . + + return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . PHP_VERSION . ' curl/' . $curlVersion['version']; } - private function createRequest($path, $headers = array()) + private function createRequest($path, $headers = []) { array_push( $headers, @@ -156,32 +158,34 @@ private function createRequest($path, $headers = array()) return $this->httpRequestFactory->request( $this->urlFor($path), - array( + [ 'caBundle' => $this->caBundle, 'connectTimeout' => $this->connectTimeout, 'headers' => $headers, 'proxy' => $this->proxy, 'timeout' => $this->timeout, 'userAgent' => $this->userAgent(), - ) + ] ); } /** - * @param integer $statusCode the HTTP status code of the response + * @param int $statusCode the HTTP status code of the response * @param string $contentType the Content-Type of the response - * @param string $body the response body - * @param string $service the name of the service - * @param string $path the path used in the request + * @param string $body the response body + * @param string $service the name of the service + * @param string $path the path used in the request + * + * @throws AuthenticationException when there is an issue authenticating the + * request + * @throws InsufficientFundsException when your account is out of funds + * @throws InvalidRequestException when the request is invalid for some + * other reason, e.g., invalid JSON in the POST. + * @throws HttpException when an unexpected HTTP error occurs + * @throws WebServiceException when some other error occurs. This also + * serves as the base class for the above exceptions + * * @return array The decoded content of a successful response - * @throws AuthenticationException when there is an issue authenticating the - * request. - * @throws InsufficientFundsException when your account is out of funds. - * @throws InvalidRequestException when the request is invalid for some - * other reason, e.g., invalid JSON in the POST. - * @throws HttpException when an unexpected HTTP error occurs. - * @throws WebServiceException when some other error occurs. This also - * serves as the base class for the above exceptions */ private function handleResponse( $statusCode, @@ -194,9 +198,10 @@ private function handleResponse( $this->handle4xx($statusCode, $contentType, $body, $service, $path); } elseif ($statusCode >= 500) { $this->handle5xx($statusCode, $service, $path); - } elseif ($statusCode != 200) { + } elseif ($statusCode !== 200) { $this->handleUnexpectedStatus($statusCode, $service, $path); } + return $this->handleSuccess($body, $service); } @@ -223,8 +228,9 @@ private function jsonErrorDescription() } /** - * @param string $path The path to use in the URL - * @return string The constructed URL + * @param string $path the path to use in the URL + * + * @return string the constructed URL */ private function urlFor($path) { @@ -232,11 +238,12 @@ private function urlFor($path) } /** - * @param int $statusCode The HTTP status code - * @param string $contentType The response content-type - * @param string $body The response body - * @param string $service The service name - * @param string $path The path used in the request + * @param int $statusCode the HTTP status code + * @param string $contentType the response content-type + * @param string $body the response body + * @param string $service the service name + * @param string $path the path used in the request + * * @throws AuthenticationException * @throws HttpException * @throws InsufficientFundsException @@ -259,7 +266,7 @@ private function handle4xx( if (!strstr($contentType, 'json')) { throw new HttpException( "Received a $statusCode error for $service with " . - "the following body: " . $body, + 'the following body: ' . $body, $statusCode, $this->urlFor($path) ); @@ -294,10 +301,11 @@ private function handle4xx( } /** - * @param string $message The error message from the web service - * @param string $code The error code from the web service - * @param int $statusCode The HTTP status code - * @param string $path The path used in the request + * @param string $message the error message from the web service + * @param string $code the error code from the web service + * @param int $statusCode the HTTP status code + * @param string $path the path used in the request + * * @throws AuthenticationException * @throws InvalidRequestException * @throws InsufficientFundsException @@ -353,9 +361,10 @@ private function handleWebServiceError( } /** - * @param int $statusCode The HTTP status code - * @param string $service The service name - * @param string $path The URI path used in the request + * @param int $statusCode the HTTP status code + * @param string $service the service name + * @param string $path the URI path used in the request + * * @throws HttpException */ private function handle5xx($statusCode, $service, $path) @@ -368,9 +377,10 @@ private function handle5xx($statusCode, $service, $path) } /** - * @param int $statusCode The HTTP status code - * @param string $service The service name - * @param string $path The URI path used in the request + * @param int $statusCode the HTTP status code + * @param string $service the service name + * @param string $path the URI path used in the request + * * @throws HttpException */ private function handleUnexpectedStatus($statusCode, $service, $path) @@ -384,18 +394,20 @@ private function handleUnexpectedStatus($statusCode, $service, $path) } /** - * @param string $body The successful request body - * @param string $service The service name - * @return array The decoded request body + * @param string $body the successful request body + * @param string $service the service name + * * @throws WebServiceException if the request body cannot be decoded as - * JSON + * JSON + * + * @return array the decoded request body */ private function handleSuccess($body, $service) { - if (strlen($body) == 0) { + if (strlen($body) === 0) { throw new WebServiceException( "Received a 200 response for $service but did not " . - "receive a HTTP body." + 'receive a HTTP body.' ); } @@ -417,14 +429,14 @@ private function getCaBundle() // On OS X, when the SSL version is "SecureTransport", the system's // keychain will be used. - if ($curlVersion['ssl_version'] ==='SecureTransport') { + if ($curlVersion['ssl_version'] === 'SecureTransport') { return; } $cert = CaBundle::getSystemCaRootBundlePath(); // Check if the cert is inside a phar. If so, we need to copy the cert // to a temp file so that curl can see it. - if (substr($cert, 0, 7) == 'phar://') { + if (substr($cert, 0, 7) === 'phar://') { $tempDir = sys_get_temp_dir(); $newCert = tempnam($tempDir, 'geoip2-'); if ($newCert === false) { @@ -452,6 +464,7 @@ function () use ($newCert) { if (!file_exists($cert)) { throw new \RuntimeException("CA cert does not exist at $cert"); } + return $cert; } } diff --git a/src/WebService/Http/CurlRequest.php b/src/WebService/Http/CurlRequest.php index 548c9b1..e44e408 100644 --- a/src/WebService/Http/CurlRequest.php +++ b/src/WebService/Http/CurlRequest.php @@ -6,7 +6,7 @@ /** * This class is for internal use only. Semantic versioning does not not apply. - * @package MaxMind\WebService\Http + * * @internal */ class CurlRequest implements Request @@ -26,6 +26,7 @@ public function __construct($url, $options) /** * @param $body + * * @return array */ public function post($body) @@ -62,7 +63,6 @@ private function createCurl() $opts[CURLOPT_SSL_VERIFYPEER] = true; $opts[CURLOPT_RETURNTRANSFER] = true; - $opts[CURLOPT_HTTPHEADER] = $this->options['headers']; $opts[CURLOPT_USERAGENT] = $this->options['userAgent']; $opts[CURLOPT_PROXY] = $this->options['proxy']; @@ -84,6 +84,7 @@ private function createCurl() } curl_setopt_array($curl, $opts); + return $curl; } @@ -104,6 +105,6 @@ private function execute($curl) $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); curl_close($curl); - return array($statusCode, $contentType, $body); + return [$statusCode, $contentType, $body]; } } diff --git a/src/WebService/Http/Request.php b/src/WebService/Http/Request.php index dcb8be3..27bdd58 100644 --- a/src/WebService/Http/Request.php +++ b/src/WebService/Http/Request.php @@ -3,8 +3,8 @@ namespace MaxMind\WebService\Http; /** - * Interface Request - * @package MaxMind\WebService\Http + * Interface Request. + * * @internal */ interface Request @@ -17,6 +17,7 @@ public function __construct($url, $options); /** * @param $body + * * @return mixed */ public function post($body); diff --git a/src/WebService/Http/RequestFactory.php b/src/WebService/Http/RequestFactory.php index ccc10fd..099fb70 100644 --- a/src/WebService/Http/RequestFactory.php +++ b/src/WebService/Http/RequestFactory.php @@ -3,8 +3,8 @@ namespace MaxMind\WebService\Http; /** - * Class RequestFactory - * @package MaxMind\WebService\Http + * Class RequestFactory. + * * @internal */ class RequestFactory @@ -16,6 +16,7 @@ public function __construct() /** * @param $url * @param $options + * * @return CurlRequest */ public function request($url, $options) diff --git a/tests/MaxMind/Test/WebService/ClientTest.php b/tests/MaxMind/Test/WebService/ClientTest.php index 39c095b..d600e90 100644 --- a/tests/MaxMind/Test/WebService/ClientTest.php +++ b/tests/MaxMind/Test/WebService/ClientTest.php @@ -5,15 +5,17 @@ use Composer\CaBundle\CaBundle; use MaxMind\WebService\Client; +/** + * @coversNothing + */ class ClientTest extends \PHPUnit_Framework_TestCase { - public function test200() { - $this->assertEquals( - array('a' => 'b'), + $this->assertSame( + ['a' => 'b'], $this->withResponse( - '200', + 200, 'application/json', '{"a":"b"}' ), @@ -26,53 +28,55 @@ public function testOptions() $this->runRequest( 'TestService', '/path', - array(), + [], 200, 'application/json', '{}', 3213, 'abcdefghij', - array( + [ 'caBundle' => '/path/to/ca.pem', 'connectTimeout' => 15, 'proxy' => 'http://bob:pass@127.0.0.1:10', 'timeout' => 100, 'userAgent' => 'TestClient/1', - ) + ] ); } /** - * @expectedException MaxMind\Exception\WebServiceException + * @expectedException \MaxMind\Exception\WebServiceException * @expectedExceptionMessage Received a 200 response for TestService but could not decode the response as JSON: Syntax error. Body: { */ public function test200WithInvalidJson() { - $this->withResponse('200', 'application/json', '{'); + $this->withResponse(200, 'application/json', '{'); } /** - * @expectedException MaxMind\Exception\InsufficientFundsException + * @expectedException \MaxMind\Exception\InsufficientFundsException * @expectedExceptionMessage out of credit */ public function testInsufficientFunds() { $this->withResponse( - '402', + 402, 'application/json', '{"code":"INSUFFICIENT_FUNDS","error":"out of credit"}' ); } /** - * @expectedException MaxMind\Exception\AuthenticationException + * @expectedException \MaxMind\Exception\AuthenticationException * @expectedExceptionMessage Invalid auth * @dataProvider invalidAuthCodes + * + * @param mixed $code */ public function testInvalidAuth($code) { $this->withResponse( - '401', + 401, 'application/json', '{"code":"' . $code . '","error":"Invalid auth"}' ); @@ -80,92 +84,92 @@ public function testInvalidAuth($code) public function invalidAuthCodes() { - return array( - array('AUTHORIZATION_INVALID'), - array('LICENSE_KEY_REQUIRED'), - array('USER_ID_REQUIRED'), - array('USER_ID_UNKNOWN'), - ); + return [ + ['AUTHORIZATION_INVALID'], + ['LICENSE_KEY_REQUIRED'], + ['USER_ID_REQUIRED'], + ['USER_ID_UNKNOWN'], + ]; } /** - * @expectedException MaxMind\Exception\PermissionRequiredException + * @expectedException \MaxMind\Exception\PermissionRequiredException * @expectedExceptionMessage Permission required */ public function testPermissionRequired() { $this->withResponse( - '403', + 403, 'application/json', '{"code":"PERMISSION_REQUIRED","error":"Permission required"}' ); } /** - * @expectedException MaxMind\Exception\InvalidRequestException + * @expectedException \MaxMind\Exception\InvalidRequestException * @expectedExceptionMessage IP invalid */ public function testInvalidRequest() { $this->withResponse( - '400', + 400, 'application/json', '{"code":"IP_ADDRESS_INVALID","error":"IP invalid"}' ); } /** - * @expectedException MaxMind\Exception\WebServiceException + * @expectedException \MaxMind\Exception\WebServiceException * @expectedExceptionMessage Received a 400 error for TestService but could not decode the response as JSON: Syntax error. Body: {"blah"} */ public function test400WithInvalidJson() { - $this->withResponse('400', 'application/json', '{"blah"}'); + $this->withResponse(400, 'application/json', '{"blah"}'); } /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage Received a 400 error for TestService with no body */ public function test400WithNoBody() { - $this->withResponse('400', 'application/json', ''); + $this->withResponse(400, 'application/json', ''); } /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage Received a 400 error for TestService with the following body: text */ public function test400WithUnexpectedContentType() { - $this->withResponse('400', 'text/plain', 'text'); + $this->withResponse(400, 'text/plain', 'text'); } /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage Error response contains JSON but it does not specify code or error keys: {"not":"expected"} */ public function test400WithUnexpectedJson() { - $this->withResponse('400', 'application/json', '{"not":"expected"}'); + $this->withResponse(400, 'application/json', '{"not":"expected"}'); } /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage Received an unexpected HTTP status (300) for TestService */ public function test300() { - $this->withResponse('300', 'application/json', ''); + $this->withResponse(300, 'application/json', ''); } /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage Received a server error (500) for TestService */ public function test500() { - $this->withResponse('500', 'application/json', ''); + $this->withResponse(500, 'application/json', ''); } // convenience method when you don't care about the request @@ -174,7 +178,7 @@ private function withResponse($statusCode, $contentType, $body) return $this->runRequest( 'TestService', '/path', - array(), + [], $statusCode, $contentType, $body @@ -190,7 +194,7 @@ private function runRequest( $responseBody, $userId = 10, $licenseKey = '0123456789', - $options = array() + $options = [] ) { $stub = $this->getMockForAbstractClass( 'MaxMind\\WebService\\Http\\Request' @@ -199,7 +203,7 @@ private function runRequest( $stub->expects($this->once()) ->method('post') ->with($this->equalTo(json_encode($requestContent))) - ->willReturn(array($statusCode, $contentType, $responseBody)); + ->willReturn([$statusCode, $contentType, $responseBody]); $factory = $this->getMockBuilder( 'MaxMind\\WebService\\Http\\RequestFactory' @@ -209,16 +213,16 @@ private function runRequest( $url = 'https://' . $host . $path; - $headers = array( + $headers = [ 'Content-Type: application/json', 'Authorization: Basic ' . base64_encode($userId . ':' . $licenseKey), 'Accept: application/json', - ); + ]; $curlVersion = curl_version(); $userAgent = 'MaxMind-WS-API/' . Client::VERSION . ' PHP/' . PHP_VERSION - . ' curl/' . $curlVersion['version']; + . ' curl/' . $curlVersion['version']; if (isset($options['userAgent'])) { $userAgent = $options['userAgent'] . ' ' . $userAgent; } @@ -231,7 +235,7 @@ private function runRequest( ->with( $this->equalTo($url), $this->equalTo( - array( + [ 'headers' => $headers, 'userAgent' => $userAgent, 'connectTimeout' => isset($options['connectTimeout']) @@ -240,8 +244,8 @@ private function runRequest( ? $options['timeout'] : null, 'caBundle' => $caBundle, 'proxy' => isset($options['proxy']) - ? $options['proxy'] : null, - ) + ? $options['proxy'] : null, + ] ) )->willReturn($stub); diff --git a/tests/MaxMind/Test/WebService/Http/CurlRequestTest.php b/tests/MaxMind/Test/WebService/Http/CurlRequestTest.php index ed9aaaa..8041b34 100644 --- a/tests/MaxMind/Test/WebService/Http/CurlRequestTest.php +++ b/tests/MaxMind/Test/WebService/Http/CurlRequestTest.php @@ -10,20 +10,22 @@ // server, which is very painful to do in PHP 5.3. For 5.4+, there are // various solutions. When we increase our required PHP version, we should // look into those. +/** + * @coversNothing + */ class CurlRequestTest extends \PHPUnit_Framework_TestCase { - - private $options = array( - 'caBundle'=> null, + private $options = [ + 'caBundle' => null, 'connectTimeout' => 0, - 'headers' => array(), + 'headers' => [], 'proxy' => null, 'timeout' => 0, 'userAgent' => 'Test', - ); + ]; /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage cURL error (6): */ public function testGet() @@ -36,9 +38,8 @@ public function testGet() $cr->get(); } - /** - * @expectedException MaxMind\Exception\HttpException + * @expectedException \MaxMind\Exception\HttpException * @expectedExceptionMessage cURL error (6): */ public function testPost()