Skip to content
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

Add native_constant_invocation CS rule #1833

Merged
merged 3 commits into from
Oct 27, 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
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ return PhpCsFixer\Config::create()
'array_syntax' => ['syntax' => 'short'],
'is_null' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'native_constant_invocation' => true,
'native_function_invocation' => true,
'no_alias_functions' => true,
'no_useless_else' => true,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added support for defining a connection pool with DSN. Example: `pool(http://127.0.0.1 http://127.0.0.2/bar?timeout=4)` [#1808](https://github.com/ruflin/Elastica/pull/1808)
* Added `Elastica\Aggregation\Composite` aggregation [#1804](https://github.com/ruflin/Elastica/pull/1804)
* Added `symfony/deprecation-contracts` package to handle deprecations [#1823](https://github.com/ruflin/Elastica/pull/1823)
* Added `native_constant_invocation` CS rule [#1833](https://github.com/ruflin/Elastica/pull/1833)
### Changed
* Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791)
* Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791)
Expand Down
4 changes: 2 additions & 2 deletions src/Bulk/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function toArray(): array

public function toString(): string
{
$string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT).Bulk::DELIMITER;
$string = JSON::stringify($this->getActionMetadata(), \JSON_FORCE_OBJECT).Bulk::DELIMITER;
if ($this->hasSource()) {
$source = $this->getSource();
if (\is_string($source)) {
Expand All @@ -167,7 +167,7 @@ public function toString(): string
}
$string .= '{"doc": '.$source['doc'].$docAsUpsert.'}';
} else {
$string .= JSON::stringify($source, JSON_UNESCAPED_UNICODE);
$string .= JSON::stringify($source, \JSON_UNESCAPED_UNICODE);
}
$string .= Bulk::DELIMITER;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/Bulk/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ResponseSet $responseSet)
{
$this->_init($responseSet);

$message = 'Error in one or more bulk request actions:'.PHP_EOL.PHP_EOL;
$message = 'Error in one or more bulk request actions:'.\PHP_EOL.\PHP_EOL;
$message .= $this->getActionExceptionsAsString();

parent::__construct($message);
Expand Down Expand Up @@ -70,7 +70,7 @@ public function getActionExceptionsAsString(): string
{
$message = '';
foreach ($this->getActionExceptions() as $actionException) {
$message .= $actionException->getMessage().PHP_EOL;
$message .= $actionException->getMessage().\PHP_EOL;
}

return $message;
Expand Down
14 changes: 7 additions & 7 deletions src/Exception/Connection/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public function __construct($error, ?Request $request = null, ?Response $respons
public function getErrorMessage(int $error): string
{
switch ($error) {
case CURLE_UNSUPPORTED_PROTOCOL:
case \CURLE_UNSUPPORTED_PROTOCOL:
return 'Unsupported protocol';
case CURLE_FAILED_INIT:
case \CURLE_FAILED_INIT:
return 'Internal cUrl error?';
case CURLE_URL_MALFORMAT:
case \CURLE_URL_MALFORMAT:
return 'Malformed URL';
case CURLE_COULDNT_RESOLVE_PROXY:
case \CURLE_COULDNT_RESOLVE_PROXY:
return "Couldn't resolve proxy";
case CURLE_COULDNT_RESOLVE_HOST:
case \CURLE_COULDNT_RESOLVE_HOST:
return "Couldn't resolve host";
case CURLE_COULDNT_CONNECT:
case \CURLE_COULDNT_CONNECT:
return "Couldn't connect to host, Elasticsearch down?";
case CURLE_OPERATION_TIMEOUTED:
case \CURLE_OPERATION_TIMEOUTED:
return 'Operation timed out';
}

Expand Down
4 changes: 2 additions & 2 deletions src/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function stringify($args/* inherit from json_encode */)
$args = \func_get_args();

// set defaults
isset($args[1]) ? $args[1] |= JSON_PRESERVE_ZERO_FRACTION : $args[1] = JSON_PRESERVE_ZERO_FRACTION;
isset($args[1]) ? $args[1] |= \JSON_PRESERVE_ZERO_FRACTION : $args[1] = \JSON_PRESERVE_ZERO_FRACTION;

// run encode and output
$string = \call_user_func_array('json_encode', $args);
Expand All @@ -86,6 +86,6 @@ public static function stringify($args/* inherit from json_encode */)
*/
private static function getJsonLastErrorMsg()
{
return JSON_ERROR_NONE !== \json_last_error() ? \json_last_error_msg() : false;
return \JSON_ERROR_NONE !== \json_last_error() ? \json_last_error_msg() : false;
}
}
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function getData()

try {
if ($this->getJsonBigintConversion()) {
$response = JSON::parse($response, true, 512, JSON_BIGINT_AS_STRING);
$response = JSON::parse($response, true, 512, \JSON_BIGINT_AS_STRING);
} else {
$response = JSON::parse($response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function _createPsr7Request(Request $request, Connection $connection)
$req = $req->withBody(
Psr7\stream_for(
\is_array($data)
? JSON::stringify($data, JSON_UNESCAPED_UNICODE)
? JSON::stringify($data, \JSON_UNESCAPED_UNICODE)
: $data
)
);
Expand Down
42 changes: 21 additions & 21 deletions src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,30 @@ public function exec(Request $request, array $params): Response
);
}

\curl_setopt($conn, CURLOPT_URL, $baseUri);
\curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
\curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
\curl_setopt($conn, \CURLOPT_URL, $baseUri);
\curl_setopt($conn, \CURLOPT_TIMEOUT, $connection->getTimeout());
\curl_setopt($conn, \CURLOPT_FORBID_REUSE, 0);

// Tell ES that we support the compressed responses
// An "Accept-Encoding" header containing all supported encoding types is sent
// curl will decode the response automatically if the response is encoded
\curl_setopt($conn, CURLOPT_ENCODING, '');
\curl_setopt($conn, \CURLOPT_ENCODING, '');

/* @see Connection::setConnectTimeout() */
$connectTimeout = $connection->getConnectTimeout();
if ($connectTimeout > 0) {
\curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
\curl_setopt($conn, \CURLOPT_CONNECTTIMEOUT, $connectTimeout);
}

if (null !== $proxy = $connection->getProxy()) {
\curl_setopt($conn, CURLOPT_PROXY, $proxy);
\curl_setopt($conn, \CURLOPT_PROXY, $proxy);
}

$username = $connection->getUsername();
$password = $connection->getPassword();
if (null !== $username && null !== $password) {
\curl_setopt($conn, CURLOPT_HTTPAUTH, $this->_getAuthType());
\curl_setopt($conn, CURLOPT_USERPWD, "{$username}:{$password}");
\curl_setopt($conn, \CURLOPT_HTTPAUTH, $this->_getAuthType());
\curl_setopt($conn, \CURLOPT_USERPWD, "{$username}:{$password}");
}

$this->_setupCurl($conn);
Expand All @@ -124,7 +124,7 @@ public function exec(Request $request, array $params): Response
}

if (\is_array($data)) {
$content = JSON::stringify($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$content = JSON::stringify($data, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES);
} else {
$content = $data;

Expand All @@ -135,22 +135,22 @@ public function exec(Request $request, array $params): Response
\array_push($headers, \sprintf('Content-Type: %s', $request->getContentType()));
if ($connection->hasCompression()) {
// Compress the body of the request ...
\curl_setopt($conn, CURLOPT_POSTFIELDS, \gzencode($content));
\curl_setopt($conn, \CURLOPT_POSTFIELDS, \gzencode($content));

// ... and tell ES that it is compressed
\array_push($headers, 'Content-Encoding: gzip');
} else {
\curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
\curl_setopt($conn, \CURLOPT_POSTFIELDS, $content);
}
} else {
\curl_setopt($conn, CURLOPT_POSTFIELDS, '');
\curl_setopt($conn, \CURLOPT_POSTFIELDS, '');
}

\curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
\curl_setopt($conn, \CURLOPT_HTTPHEADER, $headers);

\curl_setopt($conn, CURLOPT_NOBODY, 'HEAD' == $httpMethod);
\curl_setopt($conn, \CURLOPT_NOBODY, 'HEAD' == $httpMethod);

\curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $httpMethod);
\curl_setopt($conn, \CURLOPT_CUSTOMREQUEST, $httpMethod);

$start = \microtime(true);

Expand All @@ -164,7 +164,7 @@ public function exec(Request $request, array $params): Response
// Checks if error exists
$errorNumber = \curl_errno($conn);

$response = new Response($responseString, \curl_getinfo($conn, CURLINFO_HTTP_CODE));
$response = new Response($responseString, \curl_getinfo($conn, \CURLINFO_HTTP_CODE));
$response->setQueryTime($end - $start);
$response->setTransferInfo(\curl_getinfo($conn));
if ($connection->hasConfig('bigintConversion')) {
Expand Down Expand Up @@ -220,19 +220,19 @@ protected function _getAuthType()
{
switch ($this->_connection->getAuthType()) {
case 'digest':
return CURLAUTH_DIGEST;
return \CURLAUTH_DIGEST;
break;
case 'gssnegotiate':
return CURLAUTH_GSSNEGOTIATE;
return \CURLAUTH_GSSNEGOTIATE;
break;
case 'ntlm':
return CURLAUTH_NTLM;
return \CURLAUTH_NTLM;
break;
case 'basic':
return CURLAUTH_BASIC;
return \CURLAUTH_BASIC;
break;
default:
return CURLAUTH_ANY;
return \CURLAUTH_ANY;
}
}
}
2 changes: 1 addition & 1 deletion src/Transport/HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function _createHttpAdapterRequest(ElasticaRequest $elasticaRequest, C
}

if (\is_array($data)) {
$body = JSON::stringify($data, JSON_UNESCAPED_UNICODE);
$body = JSON::stringify($data, \JSON_UNESCAPED_UNICODE);
} else {
$body = $data;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ protected function tearDown(): void

protected static function hideDeprecated(): void
{
\error_reporting(\error_reporting() & ~E_USER_DEPRECATED);
\error_reporting(\error_reporting() & ~\E_USER_DEPRECATED);
}

protected static function showDeprecated(): void
{
\error_reporting(\error_reporting() | E_USER_DEPRECATED);
\error_reporting(\error_reporting() | \E_USER_DEPRECATED);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function testSend(): void
{"name":"The Thing"}
';

$expected = \str_replace(PHP_EOL, "\n", $expected);
$this->assertEquals($expected, (string) \str_replace(PHP_EOL, "\n", (string) $bulk));
$expected = \str_replace(\PHP_EOL, "\n", $expected);
$this->assertEquals($expected, (string) \str_replace(\PHP_EOL, "\n", (string) $bulk));

$response = $bulk->send();

Expand Down Expand Up @@ -387,7 +387,7 @@ public function testRawDocumentDataRequest(): void
{"index":{}}
{"name":"The Human Torch"}
';
$expectedJson = \str_replace(PHP_EOL, "\n", $expectedJson);
$expectedJson = \str_replace(\PHP_EOL, "\n", $expectedJson);
$this->assertEquals($expectedJson, $bulk->toString());

$response = $bulk->send();
Expand Down
2 changes: 1 addition & 1 deletion tests/ErrorsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getDeprecatedCount()
$count = 0;

foreach ($this->errors as $error) {
if (E_USER_DEPRECATED === $error[0]) {
if (\E_USER_DEPRECATED === $error[0]) {
++$count;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testStringifyMustThrowExceptionNanOrInf(): void
$this->expectException(\Elastica\Exception\JSONParseException::class);
$this->expectExceptionMessage('Inf and NaN cannot be JSON encoded');

$arr = [NAN, INF];
$arr = [\NAN, \INF];
JSON::stringify($arr);
$this->assertTrue(true);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Transport/AbstractTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function getTransport()
{
return [
[
['transport' => 'Http', 'curl' => [CURLINFO_HEADER_OUT => true]],
['transport' => 'Http', 'curl' => [\CURLINFO_HEADER_OUT => true]],
],
[
['transport' => 'Guzzle', 'curl' => [CURLINFO_HEADER_OUT => true]],
['transport' => 'Guzzle', 'curl' => [\CURLINFO_HEADER_OUT => true]],
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Transport/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function testBodyReuse(): void
*/
public function testRequestSuccessWithHttpCompressionEnabled(): void
{
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => true, 'curl' => [CURLINFO_HEADER_OUT => true]]]);
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => true, 'curl' => [\CURLINFO_HEADER_OUT => true]]]);

$index = $client->getIndex('elastica_request_with_body_and_http_compression_enabled');

Expand All @@ -198,7 +198,7 @@ public function testRequestSuccessWithHttpCompressionEnabled(): void
*/
public function testRequestSuccessWithHttpCompressionDisabled(): void
{
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => false, 'curl' => [CURLINFO_HEADER_OUT => true]]]);
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => false, 'curl' => [\CURLINFO_HEADER_OUT => true]]]);

$index = $client->getIndex('elastica_request_with_body_and_http_compression_disabled');

Expand Down