Skip to content

Add @template-covariant to error on Result Interface#1

Open
TheDevick wants to merge 1 commit into
shipsaas:mainfrom
TheDevick:patch-1
Open

Add @template-covariant to error on Result Interface#1
TheDevick wants to merge 1 commit into
shipsaas:mainfrom
TheDevick:patch-1

Conversation

@TheDevick

Copy link
Copy Markdown

Hello!
I have a project where I have:
src/Http/Exceptions/ApiException.php
src/Http/Exceptions/ApiNotFoundException.php

Where ApiNotFound extends ApiException.

In a method, like:

class BookService
{
    /*
     * @return ResultInterface<Book, ApiException>
     */
    public function create(int $authorId, string $name, string $content): ResultInterface<Book, ApiException>
    {
        $author = ...

        if(!$author) // Author not found
        {
            return new Err(new ApiNotFoundException());
        }

        // ...
    }
}

Psalm tells me:

ERROR: InvalidReturnType - src/Service/BookService.php - The declared return type 'NeverThrow\ResultInterface<App\Entity\Book, App\Http\Exception\ApiException>' for App\Service\BookService::create is incorrect, got 'NeverThrow\Err<App\Http\Exception\ApiException>|NeverThrow\Err<App\Http\Exception\NotFoundException>|NeverThrow\Ok<App\Entity\Book>' (see https://psalm.dev/011) * @return ResultInterface<Book, ApiException>

ERROR: InvalidReturnStatement - src/Service/BookService.php - The inferred type 'NeverThrow\Err<App\Http\Exception\NotFoundException>' does not match the declared return type 'NeverThrow\ResultInterface<App\Entity\Book, App\Http\Exception\ApiException>' for Appsrc/Service/BookService::create (see https://psalm.dev/128) return new Err(new NotFoundException('Author not found')));

Solutions

Simply change the template from the error from Template to Template Covariant

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant