Skip to content

Conversation

@zjbarg
Copy link

@zjbarg zjbarg commented Oct 27, 2025

No description provided.

@zjbarg
Copy link
Author

zjbarg commented Oct 27, 2025

@prewk can we use @template-covariant?

We are getting errors when running phpstan level 6, like

Method ...someMethod() should return
Prewk\Result<array{report_document_id: string, url: string, compression_algorithm: string|null}, ...some-error-type>
but returns 
Prewk\Result\Ok<array{report_document_id: string, url: string, compression_algorithm: string|null}>.                                               
return.type                                                                             
Template type T on class Prewk\Result is not covariant. Learn more:                      
https://phpstan.org/blog/whats-up-with-template-covariant   

@zjbarg zjbarg marked this pull request as ready for review October 27, 2025 21:53
@prewk
Copy link
Owner

prewk commented Oct 28, 2025

I'm trying to be a supportive but passive maintainer these days. Could you give me an opinion here, @t1sh0o?

@t1sh0o
Copy link
Contributor

t1sh0o commented Nov 12, 2025

@zjbarg I don't think that adding @template-covariant is possible here, because we use the T and E params as input as well. Covariant is fine if we use them only as return types, but when as input params of functions like and, or, etc. it should be contravariant, and as far as I understand variance, you cannot have both co and contra. This is why the errors from psalm.
I also tried to reproduce your case, but it works for me, I guess I ma not doing something like you are. Nevertheless, I would try to use a DTO combined with results, and in most cases, it works.
Here is my simulation. Note that if I hardcode 'compression_algorithm' => null or some string, it errors, but then the error is obvious that both return type and what is returned are not the same, because of them being invariant.

getResult();

/**
 * @return Result<array{report_document_id: string, url: string, compression_algorithm: string|null}, array{status:string}>
 */
function getResult(?string $algo = null): Result
{
    return ok(['report_document_id' => '123', 'url' => 'https://example.com', 'compression_algorithm' => $algo]);
}

@zjbarg zjbarg closed this Nov 25, 2025
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.

3 participants