Giter Site home page Giter Site logo

Comments (4)

staabm avatar staabm commented on June 2, 2024

@hemberger could you look into that one?

from phpstan-dba.

hemberger avatar hemberger commented on June 2, 2024

Sure, I'll look into this today.

from phpstan-dba.

hemberger avatar hemberger commented on June 2, 2024

Here's my test setup:

CREATE TABLE testing (
    id INT PRIMARY KEY AUTO_INCREMENT,
    my_data JSON NOT NULL,
    my_bool BOOLEAN NOT NULL
);
$db->insert(
    'testing',
    [   
        'my_bool' => true,
        'my_data' => [1, 2, 3], 
    ],  
    [   
        'my_bool' => Types::BOOLEAN,
        'my_data' => Types::JSON,
    ],  
);

And my results:

Query error: Column "testing.my_bool" expects value type int<-128, 127>, got type true
Query error: Column "testing.my_data" expects value type string, got type array{1, 2, 3}

Some things to note:

  1. In my query error above, the BOOLEAN column is properly identified as int<-128, 127>, because this pseudo-type is an alias for TINYINT(1) (at least in MySQL). As such, I think it would be prudent to use the bool PHP type for MySQL columns that are identically TINYINT(1). See, e.g. https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-19.html#mysqld-8-0-19-deprecation-removal.

    MySQL Connectors make the assumption that TINYINT(1) columns originated as BOOLEAN columns

  2. Even if I use TINYINT(1) UNSIGNED instead of BOOLEAN, I still cannot reproduce the error as you have described it:

    Query error: Column "testing.my_bool" expects value type int, got type true

    Maybe you are using an enum instead of TINYINT(1) UNSIGNED? More details about your setup would be helpful here.

  3. DoctrineKeyValueStyleRule is not yet advanced enough to account for the many Doctrine\DBAL\Type classes that may be used to convert an input value to its corresponding database type (e.g. PHP array to JSON string, as in this issue). I'm willing to help implement these, but I'm not sure how to best do this. It seems like we'd need to maintain a mapping between input and output types for each Doctrine\DBAL\Type class.

    We might be able to use reflection on the Type class itself to get the output type (e.g. JsonType::convertToDatabaseValue), but the Type classes, by design, accept any input type. Some input types will cause an exception to be thrown, but others will result in a dangerous type coercion (e.g. casting a non-numeric string to int). This is the reason that this rule does type checks in the first place -- to provide the validation that Doctrine\DBAL does not.

@staabm Assuming you eventually want more complex type converters to be supported, do you have any opinions about how to proceed with this?

from phpstan-dba.

staabm avatar staabm commented on June 2, 2024

Doctrine types have been requested before #278

I think we could just have a separate class which maps the doctrine type constant-types to phpstan types.

Just a guess: Maybe we could get some inspiration from phpstan-doctrine

doctrine relfection could utilize it. I don't think we should plug it directly into SchemaReflection or QueryReflection

We could also add a rule which validates that the used doctrine type is compatible with the underlying schema column type - if this sounds useful for doctrine users.

from phpstan-dba.

Related Issues (20)

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.