staabm/phpstan-dba
Composer 安装命令:
composer require --dev staabm/phpstan-dba
包简介
README 文档
README
phpstan-dba makes your phpstan static code analysis jobs aware of datatypes within your database.
With this information at hand we are able to detect type inconsistencies between your domain model and database-schema.
Additionally errors in code handling the results of sql queries can be detected.
This extension provides the following features, as long as you stick to the rules:
- result set type-inference
- detect errors in sql queries
- detect placeholder/bound value mismatches
- query plan analysis to detect performance issues
- builtin support for
doctrine/dbal,mysqli, andPDO - API to configure the same features for your custom sql based database access layer
- Opt-In analysis of write queries (since version 0.2.55+)
In case you are using Doctrine ORM, you might use phpstan-dba in tandem with phpstan-doctrine.
Note
At the moment only MySQL/MariaDB and PGSQL databases are supported. Technically it's not a big problem to support other databases though.
Talks
phpstan-dba - check your sql queries like a boss May 2023, at PHP Usergroup in Frankfurt Main (Germany).
DEMO
see the 'Files Changed' tab of the DEMO-PR for a quick glance.
💌 Support phpstan-dba
Consider supporting the project, so we can make this tool even better even faster for everyone.
Installation
First, use composer to install:
composer require --dev staabm/phpstan-dba
Second, create a phpstan-dba-bootstrap.php file, which allows to you to configure phpstan-dba (this optionally includes database connection details, to introspect the database; if you would rather not do this see Record and Replay:
<?php // phpstan-dba-bootstrap.php use staabm\PHPStanDba\DbSchema\SchemaHasherMysql; use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration; use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector; use staabm\PHPStanDba\QueryReflection\QueryReflection; use staabm\PHPStanDba\QueryReflection\ReplayAndRecordingQueryReflector; use staabm\PHPStanDba\QueryReflection\ReplayQueryReflector; use staabm\PHPStanDba\QueryReflection\ReflectionCache; require_once __DIR__ . '/vendor/autoload.php'; $config = new RuntimeConfiguration(); // $config->debugMode(true); // $config->stringifyTypes(true); // $config->analyzeQueryPlans(true); // $config->utilizeSqlAst(true); // TODO: Put your database credentials here $mysqli = new mysqli('hostname', 'username', 'password', 'database'); // Alternatively you can use PdoMysqlQueryReflector, PdoPgSqlQueryReflector instead $reflector = new MysqliQueryReflector($mysqli); /* $cacheFile = __DIR__.'/.phpstan-dba.cache'; $reflector = new ReplayAndRecordingQueryReflector( ReflectionCache::create( $cacheFile ), new SchemaHasherMysql($mysqli) $reflector ); */ QueryReflection::setupReflector( $reflector, $config );
Note
Configuration for PGSQL is pretty similar
Third, create or update your phpstan.neon file so bootstrapFiles includes phpstan-dba-bootstrap.php.
If you are not using phpstan/extension-installer, you will also need to include dba.neon.
Your phpstan.neon might look something like:
parameters: level: 8 paths: - src/ bootstrapFiles: - phpstan-dba-bootstrap.php includes: - ./vendor/staabm/phpstan-dba/config/dba.neon
Finally, run phpstan, e.g.
./vendor/bin/phpstan analyse -c phpstan.neon
Read more
- Runtime configuration
- Record and Replay
- Custom Query API Support
- MySQL Support
- PGSQL Support
- Reflector Overview
- How to analyze a PHP 7.x codebase?
Contribute
Setup the test-suite
- copy
.env.distinto a new file.envand adjust the credentials in it for your local needs - import the database
schema.sql(MySQL, MariaDB) orpgsql-schema.sql(Postgres)
staabm/phpstan-dba 适用场景与选型建议
staabm/phpstan-dba 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.58M 次下载、GitHub Stars 达 294, 最近一次更新时间为 2021 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 「dev」 「code quality」 「PHPStan」 「Database Access Layer」 「phpstan-extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 staabm/phpstan-dba 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 staabm/phpstan-dba 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 staabm/phpstan-dba 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Monolog bridge for PHP Debugbar
Simple and fast methods to read private properties and call private methods
Sentiment analysis library for PHP.
Analysis module for finding problematical shop data.
统计信息
- 总下载量: 2.58M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 294
- 点击次数: 26
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-30