Closed dakorpar closed 4 years ago
Hi, you'd be able to achieve this with generics (https://phpstan.org/blog/generics-in-php-using-phpdocs) and custom stub files (https://phpstan.org/user-guide/stub-files).
excellent, that was fast, thanks!
@ondrejmirtes actually I closed this to early, how would I actually define this since it's called in fluent interface and later it returns every time a different class. Basically setRowClass is allways important, I don't really see how could I do that in an easy way to cover all methods like that with one stub? Isn't this something that I maybe would need to write then dynamic return type extension?
What about:
see below:
Nice, I had to change it a bit will leave it here if someone else needs it! Really nice :)
<?php
namespace Dibi;
/**
* @template T of object
*/
class Result
{
/**
* @template U of object
* @param class-string<U>|null $class
* @return self<U>
*/
public function setRowClass(?string $class): self;
/**
* @return T
*/
public function fetch();
/**
* @return self<T>
*/
public function fetchAll();
}
IMHO this could be included directly in this extension maybe?
The real-life situation is a bit more complicated, generics can't handle the null
argument well, and fetch
can also sometimes return an array 🤷 But it's fine if it works for you.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I'm allways using typed entities with dibi...
this will allways return
UserEntity|null
but currently phpstan is reporting this as:Method App\Repositories\UserRepository::getByEmail() should return App\Entities\UserEntity|null but returns
array|Dibi\Row|null.
Adding support for this would be cool?