PHPStan based SQL static analysis and type inference for the database access layer
See the codephpstan-dba: PHPStan based SQL static analysis and type inference for the database access layerphpstan-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:
doctrine/dbal, mysqli, and PDOIn 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.
phpstan-dba - check your sql queries like a boss May 2023, at PHP Usergroup in Frankfurt Main (Germany).
see the 'Files Changed' tab of the DEMO-PR for a quick glance.
Consider supporting the project, so we can make this tool even better even faster for everyone.
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;
/** @var PHPStan\DependencyInjection\Container $container */
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 = $container->getParameter('resultCachePath') . '.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
Setup the test-suite
.env.dist into a new file .env and adjust the credentials in it for your local needsschema.sql (MySQL, MariaDB) or pgsql-schema.sql (Postgres)247 followers · starred Jul 2023
260 followers · starred Oct 2024
214 followers · starred May 2022
128 followers · starred Apr 2023
PHPStan based SQL static analysis and type inference for the database access layer
See the codephpstan-dba: PHPStan based SQL static analysis and type inference for the database access layerphpstan-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:
doctrine/dbal, mysqli, and PDOIn 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.
phpstan-dba - check your sql queries like a boss May 2023, at PHP Usergroup in Frankfurt Main (Germany).
see the 'Files Changed' tab of the DEMO-PR for a quick glance.
Consider supporting the project, so we can make this tool even better even faster for everyone.
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;
/** @var PHPStan\DependencyInjection\Container $container */
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 = $container->getParameter('resultCachePath') . '.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
Setup the test-suite
.env.dist into a new file .env and adjust the credentials in it for your local needsschema.sql (MySQL, MariaDB) or pgsql-schema.sql (Postgres)247 followers · starred Jul 2023
260 followers · starred Oct 2024
214 followers · starred May 2022
128 followers · starred Apr 2023