From 0c534a397d771434e7e9069b783a9173de28e6aa Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 4 Aug 2022 16:00:15 +0200 Subject: [PATCH 1/2] Bump PHPStan to level 5 --- CHANGELOG.md | 1 + phpstan-baseline.neon | 105 ++++++++++++++++++++- phpstan.neon | 2 +- src/Query/FunctionScore.php | 2 + tests/ClientFunctionalTest.php | 6 +- tests/Transport/TransportBenchmarkTest.php | 2 +- 6 files changed, 112 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71bf4540af..48901baa2e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Changed `Elastica\Index\Settings::get` adding ability to get default settings by @krasilnikovm [#2115](https://github.com/ruflin/Elastica/pull/2115) * Update `AWSAuthV4 transport` to sanitize host name for AWS requests before signing [#2090](https://github.com/ruflin/Elastica/pull/2090) * New method `Elastica\Aggregation\Terms::setMissingBucket`. For Composite Agg. Include in the response documents without a value for a given source. [#2117](https://github.com/ruflin/Elastica/pull/2117) +* Increased `PHPStan` level to `5` by @franmomu [#2108](https://github.com/ruflin/Elastica/pull/2108) ### Deprecated ### Removed * Removed `CallbackStrategyTestHelper` and `ErrorsCollector` from `tests` [#2111](https://github.com/ruflin/Elastica/pull/2111) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f41a145076..03c6306f57 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -25,11 +25,26 @@ parameters: count: 1 path: src/QueryBuilder.php + - + message: "#^Function GuzzleHttp\\\\Psr7\\\\modify_request not found\\.$#" + count: 1 + path: src/Transport/AwsAuthV4.php + - message: "#^Function GuzzleHttp\\\\Psr7\\\\stream_for not found\\.$#" count: 1 path: src/Transport/Guzzle.php + - + message: "#^Parameter \\#1 \\$precision of method Elastica\\\\Aggregation\\\\GeohashGrid\\:\\:setPrecision\\(\\) expects int\\|string, float given\\.$#" + count: 1 + path: tests/Aggregation/GeohashGridTest.php + + - + message: "#^Parameter \\#1 \\$data of static method Elastica\\\\Bulk\\\\Action\\\\AbstractDocument\\:\\:create\\(\\) expects Elastica\\\\Document\\|Elastica\\\\Script\\\\AbstractScript, stdClass given\\.$#" + count: 1 + path: tests/Bulk/Action/AbstractDocumentTest.php + - message: "#^Access to an undefined property Elastica\\\\Document\\:\\:\\$counter\\.$#" count: 1 @@ -55,6 +70,16 @@ parameters: count: 1 path: tests/ClientFunctionalTest.php + - + message: "#^Parameter \\#2 \\$data of method Elastica\\\\Client\\:\\:updateDocument\\(\\) expects array\\|Elastica\\\\Document\\|Elastica\\\\Script\\\\AbstractScript, stdClass given\\.$#" + count: 1 + path: tests/ClientFunctionalTest.php + + - + message: "#^Parameter \\#1 \\$params of static method Elastica\\\\Connection\\:\\:create\\(\\) expects array\\|Elastica\\\\Connection, string given\\.$#" + count: 1 + path: tests/ConnectionTest.php + - message: "#^Access to an undefined property Elastica\\\\Document\\:\\:\\$field1\\.$#" count: 1 @@ -95,6 +120,11 @@ parameters: count: 1 path: tests/DocumentTest.php + - + message: "#^Parameter \\#2 \\$name of class Elastica\\\\IndexTemplate constructor expects string, null given\\.$#" + count: 1 + path: tests/IndexTemplateTest.php + - message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 @@ -105,6 +135,36 @@ parameters: count: 2 path: tests/Multi/SearchTest.php + - + message: "#^Parameter \\#3 \\$origin of method Elastica\\\\Query\\\\FunctionScore\\:\\:addDecayFunction\\(\\) expects string, int given\\.$#" + count: 4 + path: tests/Query/FunctionScoreTest.php + + - + message: "#^Parameter \\#4 \\$scale of method Elastica\\\\Query\\\\FunctionScore\\:\\:addDecayFunction\\(\\) expects string, float given\\.$#" + count: 2 + path: tests/Query/FunctionScoreTest.php + + - + message: "#^Parameter \\#4 \\$scale of method Elastica\\\\Query\\\\FunctionScore\\:\\:addDecayFunction\\(\\) expects string, int given\\.$#" + count: 2 + path: tests/Query/FunctionScoreTest.php + + - + message: "#^Parameter \\#1 \\$clauses of class Elastica\\\\Query\\\\SpanNear constructor expects array\\, array\\ given\\.$#" + count: 1 + path: tests/Query/SpanNearTest.php + + - + message: "#^Parameter \\#1 \\$clauses of class Elastica\\\\Query\\\\SpanOr constructor expects array\\, array\\ given\\.$#" + count: 1 + path: tests/Query/SpanOrTest.php + + - + message: "#^Parameter \\#1 \\$term of method Elastica\\\\Query\\\\Terms\\:\\:addTerm\\(\\) expects bool\\|float\\|int\\|string, stdClass given\\.$#" + count: 1 + path: tests/Query/TermsTest.php + - message: "#^Call to an undefined method ReflectionType\\:\\:getName\\(\\)\\.$#" count: 2 @@ -125,17 +185,58 @@ parameters: count: 1 path: tests/QueryBuilderTest.php + - + message: "#^Parameter \\#1 \\$value of method Elastica\\\\Reindex\\:\\:setWaitForCompletion\\(\\) expects bool, string given\\.$#" + count: 1 + path: tests/ReindexTest.php + - message: "#^Expression \"\\$index\\-\\>search\\('elastica search'\\)\\[3\\]\" on a separate line does not do anything\\.$#" count: 1 path: tests/ResultSetTest.php + - + message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, int given\\.$#" + count: 1 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, stdClass given\\.$#" + count: 1 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:addIndex\\(\\) expects Elastica\\\\Index, string given\\.$#" + count: 1 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$index of method Elastica\\\\Search\\:\\:hasIndex\\(\\) expects Elastica\\\\Index, string given\\.$#" + count: 2 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndices\\(\\) expects array\\, array\\ given\\.$#" + count: 1 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndices\\(\\) expects array\\, array\\ given\\.$#" + count: 1 + path: tests/SearchTest.php + + - + message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndicesByName\\(\\) expects array\\, array\\ given\\.$#" + count: 1 + path: tests/SearchTest.php + - message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 path: tests/SnapshotTest.php - - message: "#^Function GuzzleHttp\\\\Psr7\\\\modify_request not found\\.$#" + message: "#^Parameter \\#1 \\$suggestion of static method Elastica\\\\Suggest\\:\\:create\\(\\) expects Elastica\\\\Suggest\\|Elastica\\\\Suggest\\\\AbstractSuggest, Elastica\\\\Query\\\\BoolQuery given\\.$#" count: 1 - path: src/Transport/AwsAuthV4.php + path: tests/SuggestTest.php + diff --git a/phpstan.neon b/phpstan.neon index 6b5b0fc58b..7067173597 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,7 +3,7 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 5 paths: - src - tests diff --git a/src/Query/FunctionScore.php b/src/Query/FunctionScore.php index 9f46d4b36b..8fde70f3da 100644 --- a/src/Query/FunctionScore.php +++ b/src/Query/FunctionScore.php @@ -110,6 +110,8 @@ public function addScriptScoreFunction(AbstractScript $script, ?AbstractQuery $f /** * Add a decay function to the query. * + * TODO: Change "$origin" and "$scale" parameter types to allow "float|int|string". + * * @param string $function see DECAY_* constants for valid options * @param string $field the document field on which to perform the decay function * @param string $origin the origin value for this decay function diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index 722964a8ac..f13d2e9783 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -174,8 +174,10 @@ public function testBulkIndex(): void $this->assertEquals('AnonCoin', $index->getDocument(1)->get('name')); $this->assertEquals('iXcoin', $index->getDocument(2)->get('name')); - $ixCoin->setIndex(null); // Make sure the index gets set properly if missing - $index->deleteDocuments([$anonCoin, $ixCoin]); + $index->deleteDocuments([ + new Document('1', ['name' => 'AnonCoin']), + new Document('2', ['name' => 'iXcoin']), + ]); $this->expectException(NotFoundException::class); $index->getDocument(1); diff --git a/tests/Transport/TransportBenchmarkTest.php b/tests/Transport/TransportBenchmarkTest.php index 04be5dc277..e5d72a1f99 100644 --- a/tests/Transport/TransportBenchmarkTest.php +++ b/tests/Transport/TransportBenchmarkTest.php @@ -45,7 +45,7 @@ public function testAddDocument(array $config, string $transport): void $times = []; for ($i = 0; $i < $this->_max; ++$i) { - $data = $this->getData($i); + $data = $this->getData((string) $i); $doc = new Document((string) $i, $data); $result = $index->addDocument($doc); $times[] = $result->getQueryTime(); From 2cd3feee545136728523d43a79e787faa5af5e9a Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sun, 2 Oct 2022 22:50:24 +0200 Subject: [PATCH 2/2] Add test for Index::updateDocuments and Index::deleteDocuments --- tests/ClientFunctionalTest.php | 13 +++++-------- tests/IndexTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index f13d2e9783..712f67de7b 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -166,18 +166,15 @@ public function testBulkIndex(): void $this->assertEquals('anoncoin', $index->getDocument(1)->get('name')); $this->assertEquals('ixcoin', $index->getDocument(2)->get('name')); - $index->updateDocuments([ - new Document('1', ['name' => 'AnonCoin']), - new Document('2', ['name' => 'iXcoin']), - ]); + $anonCoin->set('name', 'AnonCoin'); + $ixCoin->set('name', 'iXcoin'); + + $index->updateDocuments([$anonCoin, $ixCoin]); $this->assertEquals('AnonCoin', $index->getDocument(1)->get('name')); $this->assertEquals('iXcoin', $index->getDocument(2)->get('name')); - $index->deleteDocuments([ - new Document('1', ['name' => 'AnonCoin']), - new Document('2', ['name' => 'iXcoin']), - ]); + $index->deleteDocuments([$anonCoin, $ixCoin]); $this->expectException(NotFoundException::class); $index->getDocument(1); diff --git a/tests/IndexTest.php b/tests/IndexTest.php index a7ff55fc6d..604837d9cb 100644 --- a/tests/IndexTest.php +++ b/tests/IndexTest.php @@ -175,6 +175,23 @@ public function testCountGet(): void $this->assertEquals(1, $index->count($query, Request::GET)); } + /** + * @group unit + */ + public function testDeleteDocumentsSetsIndex(): void + { + $client = $this->createMock(Client::class); + $index = new Index($client, 'test'); + + $doc1 = new Document('1', ['name' => 'ruflin']); + $doc2 = new Document('2', ['name' => 'nicolas']); + + $index->deleteDocuments([$doc1, $doc2]); + + $this->assertEquals('test', $doc1->getIndex()); + $this->assertEquals('test', $doc2->getIndex()); + } + /** * @group functional */ @@ -330,6 +347,23 @@ public function testDeleteByQueryWithQueryAndOptions(): void $this->assertEquals(0, $response->count()); } + /** + * @group unit + */ + public function testUpdateDocumentsSetsIndex(): void + { + $client = $this->createMock(Client::class); + $index = new Index($client, 'test'); + + $doc1 = new Document('1', ['name' => 'ruflin']); + $doc2 = new Document('2', ['name' => 'nicolas']); + + $index->updateDocuments([$doc1, $doc2]); + + $this->assertEquals('test', $doc1->getIndex()); + $this->assertEquals('test', $doc2->getIndex()); + } + /** * @group functional */