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

Bump PHPStan to level 5 #2108

merged 2 commits into from
Oct 10, 2022

Conversation

franmomu
Copy link
Contributor

@franmomu franmomu commented Aug 4, 2022

Most of the ignored errors are expected because they are testing exceptions using wrong types and PHPStan complains about that. Like:

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

 ------ ---------------------------------------------------------------------------------------------------------------------
  Line   tests/Aggregation/GeohashGridTest.php
 ------ ---------------------------------------------------------------------------------------------------------------------
  56     Parameter #1 $precision of method Elastica\Aggregation\GeohashGrid::setPrecision() expects int|string, float given.
 ------ ---------------------------------------------------------------------------------------------------------------------

The different ones are:

  • The ones in tests/Query/FunctionScoreTest.php, which I think it's fine to add TODO just in case someone has overridden FunctionScore::addDecayFunction() method, if this is fine we can close Allow int and float for FunctionScore::addDecayFunction() origin and scale #2089
  • In ClientFunctionTest, there is a call to Document::setIndex() passing null which is not allowed, that is done to make sure that the index is properly set when deleting the documents. Instead of that a new document can be passed as we do lines above to update them:
    $anonCoin = new Document('1', ['name' => 'anoncoin']);
    $ixCoin = new Document('2', ['name' => 'ixcoin']);
    $index->addDocuments([$anonCoin, $ixCoin]);
    $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']),
    ]);
    $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);
    $index->getDocument(1);
    $index->getDocument(2);

$ixCoin->setIndex(null); // Make sure the index gets set properly if missing
$index->deleteDocuments([$anonCoin, $ixCoin]);
$index->deleteDocuments([
new Document('1', ['name' => 'AnonCoin']),
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you need to recreate the doc here for this change? I also wander why we do it onl line 170/171?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test to reuse the documents and added a couple of tests to check that the index is set if missing (to remove the $ixCoin->setIndex(null); part)

@@ -30,6 +30,16 @@ parameters:
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.

@ruflin ruflin merged commit 1fe34e6 into ruflin:master Oct 10, 2022
@ruflin
Copy link
Owner

ruflin commented Oct 10, 2022

@franmomu Thanks for getting us to phpstan level 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants