Git Product home page Git Product logo

psalm-pdo-mysql-plugin's Introduction

This plugin let Psalm better understand MySQL SELECT's, executed via PDO.

Some assumptions

Plugin is developed initially for internal purpose, so there is some limitation. It assumes that PDO is configured as:

  • PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION, so query can't return false
  • PDO::ATTR_EMULATE_PREPARES is set to true

It can be configured later, also pull requests are always welcome :)

Usage example

Database:

CREATE TABLE `users` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255),
    PRIMARY KEY (`id`)
);

Code:

<?php

/** @return array{id: string, username: string, email: ?string} */
function fetchUser(PDO $pdo): array {
    return $pdo->query("SELECT * FROM users WHERE id=1")->fetch(PDO::FETCH_ASSOC);
} 

Configuration

You should generate database description, otherwise plugin will not register.

Use script vendor/bin/dump-databases to generate it:

vendor/bin/dump-databases mysql:host=localhost user mysecretpassword -- dbname > databases.xml

It executes following query:

SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = :schema
ORDER BY TABLE_NAME

Then add plugin configuration in psalm.xml as follows:

<psalm
        maxStringLength="1000" 
        xmlns:xi="http://www.w3.org/2001/XInclude">
<!--
    maxStringLength is important for analyzing long queries
    and XInclude is useful for including database declaration
    from other file
 -->
    <plugins>
        <pluginClass class="M03r\PsalmPDOMySQL\Plugin">
            <xi:include href="databases.xml" />
            <!-- if you use PDO-like wrapper/interface -->
            <PDOClass>App\Bundle\PDOInterface</PDOClass>
        </pluginClass>
        <!-- other plugins -->
    </plugins>
</psalm>

Please do not forget update databases.xml when modifying database schema.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.