Skip to content

feat: automatic code style checking #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ vendor/
.idea/

# Environment files
.env/*.*
.env/*.*

# Local configs
.php_cs
.php_cs.cache
phpunit.xml
20 changes: 20 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/lib');

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'cast_spaces' => ['space' => 'single'],
'yoda_style' => false,
'concat_space' => ['spacing' => 'one'],
'phpdoc_summary' => false,
'combine_consecutive_unsets' => true,
'final_internal_class' => true,
'global_namespace_import' => ['import_classes' => false],
])
->setFinder($finder);
33 changes: 20 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
language: php

before_script:
- if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi;
- composer install
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;

script:
- make test

after_success:
- bash <(curl -s https://codecov.io/bash)

php:
- 5.6
- 7.0
Expand All @@ -20,8 +9,26 @@ php:
- 7.4

env:
- dependencies=lowest
- dependencies=highest
matrix:
- dependencies=lowest
- dependencies=highest
global:
- composer_flags="--no-interaction --no-ansi --no-progress --no-suggest --verbose"

before_install:
- composer self-update
- composer clear-cache

install:
- if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi;
- if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi;
- if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi;

script:
- vendor/bin/phpunit test/unit

after_success:
- bash <(curl -s https://codecov.io/bash)

notifications:
slack:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ clean:
@rm -rf vendor composer.lock

install: clean
composer install
composer install --no-suggest --no-scripts --no-progress --no-interaction

test: install
vendor/bin/phpunit test/unit
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
"require": {
"php": ">=5.6",
"ext-curl": "*",
"ext-json": "*"
"ext-json": "*",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.4",
"squizlabs/php_codesniffer": "~2.0"
"phpunit/phpunit": "^5.7",
"sebastian/version": "^1.0.6",
"squizlabs/php_codesniffer": "~2.0",
"friendsofphp/php-cs-fixer": "^2.16"
},
"autoload": {
"psr-4": {
Expand Down
86 changes: 48 additions & 38 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
use SendGrid\Exception\InvalidRequest;

/**
*
* Class Client
* @package SendGrid
* @version 3.9.5
*
* Quickly and easily access any REST or REST-like API.
Expand Down Expand Up @@ -76,7 +74,6 @@
* @method Client segments()
* @method Client singlesends()
*
*
* Devices
* @method Client devices()
*
Expand Down Expand Up @@ -206,24 +203,30 @@ class Client
protected $retryOnLimit;

/**
* These are the supported HTTP verbs
* Supported HTTP verbs.
*
* @var array
*/
private $methods = ['get', 'post', 'patch', 'put', 'delete'];

/**
* Initialize the client
*
* @param string $host the base url (e.g. https://api.sendgrid.com)
* @param array $headers global request headers
* @param string $version api version (configurable) - this is specific to the SendGrid API
* @param array $path holds the segments of the url path
* @param array $curlOptions extra options to set during curl initialization
* @param bool $retryOnLimit set default retry on limit flag
*/
public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null, $retryOnLimit = false)
{
* Initialize the client.
*
* @param string $host the base url (e.g. https://api.sendgrid.com)
* @param array $headers global request headers
* @param string $version api version (configurable) - this is specific to the SendGrid API
* @param array $path holds the segments of the url path
* @param array $curlOptions extra options to set during curl initialization
* @param bool $retryOnLimit set default retry on limit flag
*/
public function __construct(
$host,
$headers = null,
$version = null,
$path = null,
$curlOptions = null,
$retryOnLimit = false
) {
$this->host = $host;
$this->headers = $headers ?: [];
$this->version = $version;
Expand Down Expand Up @@ -275,7 +278,7 @@ public function getCurlOptions()
}

/**
* Set extra options to set during curl initialization
* Set extra options to set during curl initialization.
*
* @param array $options
*
Expand All @@ -289,7 +292,7 @@ public function setCurlOptions(array $options)
}

/**
* Set default retry on limit flag
* Set default retry on limit flag.
*
* @param bool $retry
*
Expand All @@ -303,7 +306,7 @@ public function setRetryOnLimit($retry)
}

/**
* Set concurrent request flag
* Set concurrent request flag.
*
* @param bool $isConcurrent
*
Expand All @@ -317,7 +320,7 @@ public function setIsConcurrentRequest($isConcurrent)
}

/**
* Build the final URL to be passed
* Build the final URL to be passed.
*
* @param array $queryParams an array of all the query parameters
*
Expand All @@ -329,16 +332,17 @@ private function buildUrl($queryParams = null)
if (isset($queryParams)) {
$path .= '?' . http_build_query($queryParams);
}

return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);
}

/**
* Creates curl options for a request
* this function does not mutate any private variables
* this function does not mutate any private variables.
*
* @param string $method
* @param array $body
* @param array $headers
* @param array $body
* @param array $headers
*
* @return array
*/
Expand All @@ -349,7 +353,7 @@ private function createCurlOptions($method, $body = null, $headers = null)
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FAILONERROR => false
CURLOPT_FAILONERROR => false,
] + $this->curlOptions;

if (isset($headers)) {
Expand All @@ -369,9 +373,8 @@ private function createCurlOptions($method, $body = null, $headers = null)
}

/**
* @param array $requestData
* e.g. ['method' => 'POST', 'url' => 'www.example.com', 'body' => 'test body', 'headers' => []]
* @param bool $retryOnLimit
* @param array $requestData (method, url, body and headers)
* @param bool $retryOnLimit
*
* @return array
*/
Expand Down Expand Up @@ -401,9 +404,9 @@ private function createCurlMultiHandle(array $requests)
}

/**
* Prepare response object
* Prepare response object.
*
* @param resource $channel the curl resource
* @param resource $channel the curl resource
* @param string $content
*
* @return Response object
Expand All @@ -413,17 +416,17 @@ private function parseResponse($channel, $content)
$headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);

$responseBody = substr($content, $headerSize);
$responseBody = mb_substr($content, $headerSize);

$responseHeaders = substr($content, 0, $headerSize);
$responseHeaders = mb_substr($content, 0, $headerSize);
$responseHeaders = explode("\n", $responseHeaders);
$responseHeaders = array_map('trim', $responseHeaders);

return new Response($statusCode, $responseBody, $responseHeaders);
}

/**
* Retry request
* Retry request.
*
* @param array $responseHeaders headers from rate limited response
* @param string $method the HTTP verb
Expand All @@ -432,12 +435,14 @@ private function parseResponse($channel, $content)
* @param array $headers original headers
*
* @return Response response object
*
* @throws InvalidRequest
*/
private function retryRequest(array $responseHeaders, $method, $url, $body, $headers)
{
$sleepDurations = $responseHeaders['X-Ratelimit-Reset'] - time();
sleep($sleepDurations > 0 ? $sleepDurations : 0);

return $this->makeRequest($method, $url, $body, $headers, false);
}

Expand All @@ -452,6 +457,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea
* @param bool $retryOnLimit should retry if rate limit is reach?
*
* @return Response object
*
* @throws InvalidRequest
*/
public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false)
Expand All @@ -469,8 +475,9 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry

$response = $this->parseResponse($channel, $content);

if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) {
if ($retryOnLimit && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) {
$responseHeaders = $response->headers(true);

return $this->retryRequest($responseHeaders, $method, $url, $body, $headers);
}

Expand All @@ -480,7 +487,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
}

/**
* Send all saved requests at once
* Send all saved requests at once.
*
* @param array $requests
*
Expand All @@ -504,11 +511,10 @@ public function makeAllRequests(array $requests = [])
$responses = [];
$sleepDurations = 0;
foreach ($channels as $id => $channel) {

$content = curl_multi_getcontent($channel);
$response = $this->parseResponse($channel, $content);

if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $requests[$id]['retryOnLimit']) {
if ($requests[$id]['retryOnLimit'] && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) {
$headers = $response->headers(true);
$sleepDurations = max($sleepDurations, $headers['X-Ratelimit-Reset'] - time());
$requestData = [
Expand All @@ -531,6 +537,7 @@ public function makeAllRequests(array $requests = [])
sleep($sleepDurations > 0 ? $sleepDurations : 0);
$responses = array_merge($responses, $this->makeAllRequests($retryRequests));
}

return $responses;
}

Expand All @@ -557,20 +564,22 @@ public function _($name = null)

/**
* Dynamically add method calls to the url, then call a method.
* (e.g. client.name.name.method())
* (e.g. client.name.name.method()).
*
* @param string $name name of the dynamic method call or HTTP verb
* @param array $args parameters passed with the method call
*
* @return Client|Response|Response[]|null object
*
* @throws InvalidRequest
*/
public function __call($name, $args)
{
$name = strtolower($name);
$name = mb_strtolower($name);

if ($name === 'version') {
$this->version = $args[0];

return $this->_();
}

Expand All @@ -579,7 +588,7 @@ public function __call($name, $args)
return $this->makeAllRequests();
}

if (in_array($name, $this->methods, true)) {
if (\in_array($name, $this->methods, true)) {
$body = isset($args[0]) ? $args[0] : null;
$queryParams = isset($args[1]) ? $args[1] : null;
$url = $this->buildUrl($queryParams);
Expand All @@ -590,6 +599,7 @@ public function __call($name, $args)
// save request to be sent later
$requestData = ['method' => $name, 'url' => $url, 'body' => $body, 'headers' => $headers];
$this->savedRequests[] = $this->createSavedRequest($requestData, $retryOnLimit);

return null;
}

Expand Down
Loading