Skip to content

Commit b9f278e

Browse files
committed
chore(cs): Centralize coding standards
1 parent 4208542 commit b9f278e

26 files changed

+280
-181
lines changed

.php_cs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
11
<?php
22

3-
$year = date('Y');
3+
$config = require __DIR__.'/scripts/php-cs-fixer.php';
44

5-
$fileHeaderComment = <<<COMMENT
6-
This file is part of the contentful/contentful-core package.
7-
8-
@copyright 2015-$year Contentful GmbH
9-
@license MIT
10-
COMMENT;
11-
12-
$finder = PhpCsFixer\Finder::create()
13-
->in('scripts')
14-
->in('src')
15-
->in('tests')
16-
;
17-
18-
return PhpCsFixer\Config::create()
19-
->setFinder($finder)
20-
->setRiskyAllowed(true)
21-
->setCacheFile(__DIR__.'/.php_cs.cache')
22-
->setRules([
23-
'@Symfony' => true,
24-
'@Symfony:risky' => true,
25-
'array_syntax' => ['syntax' => 'short'],
26-
'header_comment' => [
27-
'commentType' => 'PHPDoc',
28-
'header' => $fileHeaderComment,
29-
'separate' => 'both',
30-
],
31-
'linebreak_after_opening_tag' => true,
32-
'mb_str_functions' => true,
33-
'native_function_invocation' => true,
34-
'no_php4_constructor' => true,
35-
'no_unreachable_default_argument_value' => true,
36-
'no_useless_else' => true,
37-
'no_useless_return' => true,
38-
'ordered_imports' => true,
39-
'php_unit_strict' => true,
40-
'phpdoc_order' => true,
41-
'semicolon_after_instruction' => true,
42-
'strict_comparison' => true,
43-
'strict_param' => true,
44-
])
45-
;
5+
return $config(
6+
'contentful-core',
7+
false,
8+
['scripts', 'src', 'tests']
9+
);

scripts/create-redirector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// We remove all non-stable versions from the list as we don't want to direct the docs to them by default
1919
$tags = \array_filter($tags, function ($tag) {
20-
return false === \mb_strpos($tag, '-');
20+
return \false === \mb_strpos($tag, '-');
2121
});
2222

2323
\usort($tags, function ($a, $b) {

scripts/php-cs-fixer.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the contentful/contentful-core package.
5+
*
6+
* @copyright 2015-2018 Contentful GmbH
7+
* @license MIT
8+
*/
9+
10+
return function ($packageName, $usePhp7, array $directories, array $exclude = []) {
11+
$year = \date('Y');
12+
13+
$fileHeaderComment = <<<COMMENT
14+
This file is part of the contentful/$packageName package.
15+
16+
@copyright 2015-$year Contentful GmbH
17+
@license MIT
18+
COMMENT;
19+
20+
$finder = PhpCsFixer\Finder::create();
21+
foreach ($directories as $directory) {
22+
$finder = $finder->in($directory);
23+
}
24+
25+
foreach ($exclude as $path) {
26+
$finder = $finder->notPath($path);
27+
}
28+
29+
$rules = [
30+
'@Symfony' => \true,
31+
'@Symfony:risky' => \true,
32+
'array_syntax' => [
33+
'syntax' => 'short',
34+
],
35+
'header_comment' => [
36+
'comment_type' => 'PHPDoc',
37+
'header' => $fileHeaderComment,
38+
'location' => 'after_open',
39+
'separate' => 'both',
40+
],
41+
'linebreak_after_opening_tag' => \true,
42+
'logical_operators' => \true,
43+
'mb_str_functions' => \true,
44+
'method_chaining_indentation' => \true,
45+
'multiline_whitespace_before_semicolons' => [
46+
'strategy' => 'new_line_for_chained_calls',
47+
],
48+
'multiline_comment_opening_closing' => \true,
49+
'native_constant_invocation' => [
50+
'exclude' => [],
51+
],
52+
'native_function_invocation' => \true,
53+
'no_php4_constructor' => \true,
54+
'no_unreachable_default_argument_value' => \true,
55+
'no_useless_else' => \true,
56+
'no_useless_return' => \true,
57+
'ordered_imports' => \true,
58+
'php_unit_construct' => \true,
59+
'php_unit_dedicate_assert' => \true,
60+
'php_unit_strict' => \true,
61+
'phpdoc_add_missing_param_annotation' => \true,
62+
'phpdoc_order' => \true,
63+
'semicolon_after_instruction' => \true,
64+
'strict_comparison' => \true,
65+
'strict_param' => \true,
66+
];
67+
68+
if ($usePhp7) {
69+
$rules['declare_strict_types'] = \true;
70+
$rules['ternary_to_null_coalescing'] = \true;
71+
}
72+
73+
$cache = \tempnam(\sys_get_temp_dir(), $packageName).'-php_cs.cache';
74+
75+
return PhpCsFixer\Config::create()
76+
->setFinder($finder)
77+
->setRiskyAllowed(\true)
78+
->setCacheFile($cache)
79+
->setRules($rules)
80+
;
81+
};

src/Api/BaseClient.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use GuzzleHttp\Exception\ClientException;
1515
use Psr\Http\Message\RequestInterface;
1616
use Psr\Log\LoggerInterface;
17+
use function GuzzleHttp\json_decode as guzzle_json_decode;
1718

1819
/**
1920
* Abstract client for common code for the different clients.
@@ -58,7 +59,7 @@ abstract class BaseClient
5859
* @param LoggerInterface|null $logger
5960
* @param HttpClient|null $httpClient
6061
*/
61-
public function __construct($accessToken, $host, LoggerInterface $logger = null, HttpClient $httpClient = null)
62+
public function __construct($accessToken, $host, LoggerInterface $logger = \null, HttpClient $httpClient = \null)
6263
{
6364
$this->logger = $logger ?: new NullLogger();
6465
$this->httpClient = $httpClient ?: new HttpClient();
@@ -88,7 +89,7 @@ public function __construct($accessToken, $host, LoggerInterface $logger = null,
8889
*
8990
* @return $this
9091
*/
91-
public function setApplication($name, $version = null)
92+
public function setApplication($name, $version = \null)
9293
{
9394
$this->userAgentGenerator->setApplication($name, $version);
9495

@@ -103,7 +104,7 @@ public function setApplication($name, $version = null)
103104
*
104105
* @return $this
105106
*/
106-
public function setIntegration($name, $version = null)
107+
public function setIntegration($name, $version = \null)
107108
{
108109
$this->userAgentGenerator->setIntegration($name, $version);
109110

@@ -151,10 +152,10 @@ protected function request($method, $path, array $options = [])
151152

152153
$body = $message->getResponse()
153154
? (string) $message->getResponse()->getBody()
154-
: null;
155+
: \null;
155156

156157
return $body
157-
? \GuzzleHttp\json_decode((string) $body, true)
158+
? guzzle_json_decode((string) $body, \true)
158159
: [];
159160
}
160161

@@ -168,20 +169,20 @@ protected function request($method, $path, array $options = [])
168169
*/
169170
private function callApi(RequestInterface $request)
170171
{
171-
$startTime = \microtime(true);
172+
$startTime = \microtime(\true);
172173

173-
$exception = null;
174+
$exception = \null;
174175
try {
175176
$response = $this->httpClient->send($request);
176177
} catch (ClientException $exception) {
177178
$response = $exception->hasResponse()
178179
? $exception->getResponse()
179-
: null;
180+
: \null;
180181

181182
$exception = $this->createCustomException($exception);
182183
}
183184

184-
$duration = \microtime(true) - $startTime;
185+
$duration = \microtime(\true) - $startTime;
185186

186187
return new Message(
187188
$this->getApi(),
@@ -202,10 +203,10 @@ private function callApi(RequestInterface $request)
202203
*/
203204
private function createCustomException(ClientException $exception)
204205
{
205-
$errorId = null;
206+
$errorId = \null;
206207
$response = $exception->getResponse();
207208
if ($response) {
208-
$data = \GuzzleHttp\json_decode($response->getBody(), true);
209+
$data = guzzle_json_decode($response->getBody(), \true);
209210
$errorId = $data['sys']['id'];
210211
}
211212

@@ -246,7 +247,7 @@ private function getExceptionClass($apiError)
246247
*/
247248
protected function getExceptionNamespace()
248249
{
249-
return null;
250+
return \null;
250251
}
251252

252253
/**

src/Api/BaseQuery.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getQueryData()
105105
'skip' => $this->skip,
106106
'content_type' => $this->contentType,
107107
'mimetype_group' => $this->mimeTypeGroup,
108-
'order' => $this->orderConditions ? \implode(',', $this->orderConditions) : null,
108+
'order' => $this->orderConditions ? \implode(',', $this->orderConditions) : \null,
109109
'select' => $this->select,
110110
'links_to_entry' => $this->linksToEntry,
111111
'links_to_asset' => $this->linksToAsset,
@@ -133,7 +133,7 @@ public function getQueryString()
133133
*/
134134
public function setSkip($skip)
135135
{
136-
if (null !== $skip && $skip < 0) {
136+
if (\null !== $skip && $skip < 0) {
137137
throw new \RangeException(\sprintf(
138138
'Skip value must be 0 or bigger, "%d" given.',
139139
$skip
@@ -156,7 +156,7 @@ public function setSkip($skip)
156156
*/
157157
public function setLimit($limit)
158158
{
159-
if (null !== $limit && ($limit < 1 || $limit > 1000)) {
159+
if (\null !== $limit && ($limit < 1 || $limit > 1000)) {
160160
throw new \RangeException(\sprintf(
161161
'Limit value must be between 0 and 1000, "%d" given.',
162162
$limit
@@ -179,7 +179,7 @@ public function setLimit($limit)
179179
*
180180
* @return $this
181181
*/
182-
public function orderBy($field, $reverse = false)
182+
public function orderBy($field, $reverse = \false)
183183
{
184184
if ($reverse) {
185185
$field = '-'.$field;
@@ -229,7 +229,7 @@ public function setMimeTypeGroup($group)
229229
'code',
230230
'markup',
231231
];
232-
if (null !== $group && !\in_array($group, $validGroups, true)) {
232+
if (\null !== $group && !\in_array($group, $validGroups, \true)) {
233233
throw new \InvalidArgumentException(\sprintf(
234234
'Unknown MIME-type group "%s" given. Expected "%s" or null.',
235235
$group,
@@ -268,7 +268,7 @@ public function setMimeTypeGroup($group)
268268
*
269269
* @return $this
270270
*/
271-
public function where($field, $value, $operator = null)
271+
public function where($field, $value, $operator = \null)
272272
{
273273
$validOperators = [
274274
'ne', // Not equal
@@ -284,7 +284,7 @@ public function where($field, $value, $operator = null)
284284
'near', // Nearby (for locations)
285285
'within', // Within an rectangle (for locations)
286286
];
287-
if (null !== $operator && !\in_array($operator, $validOperators, true)) {
287+
if (\null !== $operator && !\in_array($operator, $validOperators, \true)) {
288288
throw new \InvalidArgumentException(\sprintf(
289289
'Unknown operator "%s" given. Expected "%s" or null.',
290290
$operator,

src/Api/DateTimeImmutable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function formatForJson()
2828
$milliseconds = \floor($date->format('u') / 1000);
2929

3030
if ($milliseconds > 0) {
31-
$result .= '.'.\str_pad((string) $milliseconds, 3, '0', STR_PAD_LEFT);
31+
$result .= '.'.\str_pad((string) $milliseconds, 3, '0', \STR_PAD_LEFT);
3232
}
3333

3434
return $result.'Z';

src/Api/Exception.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use GuzzleHttp\Exception\RequestException;
1313
use Psr\Http\Message\RequestInterface;
1414
use Psr\Http\Message\ResponseInterface;
15+
use function GuzzleHttp\json_decode as guzzle_json_decode;
16+
use function GuzzleHttp\Psr7\parse_request as guzzle_parse_request;
17+
use function GuzzleHttp\Psr7\parse_response as guzzle_parse_response;
18+
use function GuzzleHttp\Psr7\str as guzzle_stringify_message;
1519

1620
/**
1721
* An Exception is thrown when an errors occurs while communicating with the API.
@@ -26,7 +30,7 @@ class Exception extends \RuntimeException implements \Serializable
2630
/**
2731
* @var string|null
2832
*/
29-
private $requestId = null;
33+
private $requestId;
3034

3135
/**
3236
* @var RequestInterface
@@ -69,8 +73,8 @@ public function serialize()
6973
'file' => $this->message,
7074
'line' => $this->line,
7175
'requestId' => $this->requestId,
72-
'request' => \GuzzleHttp\Psr7\str($this->request),
73-
'response' => $this->response ? \GuzzleHttp\Psr7\str($this->response) : null,
76+
'request' => guzzle_stringify_message($this->request),
77+
'response' => $this->response ? guzzle_stringify_message($this->response) : \null,
7478
]);
7579
}
7680

@@ -83,18 +87,18 @@ public function unserialize($serialized)
8387
$this->file = $data['file'];
8488
$this->line = $data['line'];
8589
$this->requestId = $data['requestId'];
86-
$this->request = \GuzzleHttp\Psr7\parse_request($data['request']);
87-
$this->response = $data['response'] ? \GuzzleHttp\Psr7\parse_response($data['response']) : null;
90+
$this->request = guzzle_parse_request($data['request']);
91+
$this->response = $data['response'] ? guzzle_parse_response($data['response']) : \null;
8892
}
8993

90-
private static function createExceptionMessage(RequestException $previous, ResponseInterface $response = null)
94+
private static function createExceptionMessage(RequestException $previous, ResponseInterface $response = \null)
9195
{
9296
if (!$response) {
9397
return $previous->getMessage();
9498
}
9599

96100
try {
97-
$result = \GuzzleHttp\json_decode($response->getBody(), true);
101+
$result = guzzle_json_decode($response->getBody(), \true);
98102
if (isset($result['message'])) {
99103
return $result['message'];
100104
}
@@ -132,7 +136,7 @@ public function getResponse()
132136
*/
133137
public function hasResponse()
134138
{
135-
return null !== $this->response;
139+
return \null !== $this->response;
136140
}
137141

138142
/**

0 commit comments

Comments
 (0)