phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.97k forks source link

[BUG]: ResultsetInterface missing methods #16534

Open noone-silent opened 8 months ago

noone-silent commented 8 months ago

Describe the bug The ResultInterface is missing methods that are available to all result sets. Simple and Complex both only extend Resultset. Resultset is the only Class that implements ResultsetInterface. Therefor all methods from Resultset should also be in ResultsetInterface. For example currently the count() method is missing. So to make static code analyzer happy, we need to add a phpdoc overwrite of the type, or use count($model->toArray()).

To Reproduce Read code

Expected behavior In general all Interfaces should be checked for missing methods.

Screenshots

Details

jturbide commented 7 months ago

I strongly agree for this one. I was about to open another issue but it seems to be exactly my problem as well. Static code analyzers such as Psalm, PHPStan will report false-positive error when iterating results from "Model::find()" requests.

Argument of an invalid type Phalcon\Mvc\Model\ResultsetInterface supplied for foreach, only iterables are supported.

\Phalcon\Mvc\Model\ResultsetInterface should extend \Iterator, \SeekableIterator, \Countable, \ArrayAccess, \Serializable, \JsonSerializable directly from the interface definition instead of implementing those in the "Resultset" class.

Using the current Phalcon version 5.6.2, just to make the static code analyzers happy, we have to do unnecessary ignores or assertations like this:

$users = User::find();
assert($users instanceof \Iterator);
foreach ($users as $user) {
    // ...
}

If it's not possible to alter the \Phalcon\Mvc\Model\ResultsetInterface, then I would opt for another interface \Phalcon\Mvc\Model\IterableResultsetInterface which would extend \Phalcon\Mvc\Model\ResultsetInterface, \Iterator, \SeekableIterator, \Countable, \ArrayAccess, \Serializable, \JsonSerializable.