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

Bump PHPStan to level 5 #2108

Merged
merged 2 commits into from
Oct 10, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
105 changes: 103 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

-
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to get my head around why we don't fix these in the test files directly? Related to breaking changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what it was explained in the PR description #2108 (comment), a test like this:

public function testGeohashGridAggregationWithNotAllowedPrecision(): void
{
$this->expectException(\TypeError::class);
$agg = new GeohashGrid('hash', 'location');
$agg->setPrecision(1.5);
}

makes PHPStan fails because setPrecision only accepts int|string:

* @param int|string $precision an integer between 1 and 12, inclusive. Defaults to 5 or distance like 1km, 10m
*
* @return $this
*/
public function setPrecision($precision): self
{
if (!\is_int($precision) && !\is_string($precision)) {
throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be of type int|string, %s given.', __METHOD__, \is_object($precision) ? \get_class($precision) : \gettype($precision)));
}

We can get rid of these ignored errors and checks in the next major if we add type parameter declarations.

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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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\\<Elastica\\\\Query\\\\AbstractSpanQuery\\>, array\\<int, Elastica\\\\Query\\\\Term\\> given\\.$#"
count: 1
path: tests/Query/SpanNearTest.php

-
message: "#^Parameter \\#1 \\$clauses of class Elastica\\\\Query\\\\SpanOr constructor expects array\\<Elastica\\\\Query\\\\AbstractSpanQuery\\>, array\\<int, Elastica\\\\Query\\\\Term\\> 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
Expand All @@ -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\\<Elastica\\\\Index\\>, array\\<int, Elastica\\\\Index\\|string\\> given\\.$#"
count: 1
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndices\\(\\) expects array\\<Elastica\\\\Index\\>, array\\<int, stdClass\\> given\\.$#"
count: 1
path: tests/SearchTest.php

-
message: "#^Parameter \\#1 \\$indices of method Elastica\\\\Search\\:\\:addIndicesByName\\(\\) expects array\\<string\\>, array\\<int, stdClass\\> 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

2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 5
paths:
- src
- tests
Expand Down
2 changes: 2 additions & 0 deletions src/Query/FunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,14 @@ 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'));

$ixCoin->setIndex(null); // Make sure the index gets set properly if missing
$index->deleteDocuments([$anonCoin, $ixCoin]);

$this->expectException(NotFoundException::class);
Expand Down
34 changes: 34 additions & 0 deletions tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Transport/TransportBenchmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down