PHPackages                             rkr/phpstan-types-from-sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rkr/phpstan-types-from-sql

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rkr/phpstan-types-from-sql
==========================

Generate phpstan shapes from sql tables

06PHP

Since Nov 1Pushed 6mo agoCompare

[ Source](https://github.com/rkrx/php-phpstan-sql-generator)[ Packagist](https://packagist.org/packages/rkr/phpstan-types-from-sql)[ RSS](/packages/rkr-phpstan-types-from-sql/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHPStan Types From SQL
======================

[](#phpstan-types-from-sql)

Generate PHPStan (and optionally Psalm) shape types directly from your database schema. Point the generator at a MySQL or PostgreSQL database via PDO and it produces a small PHP class whose docblock contains `@phpstan-type` (and also `@psalm-type`) definitions for every table.

Works great to keep static-analysis types in sync with your DB without hand-writing shapes.

Requirements
------------

[](#requirements)

- PHP 8.2+
- `ext-pdo`
- For analysis: PHPStan 1.10+ (optional) and/or Psalm (optional)

Install
-------

[](#install)

- `composer require --dev rkr/phpstan-types-from-sql`

If you want to autoload generated types under your own namespace (recommended), add an autoload-dev mapping in your project’s `composer.json`, e.g.:

- In `composer.json` → `autoload-dev.psr-4`: `{ "App\\\Types\\\": "generated/" }`
- Then run `composer dump-autoload`

Usage
-----

[](#usage)

### CLI Usage

[](#cli-usage)

- Composer exposes `vendor/bin/phpstan-sql-types` after install.
- Generate types directly from the command line without writing any PHP glue.

Parameters

- Database connection
    - `-d`, `--db-dsn ` Database DSN, e.g. `mysql:host=127.0.0.1;dbname=your_db;charset=utf8mb4` or `pgsql:host=127.0.0.1;port=5432;dbname=your_db`
    - `-u`, `--db-user ` Database user
    - `-p`, `--db-password ` Database password
    - `-s`, `--db-schema ` Schema name (Postgres required unless `current_schema()` fits; optional for MySQL)
- Analyzer generation
    - `-a`, `--array-shapes` Use `array{...}` shapes (default)
    - `-o`, `--object-shapes` Use `object{...}` shapes (MySQL); Postgres currently generates arrays
    - `--psalm` Include Psalm `@psalm-type` definitions
    - `--phpstan` Include PHPStan `@phpstan-type` definitions
    - If neither `--psalm` nor `--phpstan` is provided, both are generated
    - `-c`, `--class-name ` Generated class name (example: `DbTypes`)
    - `-n`, `--namespace ` Generated namespace of the class (example: `DbTypes`)
- Output
    - `--output ` Write to a file; if omitted, writes to stdout

Examples

MySQL → file (object shapes, both analyzers):

```
vendor/bin/phpstan-sql-types \
  -d "mysql:host=127.0.0.1;dbname=your_db;charset=utf8mb4" \
  -u your_user -p your_password \
  -c DbTypes -n DbTypes \
  --output generated/DbTypes.php

```

PostgreSQL → stdout (array shapes, both analyzers):

```
vendor/bin/phpstan-sql-types \
  -d "pgsql:host=127.0.0.1;port=5432;dbname=your_db" -s public \
  -u your_user -p your_password  \
  -c DbTypes -n DbTypes > generated/DbTypes.php

```

Notes

- Postgres currently emits array shapes; `--object-shapes` is ignored there.
- If only `--psalm` is requested for Postgres, the tool mirrors the PHPStan types as Psalm types for convenience.

### Code examples

[](#code-examples)

#### MySQL Example

[](#mysql-example)

- This example mirrors `test-mysql.php` but with placeholder credentials.

```
