forrest79 / phpgsql-phpstan
PhPgSql 类反射扩展和 PHPStan 设置。
Requires
- php: ^8.0
- phpstan/phpstan: ^1.10
Requires (Dev)
- dg/bypass-finals: ^1.4
- forrest79/phpcs: ^1.2
- forrest79/phpcs-ignores: ^0.3
- forrest79/phpgsql: ^1.0
- phpstan/phpstan-strict-rules: ^1.5
Suggests
- dg/bypass-finals: Needed for is_dbrow() when used with withProperties and custom row is marked as final
- nette/tester: Can be used instead of db/bypass-finals
README
简介
此扩展定义了动态方法和 PHPStan 设置,用于 Forrest79\PhPgSql
。
安装
要使用此扩展,请在 Composer 中引入它
composer require --dev forrest79/phpgsql-phpstan
使用
将 extension.neon
包含在项目 PHPStan 配置中
includes: - vendor/forrest79/phpgsql-phpstan/extension.neon
如果您正在使用自己的 Forrest79\PhPgSql\Db\Row
或 Forrest79\PhPgSql\Fluen\Query
,可以设置如下:
parameters: forrest79: phpgsql: dbRowClass: MyOwn\PhPgSql\Db\RowXyz fluentQueryClass: MyOwn\PhPgSql\Fluent\QueryXyz
或者您也可以只设置一个扩展:
- 对于
PhPgSql\Db\Result
(从获取方法中获取正确的Row
对象 - 很遗憾,目前必须在您的代码中对Row
迭代进行类型检查)
services: Forrest79PhPgSqlPHPStanReflectionDbResultDynamicMethodReturnTypeExtension: arguments: dbRowClass: MyOwn\PhPgSql\Db\RowXyz
- 对于
PhPgSql\Fluent\QueryExecute
(从获取方法中获取正确的Row
对象 - 很遗憾,目前必须在您的代码中对Row
迭代进行类型检查)
services: Forrest79PhPgSqlPHPStanReflectionFluentQueryExecuteDynamicMethodReturnTypeExtension: arguments: dbRowClass: MyOwn\PhPgSql\Db\RowXyz
- 对于
PhPgSql\Fluent\Complex
(在query()
方法中返回正确的Query
)
services: Forrest79PhPgSqlPHPStanReflectionFluentComplexDynamicMethodReturnTypeExtension: arguments: fluentQueryClass: MyOwn\PhPgSql\Fluent\QueryXyz
您还可以使用简单的 Row
类型缩小函数 is_dbrow($row[, $expectedProperties])
。它返回 bool
,但建议您始终仅与 assert()
函数一起使用此函数。此软件包可能缺少在您的生产供应商中,因此 is_dbrow
函数也可能缺少,PHP 脚本将在此处崩溃。如果正确设置生产中的 assert()
函数,则调用 is_dbrow()
将被省略,并且您的生产代码将是正确的。
在 PHP 中,此函数仅检查 $row
是否是 Forrest79\PhPgSql\Db\Row
的实例,并且如果您指定了 $expectedProperties
(键是列名,值是字符串形式的 PHP 类型)它将检查,行是否恰好定义了预期的列(在 PHP 中没有类型检查,仅用于 PHPStan)。
真正的魔法隐藏在 PHPStan 扩展中。此函数将缩小 $row
类型为 Forrest79\PhPgSql\Db\Row
或您的自定义行对象。如果您还指定了属性,这将为 PHPStan 正确类型化 - 原始所有属性都是 mixed
。
foreach ($someQuery as $row) { // here is $row as Forrest79\PhPgSql\Db\Row for PHPStan assert(is_dbrow($row)); // here is as your custom row object } $row = $someQuery->select(['columnInt', 'columnString', 'columnFloat', 'columnDatetime'])->...->fetch(); $row->columnInt; // mixed for PHPStan $row->columnString; // mixed for PHPStan $row->columnFloat; // mixed for PHPStan $row->columnDatetime; // mixed for PHPStan assert(is_dbrow($row, ['columnInt' => '?int', 'columnString' => 'string', 'columnFloat' => 'float', 'columnDatetime' => \DateTime::class])); $row->columnInt; // int or NULL for PHPStan $row->columnString; // string for PHPStan $row->columnFloat; // float for PHPStan $row->columnDatetime; // \DateTime for PHPStan
要仅设置此扩展,请使用:
services: Forrest79PhPgSqlPHPStanAnalyserIsDbRowFunctionTypeSpecifyingExtension: arguments: dbRowClass: MyOwn\PhPgSql\Db\RowXyz
或者您可以使用简单的 DbRow
注解。
foreach ($someQuery as $row) { // here is $row as Forrest79\PhPgSql\Db\Row for PHPStan /** @var DbRow $row */ // here is as your custom row object }
在需要时使用 DbRow
伪类型,例如参数、返回值、变量等。
要仅设置此扩展,请使用:
services: Forrest79PhPgSqlPHPStanPhpDocDbRowTypeNodeResolverExtension: arguments: dbRowClass: MyOwn\PhPgSql\Db\RowXyz