-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dibi - return type extension #474
Comments
Do the dibi methods really return a array or does it always return a Row object which implement ArrayAccess and therefore can be accessed like a regular array? In other words: maybe we just need to make it return a generic Row object - and we don't need a config flag? |
Its setupable via /**
* Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position
* @return Row|array|null
*/
final public function fetch()
{
$row = $this->getResultDriver()->fetch(true);
if ($row === null) {
return null;
}
$this->fetched = true;
$this->normalize($row);
if ($this->rowFactory) {
return ($this->rowFactory)($row);
} elseif ($this->rowClass) {
return new $this->rowClass($row);
}
return $row;
} |
Seems like you already tried to discuss this with ondrejmirtes here phpstan/phpstan#2923 a it is not yet implemented |
the mentioned issue is related to how stdClass works. for concrete classes we don't have this problem. see e.g. phpstan-dba/tests/default/data/pdo.php Line 18 in dc579c0
PDOStatement .
AFAIR we need a generic stub for your object in https://github.com/staabm/phpstan-dba/blob/main/config/stubFiles.neon if we make the return type extension return a Union of |
i tried copying the stub from <?php
namespace Dibi;
/**
* @template rowType
*
* @implements Traversable<int, rowType>
* @implements IteratorAggregate<int, rowType>
*
*/
class Row implements Traversable, IteratorAggregate
{
} $app = $this->database->fetch('SELECT apps_id from apps where apps_id = %i', $appsId);
echo $app->test;
echo $app['test'];
echo $app->apps_id;
echo $app['apps_id']; there should be reported twice that test does not exists on Dibi\Row. Can you please help me with the stub? |
please open a PR with what you have. otherwise its just guesswork on my end |
Just to chime in with more info. Dibi returns However, the return behavior is configurable. There are 2 places:
How is this possible?
So when using just |
Hello @staabm
I created the dibi return extension which returns
array{...}
for fetch andarray<arary{...}>
forfetchAll()
.However, by default, dibi returns not array but
\Dibi\Row
(https://github.com/dg/dibi/blob/master/src/Dibi/Row.php#L22) which is just a crate-like class wit magic properties.I was thinking that in order to allow the dibi static analysis to be usefull for wider range of programmers, both should be supported.
Perhaps new constat
DibiConnectionFetchDynamicReturnTypeExtension::RETURN_TYPE = '\Dibi\Row'
could be added (which coudl be overwritten inphpstan-dba-bootstra.php
toDibiConnectionFetchDynamicReturnTypeExtension::RETURN_TYPE = 'array'
) and return the return type based on this variable.I tried it via
however that didnt have desired results
as the above code didnt throw phpstan exception that
unknown
isnt property of Row class.I will be happy to create pull request but I am lost for now as I couldnt force phpstan to return correct
Type
.Could you please guide me?
The text was updated successfully, but these errors were encountered: