{"id":13593696,"url":"https://github.com/staabm/phpstan-dba","last_synced_at":"2025-05-14T18:06:47.866Z","repository":{"id":37013064,"uuid":"441973403","full_name":"staabm/phpstan-dba","owner":"staabm","description":"PHPStan based SQL static analysis and type inference for the database access layer","archived":false,"fork":false,"pushed_at":"2025-05-06T08:11:21.000Z","size":1542,"stargazers_count":271,"open_issues_count":84,"forks_count":23,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-06T09:24:21.293Z","etag":null,"topics":["hacktoberfest","php","phpstan","phpstan-extension","query-validation","static-analysis","type-inference"],"latest_commit_sha":null,"homepage":"https://staabm.github.io/archive.html#phpstan-dba","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/staabm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["staabm"]}},"created_at":"2021-12-26T20:00:09.000Z","updated_at":"2025-05-06T08:11:24.000Z","dependencies_parsed_at":"2023-02-10T17:15:55.455Z","dependency_job_id":"18de771c-a08e-4113-9999-0ffb63e59fab","html_url":"https://github.com/staabm/phpstan-dba","commit_stats":{"total_commits":580,"total_committers":19,"mean_commits":"30.526315789473685","dds":"0.24655172413793103","last_synced_commit":"2d127626f94d16a07f2b85982d4a28ca80f2e2cd"},"previous_names":[],"tags_count":107,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/staabm%2Fphpstan-dba","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/staabm%2Fphpstan-dba/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/staabm%2Fphpstan-dba/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/staabm%2Fphpstan-dba/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/staabm","download_url":"https://codeload.github.com/staabm/phpstan-dba/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254198514,"owners_count":22030965,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","php","phpstan","phpstan-extension","query-validation","static-analysis","type-inference"],"created_at":"2024-08-01T16:01:23.249Z","updated_at":"2025-05-14T18:06:42.858Z","avatar_url":"https://github.com/staabm.png","language":"PHP","funding_links":["https://github.com/sponsors/staabm"],"categories":["PHP"],"sub_categories":[],"readme":"# `phpstan-dba`: PHPStan based SQL static analysis and type inference for the database access layer\n\n`phpstan-dba` makes your phpstan static code analysis jobs aware of datatypes within your database.\nWith this information at hand we are able to detect type inconsistencies between your domain model and database-schema.\nAdditionally errors in code handling the results of sql queries can be detected.\n\nThis 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):\n\n* [result set type-inference](https://staabm.github.io/2022/06/19/phpstan-dba-type-inference.html)\n* [detect errors in sql queries](https://staabm.github.io/2022/08/05/phpstan-dba-syntax-error-detection.html)\n* [detect placeholder/bound value mismatches](https://staabm.github.io/2022/07/30/phpstan-dba-placeholder-validation.html)\n* [query plan analysis](https://staabm.github.io/2022/08/16/phpstan-dba-query-plan-analysis.html) to detect performance issues\n* builtin support for `doctrine/dbal`, `mysqli`, and `PDO`\n* API to configure the same features for your custom sql based database access layer\n* Opt-In analysis of write queries (since version 0.2.55+)\n\nIn case you are using Doctrine ORM, you might use `phpstan-dba` in tandem with [phpstan-doctrine](https://github.com/phpstan/phpstan-doctrine).\n\n\u003e [!NOTE]\n\u003e At the moment only MySQL/MariaDB and PGSQL databases are supported. Technically it's not a big problem to support other databases though.\n\n## Talks\n\n[phpstan-dba - check your sql queries like a boss](https://staabm.github.io/talks/phpstan-dba@phpugffm/)\nMay 2023, at PHP Usergroup in Frankfurt Main (Germany).\n\n## DEMO\n\nsee the ['Files Changed' tab of the DEMO-PR](https://github.com/staabm/phpstan-dba/pull/61/files#diff-98a3c43049f6a0c859c0303037d9773534396533d7890bad187d465d390d634e) for a quick glance.\n\n## 💌 Support phpstan-dba\n\n[Consider supporting the project](https://github.com/sponsors/staabm), so we can make this tool even better even faster for everyone.\n\n## Installation\n\n**First**, use composer to install:\n\n```shell\ncomposer require --dev staabm/phpstan-dba\n```\n\n**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):\n\n```php\n\u003c?php // phpstan-dba-bootstrap.php\n\nuse staabm\\PHPStanDba\\DbSchema\\SchemaHasherMysql;\nuse staabm\\PHPStanDba\\QueryReflection\\RuntimeConfiguration;\nuse staabm\\PHPStanDba\\QueryReflection\\MysqliQueryReflector;\nuse staabm\\PHPStanDba\\QueryReflection\\QueryReflection;\nuse staabm\\PHPStanDba\\QueryReflection\\ReplayAndRecordingQueryReflector;\nuse staabm\\PHPStanDba\\QueryReflection\\ReplayQueryReflector;\nuse staabm\\PHPStanDba\\QueryReflection\\ReflectionCache;\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$config = new RuntimeConfiguration();\n// $config-\u003edebugMode(true);\n// $config-\u003estringifyTypes(true);\n// $config-\u003eanalyzeQueryPlans(true);\n// $config-\u003eutilizeSqlAst(true);\n\n// TODO: Put your database credentials here\n$mysqli = new mysqli('hostname', 'username', 'password', 'database');\n// Alternatively you can use PdoMysqlQueryReflector, PdoPgSqlQueryReflector instead\n$reflector = new MysqliQueryReflector($mysqli);\n\n/*\n$cacheFile = __DIR__.'/.phpstan-dba.cache';\n$reflector = new ReplayAndRecordingQueryReflector(\n    ReflectionCache::create(\n        $cacheFile\n    ),\n    new SchemaHasherMysql($mysqli)\n    $reflector\n);\n*/\n\nQueryReflection::setupReflector(\n    $reflector,\n    $config\n);\n```\n\n\u003e [!NOTE]\n\u003e [Configuration for PGSQL](https://github.com/staabm/phpstan-dba/blob/main/docs/pgsql.md) is pretty similar\n\n**Third**, create or update your `phpstan.neon` file so [bootstrapFiles](https://phpstan.org/config-reference#bootstrap) includes `phpstan-dba-bootstrap.php`.\n\nIf you are **not** using [phpstan/extension-installer](https://github.com/phpstan/extension-installer), you will also need to include `dba.neon`.\n\nYour `phpstan.neon` might look something like:\n\n```neon\nparameters:\n  level: 8\n  paths:\n    - src/\n  bootstrapFiles:\n    - phpstan-dba-bootstrap.php\n\nincludes:\n  - ./vendor/staabm/phpstan-dba/config/dba.neon\n```\n\n**Finally**, run `phpstan`, e.g.\n\n```shell\n./vendor/bin/phpstan analyse -c phpstan.neon\n```\n\n## Read more\n\n- [Runtime configuration](https://github.com/staabm/phpstan-dba/blob/main/docs/configuration.md)\n- [Record and Replay](https://github.com/staabm/phpstan-dba/blob/main/docs/record-and-replay.md)\n- [Custom Query API Support](https://github.com/staabm/phpstan-dba/blob/main/docs/rules.md)\n- [MySQL Support](https://github.com/staabm/phpstan-dba/blob/main/docs/mysql.md)\n- [PGSQL Support](https://github.com/staabm/phpstan-dba/blob/main/docs/pgsql.md)\n- [Reflector Overview](https://github.com/staabm/phpstan-dba/blob/main/docs/reflectors.md)\n- [How to analyze a PHP 7.x codebase?](https://github.com/staabm/phpstan-dba/blob/main/docs/faq.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstaabm%2Fphpstan-dba","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstaabm%2Fphpstan-dba","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstaabm%2Fphpstan-dba/lists"}