Hi,

I found this issue with generics and default type:

<?php

declare(strict_types=1);

abstract class AbstractStringable implements Stringable
{
    public function __tostring(): string
    {
        return 'message';
    }
}

/**
 * @template S of AbstractStringable
 */
abstract class AbstractFinder
{
    /**
     * @return false|S
     */
    public function search(): false|AbstractStringable
    {
        return false;
    }
}

final class Finder extends AbstractFinder {}

$finder = new Finder();
$result = $finder->search();

echo "Result: {$result}";

Got error PHP0410 because the default return type is not correct.
It should be false|AbstractStringable (or bool|AbstractStringable).

Of course, the default type must be used when S does not have any other type, or more specific type.

Thank you.

Write a Reply...