Skip to content

Commit cd727b9

Browse files
authored
feat: automatic code style checking (#109)
1 parent 51e69a9 commit cd727b9

File tree

9 files changed

+127
-82
lines changed

9 files changed

+127
-82
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ vendor/
88
.idea/
99

1010
# Environment files
11-
.env/*.*
11+
.env/*.*
12+
13+
# Local configs
14+
.php_cs
15+
.php_cs.cache
16+
phpunit.xml

.php_cs.dist

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__ . '/lib');
5+
6+
return PhpCsFixer\Config::create()
7+
->setRiskyAllowed(true)
8+
->setRules([
9+
'@PSR2' => true,
10+
'@Symfony' => true,
11+
'array_syntax' => ['syntax' => 'short'],
12+
'cast_spaces' => ['space' => 'single'],
13+
'yoda_style' => false,
14+
'concat_space' => ['spacing' => 'one'],
15+
'phpdoc_summary' => false,
16+
'combine_consecutive_unsets' => true,
17+
'final_internal_class' => true,
18+
'global_namespace_import' => ['import_classes' => false],
19+
])
20+
->setFinder($finder);

.travis.yml

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
language: php
22

3-
before_script:
4-
- if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi;
5-
- composer install
6-
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;
7-
8-
script:
9-
- make test
10-
11-
after_success:
12-
- bash <(curl -s https://codecov.io/bash)
13-
143
php:
154
- 5.6
165
- 7.0
@@ -20,8 +9,26 @@ php:
209
- 7.4
2110

2211
env:
23-
- dependencies=lowest
24-
- dependencies=highest
12+
matrix:
13+
- dependencies=lowest
14+
- dependencies=highest
15+
global:
16+
- composer_flags="--no-interaction --no-ansi --no-progress --no-suggest --verbose"
17+
18+
before_install:
19+
- composer self-update
20+
- composer clear-cache
21+
22+
install:
23+
- if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi;
24+
- if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi;
25+
- if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi;
26+
27+
script:
28+
- vendor/bin/phpunit test/unit
29+
30+
after_success:
31+
- bash <(curl -s https://codecov.io/bash)
2532

2633
notifications:
2734
slack:

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ clean:
44
@rm -rf vendor composer.lock
55

66
install: clean
7-
composer install
7+
composer install --no-suggest --no-scripts --no-progress --no-interaction
88

99
test: install
1010
vendor/bin/phpunit test/unit

composer.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
"require": {
1919
"php": ">=5.6",
2020
"ext-curl": "*",
21-
"ext-json": "*"
21+
"ext-json": "*",
22+
"ext-mbstring": "*"
2223
},
2324
"require-dev": {
24-
"phpunit/phpunit": "~4.4",
25-
"squizlabs/php_codesniffer": "~2.0"
25+
"phpunit/phpunit": "^5.7",
26+
"sebastian/version": "^1.0.6",
27+
"squizlabs/php_codesniffer": "~2.0",
28+
"friendsofphp/php-cs-fixer": "^2.16"
2629
},
2730
"autoload": {
2831
"psr-4": {

lib/Client.php

+48-38
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use SendGrid\Exception\InvalidRequest;
1010

1111
/**
12-
*
1312
* Class Client
14-
* @package SendGrid
1513
* @version 3.9.5
1614
*
1715
* Quickly and easily access any REST or REST-like API.
@@ -76,7 +74,6 @@
7674
* @method Client segments()
7775
* @method Client singlesends()
7876
*
79-
*
8077
* Devices
8178
* @method Client devices()
8279
*
@@ -206,24 +203,30 @@ class Client
206203
protected $retryOnLimit;
207204

208205
/**
209-
* These are the supported HTTP verbs
206+
* Supported HTTP verbs.
210207
*
211208
* @var array
212209
*/
213210
private $methods = ['get', 'post', 'patch', 'put', 'delete'];
214211

215212
/**
216-
* Initialize the client
217-
*
218-
* @param string $host the base url (e.g. https://api.sendgrid.com)
219-
* @param array $headers global request headers
220-
* @param string $version api version (configurable) - this is specific to the SendGrid API
221-
* @param array $path holds the segments of the url path
222-
* @param array $curlOptions extra options to set during curl initialization
223-
* @param bool $retryOnLimit set default retry on limit flag
224-
*/
225-
public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null, $retryOnLimit = false)
226-
{
213+
* Initialize the client.
214+
*
215+
* @param string $host the base url (e.g. https://api.sendgrid.com)
216+
* @param array $headers global request headers
217+
* @param string $version api version (configurable) - this is specific to the SendGrid API
218+
* @param array $path holds the segments of the url path
219+
* @param array $curlOptions extra options to set during curl initialization
220+
* @param bool $retryOnLimit set default retry on limit flag
221+
*/
222+
public function __construct(
223+
$host,
224+
$headers = null,
225+
$version = null,
226+
$path = null,
227+
$curlOptions = null,
228+
$retryOnLimit = false
229+
) {
227230
$this->host = $host;
228231
$this->headers = $headers ?: [];
229232
$this->version = $version;
@@ -275,7 +278,7 @@ public function getCurlOptions()
275278
}
276279

277280
/**
278-
* Set extra options to set during curl initialization
281+
* Set extra options to set during curl initialization.
279282
*
280283
* @param array $options
281284
*
@@ -289,7 +292,7 @@ public function setCurlOptions(array $options)
289292
}
290293

291294
/**
292-
* Set default retry on limit flag
295+
* Set default retry on limit flag.
293296
*
294297
* @param bool $retry
295298
*
@@ -303,7 +306,7 @@ public function setRetryOnLimit($retry)
303306
}
304307

305308
/**
306-
* Set concurrent request flag
309+
* Set concurrent request flag.
307310
*
308311
* @param bool $isConcurrent
309312
*
@@ -317,7 +320,7 @@ public function setIsConcurrentRequest($isConcurrent)
317320
}
318321

319322
/**
320-
* Build the final URL to be passed
323+
* Build the final URL to be passed.
321324
*
322325
* @param array $queryParams an array of all the query parameters
323326
*
@@ -329,16 +332,17 @@ private function buildUrl($queryParams = null)
329332
if (isset($queryParams)) {
330333
$path .= '?' . http_build_query($queryParams);
331334
}
335+
332336
return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);
333337
}
334338

335339
/**
336340
* Creates curl options for a request
337-
* this function does not mutate any private variables
341+
* this function does not mutate any private variables.
338342
*
339343
* @param string $method
340-
* @param array $body
341-
* @param array $headers
344+
* @param array $body
345+
* @param array $headers
342346
*
343347
* @return array
344348
*/
@@ -349,7 +353,7 @@ private function createCurlOptions($method, $body = null, $headers = null)
349353
CURLOPT_HEADER => true,
350354
CURLOPT_CUSTOMREQUEST => strtoupper($method),
351355
CURLOPT_SSL_VERIFYPEER => true,
352-
CURLOPT_FAILONERROR => false
356+
CURLOPT_FAILONERROR => false,
353357
] + $this->curlOptions;
354358

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

371375
/**
372-
* @param array $requestData
373-
* e.g. ['method' => 'POST', 'url' => 'www.example.com', 'body' => 'test body', 'headers' => []]
374-
* @param bool $retryOnLimit
376+
* @param array $requestData (method, url, body and headers)
377+
* @param bool $retryOnLimit
375378
*
376379
* @return array
377380
*/
@@ -401,9 +404,9 @@ private function createCurlMultiHandle(array $requests)
401404
}
402405

403406
/**
404-
* Prepare response object
407+
* Prepare response object.
405408
*
406-
* @param resource $channel the curl resource
409+
* @param resource $channel the curl resource
407410
* @param string $content
408411
*
409412
* @return Response object
@@ -413,17 +416,17 @@ private function parseResponse($channel, $content)
413416
$headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
414417
$statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);
415418

416-
$responseBody = substr($content, $headerSize);
419+
$responseBody = mb_substr($content, $headerSize);
417420

418-
$responseHeaders = substr($content, 0, $headerSize);
421+
$responseHeaders = mb_substr($content, 0, $headerSize);
419422
$responseHeaders = explode("\n", $responseHeaders);
420423
$responseHeaders = array_map('trim', $responseHeaders);
421424

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

425428
/**
426-
* Retry request
429+
* Retry request.
427430
*
428431
* @param array $responseHeaders headers from rate limited response
429432
* @param string $method the HTTP verb
@@ -432,12 +435,14 @@ private function parseResponse($channel, $content)
432435
* @param array $headers original headers
433436
*
434437
* @return Response response object
438+
*
435439
* @throws InvalidRequest
436440
*/
437441
private function retryRequest(array $responseHeaders, $method, $url, $body, $headers)
438442
{
439443
$sleepDurations = $responseHeaders['X-Ratelimit-Reset'] - time();
440444
sleep($sleepDurations > 0 ? $sleepDurations : 0);
445+
441446
return $this->makeRequest($method, $url, $body, $headers, false);
442447
}
443448

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

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

472-
if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) {
478+
if ($retryOnLimit && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) {
473479
$responseHeaders = $response->headers(true);
480+
474481
return $this->retryRequest($responseHeaders, $method, $url, $body, $headers);
475482
}
476483

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

482489
/**
483-
* Send all saved requests at once
490+
* Send all saved requests at once.
484491
*
485492
* @param array $requests
486493
*
@@ -504,11 +511,10 @@ public function makeAllRequests(array $requests = [])
504511
$responses = [];
505512
$sleepDurations = 0;
506513
foreach ($channels as $id => $channel) {
507-
508514
$content = curl_multi_getcontent($channel);
509515
$response = $this->parseResponse($channel, $content);
510516

511-
if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $requests[$id]['retryOnLimit']) {
517+
if ($requests[$id]['retryOnLimit'] && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) {
512518
$headers = $response->headers(true);
513519
$sleepDurations = max($sleepDurations, $headers['X-Ratelimit-Reset'] - time());
514520
$requestData = [
@@ -531,6 +537,7 @@ public function makeAllRequests(array $requests = [])
531537
sleep($sleepDurations > 0 ? $sleepDurations : 0);
532538
$responses = array_merge($responses, $this->makeAllRequests($retryRequests));
533539
}
540+
534541
return $responses;
535542
}
536543

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

558565
/**
559566
* Dynamically add method calls to the url, then call a method.
560-
* (e.g. client.name.name.method())
567+
* (e.g. client.name.name.method()).
561568
*
562569
* @param string $name name of the dynamic method call or HTTP verb
563570
* @param array $args parameters passed with the method call
564571
*
565572
* @return Client|Response|Response[]|null object
573+
*
566574
* @throws InvalidRequest
567575
*/
568576
public function __call($name, $args)
569577
{
570-
$name = strtolower($name);
578+
$name = mb_strtolower($name);
571579

572580
if ($name === 'version') {
573581
$this->version = $args[0];
582+
574583
return $this->_();
575584
}
576585

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

582-
if (in_array($name, $this->methods, true)) {
591+
if (\in_array($name, $this->methods, true)) {
583592
$body = isset($args[0]) ? $args[0] : null;
584593
$queryParams = isset($args[1]) ? $args[1] : null;
585594
$url = $this->buildUrl($queryParams);
@@ -590,6 +599,7 @@ public function __call($name, $args)
590599
// save request to be sent later
591600
$requestData = ['method' => $name, 'url' => $url, 'body' => $body, 'headers' => $headers];
592601
$this->savedRequests[] = $this->createSavedRequest($requestData, $retryOnLimit);
602+
593603
return null;
594604
}
595605

0 commit comments

Comments
 (0)