staabm / phpstan-dba
Fund package maintenance!
staabm
Installs: 1 493 848
Dependents: 3
Suggesters: 0
Security: 0
Stars: 263
Watchers: 6
Forks: 18
Open Issues: 82
Type:phpstan-extension
Requires
- php: ^7.4 || ^8.0
- composer-runtime-api: ^2.0
- composer/semver: ^3.2
- doctrine/dbal: 3.*
- phpstan/phpstan: ^2.0
Requires (Dev)
- ext-mysqli: *
- ext-pdo: *
- dibi/dibi: ^4.2
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^8.5|^9.5
- symplify/easy-coding-standard: ^12.3
- vlucas/phpdotenv: ^5.4
Conflicts
- sqlftw/sqlftw: <0.1.16
- 457.x-dev
- 1.x-dev
- dev-main / 0.3.x-dev
- 0.3.1
- 0.3
- 0.2.x-dev
- 0.2.85
- 0.2.84
- 0.2.83
- 0.2.82
- 0.2.81
- 0.2.80
- 0.2.79
- 0.2.78
- 0.2.77
- 0.2.76
- 0.2.75
- 0.2.74
- 0.2.73
- 0.2.72
- 0.2.71
- 0.2.70
- 0.2.69
- 0.2.68
- 0.2.67
- 0.2.66
- 0.2.65
- 0.2.64
- 0.2.63
- 0.2.62
- 0.2.61
- 0.2.60
- 0.2.59
- 0.2.58
- 0.2.57
- 0.2.56
- 0.2.55
- 0.2.54
- 0.2.53
- 0.2.52
- 0.2.51
- 0.2.50
- 0.2.49
- 0.2.48
- 0.2.47
- 0.2.46
- 0.2.45
- 0.2.44
- 0.2.43
- 0.2.42
- 0.2.41
- 0.2.40
- 0.2.39
- 0.2.38
- 0.2.37
- 0.2.36
- 0.2.35
- 0.2.34
- 0.2.33
- 0.2.32
- 0.2.31
- 0.2.30
- 0.2.29
- 0.2.28
- 0.2.27
- 0.2.26
- 0.2.25
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
- dev-dependabot/composer/symplify/easy-coding-standard-tw-12.5
- dev-reen
- dev-list
- dev-dependabot/composer/doctrine/dbal-4.star
- dev-less-previous
- dev-less-parents
- dev-dependabot/composer/dibi/dibi-tw-5.0
- dev-re-enable
- dev-debug-hasher
- dev-close-cursor
- dev-hotifx
- dev-bug394
- dev-uknown-constants
- dev-mysqli
- dev-bug385
- dev-demo
- dev-test-obj
- dev-bug276
- dev-staabm-patch-2
- dev-debug
- dev-even-more-values
- dev-staabm-patch-1
- dev-param-types
This package is auto-updated.
Last update: 2024-12-12 21:03:21 UTC
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'; $cacheFile = __DIR__.'/.phpstan-dba.cache'; $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'); QueryReflection::setupReflector( new ReplayAndRecordingQueryReflector( ReflectionCache::create( $cacheFile ), // XXX alternatively you can use PdoMysqlQueryReflector instead new MysqliQueryReflector($mysqli), new SchemaHasherMysql($mysqli) ), $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