2

Given my class

<?php
declare(strict_types=1);

use Illuminate\Support\Collection;
use stdClass;

class PhpstanIssue
{
    /**
     * @param Collection<Collection<stdClass>> $collection
     *
     * @return Collection<Foo>
     */
    public function whyDoesThisFail(Collection $collection): Collection
    {
        return $collection
            ->flatten() // Collection<stdClass>
            ->map(static function (\stdClass $std): ?Foo {
                return Foo::get($std);
            }) // should now be Collection<?Foo>
            ->filter(); // should now be Collection<Foo>
    }
}

I am highely confused why phpstan (0.12.64) would fail with:

18: [ERROR] Method PhpstanIssue::whyDoesThisFail() should return
Illuminate\Support\Collection&iterable<Foo> but returns 
Illuminate\Support\Collection&iterable<Illuminate\Support\Collection&iterable<stdClass>>. (phpstan)

Why can't phpstan infer the proper result type of this pipe? How can I make phpstan understand the pipe?


I can verify that my code works within a phpunit testcase:

class MyCodeWorks extends TestCase
{
    public function testPipeline()
    {
        $result = (new PhpstanIssue())->whyDoesThisFail(
            new Collection(
                [
                    new Collection([new \stdClass(), new \stdClass()]),
                    new Collection([new \stdClass()]),
                ]
            )
        );

        self::assertCount(3, $result);
        foreach ($result as $item) {
            self::assertInstanceOf(Foo::class, $item);
        }
    }
}

will pass.


My Foo is just a dummy class for the sake of this question. It's only relevant that it takes a stdClass instance and transforms it into a ?Foo one.

class Foo
{
    public static function get(\stdClass $std): ?Foo
    {
        // @phpstan-ignore-next-line
        return (bool) $std ? new static() : null;
    }
}

6
  • Illuminate\Support\Collection is not a generic class by default. Are you also using Larastan?
    – Can Vural
    Jul 27, 2021 at 11:50
  • @CanVural No, I am currently not using Larastan. Could it be achieved by using it?
    – k0pernikus
    Jul 27, 2021 at 13:29
  • 1
    Yes, Larastan has stub files that marks the Collection class as generic. It should work with your example. Although it also needs a key type. So something like Collection<int, Collection<int, stdClass>> But if you don't want to use it, you can also just create your stub files that adds the generic PHPDocs to the Collection class.
    – Can Vural
    Jul 27, 2021 at 13:35
  • @CanVural I'd accept that as an answer for upvote / approve btw :)
    – k0pernikus
    Jul 27, 2021 at 16:35
  • 1
    I added an answer. How to register that stub file with PHPStan is explained in the docs (which I linked in answer), so I didn't repeat it here.
    – Can Vural
    Jul 27, 2021 at 18:35

1 Answer 1

5

Illuminate\Support\Collection class is not generic on its own. So writing Collection<Foo> is wrong. That causes the error messages like Illuminate\Support\Collection&iterable<Illuminate\Support\Collection&iterable<stdClass>>

You have two options:

  1. Installing Larastan. It's a PHPStan extension for Laravel. And it has stub files that makes Illuminate\Support\Collection class generic.

  2. Or if you are just using the illuminate/collections standalone package without full Laravel app you can write your own stub files. From PHPStan docs:

... you can write a stub file with the right PHPDoc. It’s like source code, but PHPStan only reads PHPDocs from it. So the namespace and class/interface/trait/method/function names must match with the original source you’re describing. But method bodies can stay empty, PHPStan is only interested in the PHPDocs.

For your example the following stub file should be enough:

<?php

namespace Illuminate\Support;

/**
 * @template TKey
 * @template TValue
 * @implements \ArrayAccess<TKey, TValue>
 * @implements Enumerable<TKey, TValue>
 */
class Collection implements \ArrayAccess, Enumerable
{
    /**
     * @template TReturn
     * @param callable(TValue, TKey): TReturn $callable
     * @return static<TKey, TReturn>
     */
    public function map($callable) {}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.