Skip to content
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

Support for custom API's for type inference #635

Open
ArtemGoutsoul opened this issue Dec 9, 2023 · 1 comment
Open

Support for custom API's for type inference #635

ArtemGoutsoul opened this issue Dec 9, 2023 · 1 comment

Comments

@ArtemGoutsoul
Copy link

I use laminas-db (https://docs.laminas.dev/laminas-db/) wrapper, mostly with raw, dynamically assembled queries with different fetch modes, and it would be really cool if it was possible to infer result types for themn.

Here are some examples:

$db = MyDb::new($config); // MyDb is my wrapper adound laminas-db

// suppose table1 has columns column1, column2 (nullable), column3, column4

// should return for example
// @var $r list<array{column1: string, column2: string|null}>
$r = $db->fetchAll("
	SELECT column1, column2 FROM table1 WHERE column3 = :column3 AND column4 = :column4
	", ['column3' => 'some value 2','column4' => 'some value 2']);

// should return for example
// @var $r array<string, array{column1: string, column2: string|null, column3: string}>
$r = $db->fetchAssoc("
	SELECT column1, column2, column3 FROM table1 WHERE column3 = :column3 OR column4 = :column4
	", ['column3' => 'some value 2','column4' => 'some value 2']);


// should return for example
// @var $r array{column1: string, column2: string|null}
$r = $db->getRow("
	SELECT column1, column2 FROM table1 WHERE column3 = :column3 AND column4 = :column4
	", ['column3' => 'some value 2','column4' => 'some value 2']);

// should return for example
// @var $r array<string, string|null>
$r = $db->fetchPairs("
	SELECT column1, column2 FROM table1 WHERE column3 = :column3 AND column4 = :column4
	", ['column3' => 'some value 2','column4' => 'some value 2']);

These methods could be configured similarly to the query syntax analysis: https://github.com/staabm/phpstan-dba/blob/main/docs/rules.md

Ideally the return type would be an intersection with the existing method type inferred by phpstan. That type is correct but too wide.

@staabm
Copy link
Owner

staabm commented Dec 13, 2023

I think db apis have subtle differences and therefore re-using a single extension is most of the time not possible.

but you could built your own PHPStan return type extension for your DBLayer, based on the API phpstan-dba provides you.

similar to https://github.com/staabm/phpstan-dba/blob/main/src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants