Comments (4)
@hemberger could you look into that one?
from phpstan-dba.
Sure, I'll look into this today.
from phpstan-dba.
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:
-
In my query error above, the
BOOLEAN
column is properly identified asint<-128, 127>
, because this pseudo-type is an alias forTINYINT(1)
(at least in MySQL). As such, I think it would be prudent to use thebool
PHP type for MySQL columns that are identicallyTINYINT(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
-
Even if I use
TINYINT(1) UNSIGNED
instead ofBOOLEAN
, 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. -
DoctrineKeyValueStyleRule
is not yet advanced enough to account for the manyDoctrine\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 eachDoctrine\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 theType
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.
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)
- mysqli: error on invalid escaping HOT 5
- Query AST analysis? HOT 2
- Get AST data from `$queryReflection->getResultType` HOT 15
- AST doubts regarding null/not-null HOT 4
- AST: inconsistent behavior with functions and aliases HOT 4
- Analyzing multiple connections HOT 6
- Right vs left join HOT 2
- AST: generic operator support HOT 2
- pgsql support for AST HOT 2
- give up sql based narrowing in case sqlftw cannot parse the query
- pgsql support for uuid type
- Support for (PDO->prepare())->execute() HOT 6
- generic type mysqli_result error with phpstan v1.10.36 and v1.10.37 HOT 2
- Support for custom API's for type inference HOT 1
- Check driver differences
- dibi support DATE_FORMAT
- Psalm support
- Using with doctrine/dbal HOT 8
- CI: Separate Testing of Doctrine 3.x and 4.x
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpstan-dba.