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

Could not infer a return type for array_reduce #5574

Closed
melihovv opened this issue Apr 4, 2021 · 4 comments
Closed

Could not infer a return type for array_reduce #5574

melihovv opened this issue Apr 4, 2021 · 4 comments

Comments

@melihovv
Copy link

melihovv commented Apr 4, 2021

https://psalm.dev/r/57243f56e5

<?php

/**
 * @template TKey as array-key
 * @template TValue as mixed
 */
abstract class AbstractCollection
{
    /**
     * @readonly
     * @var array<TKey, TValue>
     */
    private array $items;

    /**
     * @param array<TKey, TValue> $items
     */
    final public function __construct(array $items)
    {
        $this->items = $items;
    }

    /**
     * @template TReturn
     * @template TInitial
     * @param callable(TInitial|TReturn|null,TValue):(TInitial|TReturn) $callback
     * @param TInitial|null $initial
     * @return TReturn|TInitial|null
     */    
    public function reduce(callable $callback, $initial = null)
    {
        return array_reduce($this->items, $callback, $initial);
    }
}

Gives

INFO: MixedReturnStatement - 32:16 - Could not infer a return type

INFO: MixedInferredReturnType - 28:16 - Could not verify return type 'TInitial|TReturn|null' for AbstractCollection::reduce

Can it be fixed?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/57243f56e5
<?php

/**
 * @template TKey as array-key
 * @template TValue as mixed
 */
abstract class AbstractCollection
{
    /**
     * @readonly
     * @var array<TKey, TValue>
     */
    private array $items;

    /**
     * @param array<TKey, TValue> $items
     */
    final public function __construct(array $items)
    {
        $this->items = $items;
    }

    /**
     * @template TReturn
     * @template TInitial
     * @param callable(TInitial|TReturn|null,TValue):(TInitial|TReturn) $callback
     * @param TInitial|null $initial
     * @return TReturn|TInitial|null
     */    
    public function reduce(callable $callback, $initial = null)
    {
        return array_reduce($this->items, $callback, $initial);
    }
}
Psalm output (using commit f07b6e6):

INFO: MixedReturnStatement - 32:16 - Could not infer a return type

INFO: MixedInferredReturnType - 28:16 - Could not verify return type 'TInitial|TReturn|null' for AbstractCollection::reduce

@orklah
Copy link
Collaborator

orklah commented Oct 12, 2021

There is ArrayReduceReturnTypeProvider that handles this function's return type. If you want to take a look, please ask and I'll give you some pointers

@weirdan
Copy link
Collaborator

weirdan commented Nov 21, 2022

It seems to be fixed: https://psalm.dev/r/755aef1954

@weirdan weirdan closed this as completed Nov 21, 2022
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/755aef1954
<?php

/**
 * @template TKey as array-key
 * @template TValue as mixed
 */
abstract class AbstractCollection
{
    /**
     * @readonly
     * @var array<TKey, TValue>
     */
    private array $items;

    /**
     * @param array<TKey, TValue> $items
     */
    final public function __construct(array $items)
    {
        $this->items = $items;
    }

    /**
     * @template TReturn
     * @template TInitial
     * @param callable(TInitial|TReturn|null,TValue):(TInitial|TReturn) $callback
     * @param TInitial|null $initial
     * @return TReturn|TInitial|null
     */    
    public function reduce(callable $callback, $initial = null)
    {
        return array_reduce($this->items, $callback, $initial);
    }
}
Psalm output (using commit 7869cb5):

No issues!

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

No branches or pull requests

3 participants