https://github.com/staabm/phpstan-dba
PHPStan based SQL static analysis and type inference for the database access layer
https://github.com/staabm/phpstan-dba
hacktoberfest php phpstan phpstan-extension query-validation static-analysis type-inference
Last synced: 2 months ago
JSON representation
PHPStan based SQL static analysis and type inference for the database access layer
- Host: GitHub
- URL: https://github.com/staabm/phpstan-dba
- Owner: staabm
- License: mit
- Created: 2021-12-26T20:00:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-06T08:11:21.000Z (2 months ago)
- Last Synced: 2025-05-06T09:24:21.293Z (2 months ago)
- Topics: hacktoberfest, php, phpstan, phpstan-extension, query-validation, static-analysis, type-inference
- Language: PHP
- Homepage: https://staabm.github.io/archive.html#phpstan-dba
- Size: 1.47 MB
- Stars: 271
- Watchers: 5
- Forks: 23
- Open Issues: 84
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# `phpstan-dba`: PHPStan based SQL static analysis and type inference for the database access layer
`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](https://staabm.github.io/2022/07/23/phpstan-dba-inference-placeholder.html#the-golden-phpstan-dba-rules):
* [result set type-inference](https://staabm.github.io/2022/06/19/phpstan-dba-type-inference.html)
* [detect errors in sql queries](https://staabm.github.io/2022/08/05/phpstan-dba-syntax-error-detection.html)
* [detect placeholder/bound value mismatches](https://staabm.github.io/2022/07/30/phpstan-dba-placeholder-validation.html)
* [query plan analysis](https://staabm.github.io/2022/08/16/phpstan-dba-query-plan-analysis.html) to detect performance issues
* builtin support for `doctrine/dbal`, `mysqli`, and `PDO`
* 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](https://github.com/phpstan/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](https://staabm.github.io/talks/phpstan-dba@phpugffm/)
May 2023, at PHP Usergroup in Frankfurt Main (Germany).## DEMO
see the ['Files Changed' tab of the DEMO-PR](https://github.com/staabm/phpstan-dba/pull/61/files#diff-98a3c43049f6a0c859c0303037d9773534396533d7890bad187d465d390d634e) for a quick glance.
## 💌 Support phpstan-dba
[Consider supporting the project](https://github.com/sponsors/staabm), so we can make this tool even better even faster for everyone.
## Installation
**First**, use composer to install:
```shell
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](https://github.com/staabm/phpstan-dba/blob/main/docs/record-and-replay.md):
```php
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](https://github.com/staabm/phpstan-dba/blob/main/docs/pgsql.md) is pretty similar**Third**, create or update your `phpstan.neon` file so [bootstrapFiles](https://phpstan.org/config-reference#bootstrap) includes `phpstan-dba-bootstrap.php`.
If you are **not** using [phpstan/extension-installer](https://github.com/phpstan/extension-installer), you will also need to include `dba.neon`.
Your `phpstan.neon` might look something like:
```neon
parameters:
level: 8
paths:
- src/
bootstrapFiles:
- phpstan-dba-bootstrap.phpincludes:
- ./vendor/staabm/phpstan-dba/config/dba.neon
```**Finally**, run `phpstan`, e.g.
```shell
./vendor/bin/phpstan analyse -c phpstan.neon
```## Read more
- [Runtime configuration](https://github.com/staabm/phpstan-dba/blob/main/docs/configuration.md)
- [Record and Replay](https://github.com/staabm/phpstan-dba/blob/main/docs/record-and-replay.md)
- [Custom Query API Support](https://github.com/staabm/phpstan-dba/blob/main/docs/rules.md)
- [MySQL Support](https://github.com/staabm/phpstan-dba/blob/main/docs/mysql.md)
- [PGSQL Support](https://github.com/staabm/phpstan-dba/blob/main/docs/pgsql.md)
- [Reflector Overview](https://github.com/staabm/phpstan-dba/blob/main/docs/reflectors.md)
- [How to analyze a PHP 7.x codebase?](https://github.com/staabm/phpstan-dba/blob/main/docs/faq.md)