cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: ResultsetInterface missing methods

Open noone-silent opened this issue 4 months ago • 1 comments

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

  • Phalcon version: 5.6.1
  • PHP Version: 8.1
  • Operating System: debian
  • Installation type: sury
  • Zephir version (if any): -
  • Server: nginx
  • Other related info (Database, table schema): mariadb

noone-silent avatar Feb 27 '24 01:02 noone-silent

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.

jturbide avatar Mar 23 '24 21:03 jturbide