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 ArraySum dynamic return type extension #490

Merged
merged 3 commits into from
Apr 2, 2021

Conversation

VincentLanglet
Copy link
Contributor

No description provided.

@@ -178,4 +178,16 @@
$fooArray = ['foo'];
$poppedFoo = array_pop($fooArray);

/** @var int[] $integers */
$sumInts = array_sum($integers);
Copy link
Member

Choose a reason for hiding this comment

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

Please use the modern way of testing this, look at NodeScopeResolver::testFileAsserts.

return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$arrayType = $scope->getType($functionCall->args[0]->value);
Copy link
Member

Choose a reason for hiding this comment

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

Empty array always returns 0, this extension should honor that. You should ask for $arrayType->isIterableAtLeastOnce()->no().

return $itemType;
}

return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
Copy link
Member

Choose a reason for hiding this comment

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

Instead of the rest of the function, you can do just return TypeCombinator::intersect($itemType, new UnionType([new IntegerType(), new FloatType()])).

@VincentLanglet VincentLanglet force-pushed the arraySUm branch 3 times, most recently from 60ef5e9 to a20d860 Compare April 2, 2021 00:14
@VincentLanglet
Copy link
Contributor Author

It should be ok now @ondrejmirtes :)

@ondrejmirtes ondrejmirtes merged commit faa835b into phpstan:master Apr 2, 2021
@ondrejmirtes
Copy link
Member

Thank you!

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