Skip to content

Conversation

@drupol
Copy link
Contributor

@drupol drupol commented Oct 18, 2020

This PR:

  • Is a work in progress
  • Add a new class ArraySumFunctionDynamicReturnTypeExtension
  • Update the default configuration and include this class by default
  • Add tests
  • Update more tests, update the Dynamic Return Type mechanism so untyped $parameters would work fine as well.

Fix phpstan/phpstan#3964

Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
@drupol drupol marked this pull request as ready for review October 18, 2020 19:46
if ($keyType instanceof UnionType) {
$floatType = new FloatType();

if ($keyType->accepts($floatType, true)->yes()) {
Copy link
Member

Choose a reason for hiding this comment

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

The basic logic that accounts for most use-cases automatically should be as followed:

$isInteger = (new IntegerType())->isSuperTypeOf($argType); $isFloat = (new FloatType())->isSuperTypeOf($argType); $comparison = $isInteger->compareTo($isFloat); if ($comparison === $isInteger) { return new IntegerType(); } elseif ($comparison === $isFloat) { return new FloatType(); } return new UnionType([new IntegerType(), new FloatType()]);
Copy link
Contributor Author

@drupol drupol Oct 18, 2020

Choose a reason for hiding this comment

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

$comparison is always null if I try this snippet.

Copy link
Member

Choose a reason for hiding this comment

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

That shouldn't be the case.

Copy link
Member

Choose a reason for hiding this comment

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

Have you fetched the getIterableValueType() so that you're comparing what's inside the array?

Copy link
Member

Choose a reason for hiding this comment

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

The goal is to also handle cases like array_sum([1, 2, 3]) or array_sum($mixed).


public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$argType = $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.

Analysing array_sum() call will crash on this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But tests are passing... how come?

Copy link
Member

Choose a reason for hiding this comment

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

Because you're not testing calling array_sum() without any arguments.

@ondrejmirtes ondrejmirtes force-pushed the master branch 2 times, most recently from f54fd5f to eca550a Compare October 28, 2020 19:41
@ondrejmirtes
Copy link
Member

Feel free to reopen if you ever decide to finish this PR, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants