• Stars
    star
    250
  • Rank 162,397 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHPStan based SQL static analysis and type inference for the database access layer

phpstandba: 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:

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

Read more

More Repositories

1

annotate-pull-request-from-checkstyle

cs2pr - Annotate a GitHub Pull Request based on a Checkstyle XML-report within your GitHub Action
PHP
188
star
2

phpstan-todo-by

Todo comments with expiration
PHP
173
star
3

phpstan-baseline-analysis

Analyzes PHPStan baseline files and creates aggregated error trend-reports
PHP
73
star
4

staabm.github.io

Personal blog, about my open source activies.
HTML
7
star
5

soap-client-generator

7
star
6

oss-contribs

simple contributions statistics generator, find pull requests, referenced issues and reactions
PHP
6
star
7

thincache

Thin abstraction layer for often used cache backends.
PHP
4
star
8

redaxo-addon-installer

Composer installer package for redaxo5 cms addons
PHP
4
star
9

SchemaValidationBehavior

A Propel behavior which creates validation rules based on the database schema
PHP
4
star
10

phpstan-deployer6

phpstan stubs for Deployer version 6
4
star
11

SysMonitor

Monitors a php app and send notifications on certain error/exception/resource-exhausting/custom events
PHP
4
star
12

gh-annotation-example

example repo to show github annotations in pull requests
3
star
13

annotate-pull-request-from-checkstyle-action

cs2pr GitHub Action
Shell
3
star
14

staabm

3
star
15

http-cache-control

http cache-control tools, to ease http handling for common caching use-cases
PHP
3
star
16

secure_dotenv

A secure .env handler with encrypted key/value storage
PHP
3
star
17

rector-view-scope

PHP
2
star
18

zf-select-strip

PHP
2
star
19

AnyWhereInDB

Needle in a Haystack: Comprehensive Search on MYSQL Database
PHP
2
star
20

cronschedule

App which transforms a cronexpression into a schedule table
PHP
1
star
21

gh-pr-payload

1
star
22

phpstan-repro813

1
star
23

complex-library-installer

Composer installer package for Complex proprietary rocket framework
PHP
1
star
24

phpunit-cross-os

contains a few tools usefull for making your test-expectations agnostic to operating system specifics
PHP
1
star
25

rector-repro1

PHP
1
star
26

hungry-spl-file-info

PHP
1
star