phpstan / phpstan-dibi

Dibi class reflection extension for PHPStan
MIT License
15 stars 9 forks source link

setRowClass support #7

Closed dakorpar closed 4 years ago

dakorpar commented 4 years ago

I'm allways using typed entities with dibi...

image

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?

ondrejmirtes commented 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).

dakorpar commented 4 years ago

excellent, that was fast, thanks!

dakorpar commented 4 years ago

@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?

ondrejmirtes commented 4 years ago

What about:

see below:
dakorpar commented 4 years ago

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?

ondrejmirtes commented 4 years ago

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.

github-actions[bot] commented 3 years ago

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.