From 1da43fb38793c6801e6c9d4cbc5dd32be69427e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Mon, 26 Oct 2020 20:34:46 +0100 Subject: [PATCH 1/3] Add native_constant_invocation cs rule --- .php_cs.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/.php_cs.dist b/.php_cs.dist index 234c469ce5..8c5243bb19 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -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, From c84d6bb65528ddf7d40d5dca370c34f091272504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Mon, 26 Oct 2020 20:35:10 +0100 Subject: [PATCH 2/3] Adapt code --- src/Bulk/Action.php | 4 +-- src/Exception/Bulk/ResponseException.php | 4 +-- src/Exception/Connection/HttpException.php | 14 ++++---- src/JSON.php | 4 +-- src/Response.php | 2 +- src/Transport/Guzzle.php | 2 +- src/Transport/Http.php | 42 +++++++++++----------- src/Transport/HttpAdapter.php | 2 +- tests/Base.php | 4 +-- tests/BulkTest.php | 6 ++-- tests/ErrorsCollector.php | 2 +- tests/JSONTest.php | 2 +- tests/Transport/AbstractTransportTest.php | 4 +-- tests/Transport/HttpTest.php | 4 +-- 14 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/Bulk/Action.php b/src/Bulk/Action.php index d6a435eb49..5a7a29313a 100644 --- a/src/Bulk/Action.php +++ b/src/Bulk/Action.php @@ -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)) { @@ -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; } diff --git a/src/Exception/Bulk/ResponseException.php b/src/Exception/Bulk/ResponseException.php index 7e6b304cc9..9be64c358a 100644 --- a/src/Exception/Bulk/ResponseException.php +++ b/src/Exception/Bulk/ResponseException.php @@ -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); @@ -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; diff --git a/src/Exception/Connection/HttpException.php b/src/Exception/Connection/HttpException.php index 98eac8dd3b..3739acf45a 100644 --- a/src/Exception/Connection/HttpException.php +++ b/src/Exception/Connection/HttpException.php @@ -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'; } diff --git a/src/JSON.php b/src/JSON.php index 20481142c6..b1082e3b82 100644 --- a/src/JSON.php +++ b/src/JSON.php @@ -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); @@ -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; } } diff --git a/src/Response.php b/src/Response.php index baeb3f0375..a8d661bad2 100644 --- a/src/Response.php +++ b/src/Response.php @@ -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); } diff --git a/src/Transport/Guzzle.php b/src/Transport/Guzzle.php index 229c98cd55..a0d5c8b31c 100755 --- a/src/Transport/Guzzle.php +++ b/src/Transport/Guzzle.php @@ -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 ) ); diff --git a/src/Transport/Http.php b/src/Transport/Http.php index 363192c81d..0bbfab0c0e 100644 --- a/src/Transport/Http.php +++ b/src/Transport/Http.php @@ -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); @@ -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; @@ -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); @@ -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')) { @@ -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; } } } diff --git a/src/Transport/HttpAdapter.php b/src/Transport/HttpAdapter.php index 62e9eb48ab..0e854c060b 100644 --- a/src/Transport/HttpAdapter.php +++ b/src/Transport/HttpAdapter.php @@ -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; } diff --git a/tests/Base.php b/tests/Base.php index 985a9ff4bf..6ab24888a1 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -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); } /** diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 4adc167534..589f14b0e6 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -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(); @@ -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(); diff --git a/tests/ErrorsCollector.php b/tests/ErrorsCollector.php index 3716d32724..47f0e88ba5 100644 --- a/tests/ErrorsCollector.php +++ b/tests/ErrorsCollector.php @@ -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; } } diff --git a/tests/JSONTest.php b/tests/JSONTest.php index c0962126bd..7de83a6574 100644 --- a/tests/JSONTest.php +++ b/tests/JSONTest.php @@ -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); } diff --git a/tests/Transport/AbstractTransportTest.php b/tests/Transport/AbstractTransportTest.php index 607f36eef8..de4770188c 100644 --- a/tests/Transport/AbstractTransportTest.php +++ b/tests/Transport/AbstractTransportTest.php @@ -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]], ], ]; } diff --git a/tests/Transport/HttpTest.php b/tests/Transport/HttpTest.php index 2184cc7e7e..f14539bda0 100644 --- a/tests/Transport/HttpTest.php +++ b/tests/Transport/HttpTest.php @@ -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'); @@ -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'); From 373eb94510969548b4c41f08776d9a90f3a5df8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Mon, 26 Oct 2020 20:37:22 +0100 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d4d91397..a84bb02515 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)