Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DoctrineKeyValueStyeRule: allow int values in float columns #581

Merged
merged 4 commits into from
Mar 20, 2023

Conversation

hemberger
Copy link
Contributor

Using $databaseType->isSuperTypeOf($valueType) to check if the input value type matches the database column type was too strict. It was incorrectly raising errors for various legal scenarios, e.g.:

  • Passing an integer to a float column.
  • Passing a benevolent (float|int) to an int column.

The accepts method correctly handles these cases. Add extra logic for MixedType, since mixed types are always accepted.

NOTE: The MixedType case may be demoted to a debug-mode-only error, depending on the answer to #564 (comment).

@staabm
Copy link
Owner

staabm commented Mar 19, 2023

Passing a benevolent (float|int) to an int column.

do you say a regular float|int works but a benevolent (float|int) does not?
I kind of can't believe it :)

@hemberger
Copy link
Contributor Author

do you say a regular float|int works but a benevolent (float|int) does not? I kind of can't believe it :)

Given an int column, float|int is an error (as it should be), but currently (float|int) is also an error, and I was thinking that perhaps it shouldn't be given this documentation about them:

Benevolent unions aren’t checked strictly even at the highest level

But if you say otherwise, I'll take your word for it! :)

@staabm
Copy link
Owner

staabm commented Mar 19, 2023

Benevolent unions aren’t checked strictly even at the highest level

i guess you took that sentence from the phpstan docs somewhere? If so: it describes how native phpstan rules behave according to analysis level.

atm the phpstan-dba rules do not at all depend on phpstan rule levels (which does not mean that our RuntimeConfig could take inspiration of the ideas behind phpstan-src rules or implement similar ideas).

i don‘t know how phpstan-dba rules work with benevolent unions atm. We would need unit tests to verify it

@hemberger hemberger changed the title DoctrineKeyValueStyeRule: allow accepted types DoctrineKeyValueStyeRule: allow int values in float columns Mar 19, 2023
@hemberger hemberger requested a review from staabm March 19, 2023 22:49
Comment on lines +131 to +137
/**
* @param array-key $value A benevolent union type (int|string)
*/
public function errorWithBenevolentUnionValue(Connection $conn, $value)
{
$conn->assembleOneArray('typemix', ['c_int' => $value]);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this expectation. I would expect a error for a non-benevolent union, but I think we should be ok with a benevolent union, as long as it contains the type we expect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to change the behavior for benevolent unions as well. Would you be okay with me submitting it as a separate PR?

Copy link
Owner

@staabm staabm Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

hemberger and others added 4 commits March 20, 2023 16:41
Using `$databaseType->isSuperTypeOf($valueType)` to check if the input
value type matches the database column type was too strict. It was
incorrectly raising errors for various legal scenarios, e.g.:

* Passing an integer to a float column.
* Passing a benevolent (float|int) to an int column.

The `accepts` method correctly handles these cases. Add extra logic for
`MixedType`, since mixed types are always accepted.
Handle the `FloatType` explicitly instead of testing with `accepts`,
since that function does more than what we want.

Note that this makes benevolent union types unacceptable again unless
all types in the union are acceptable.

I've also simplified the logic for casting an `IntegerRangeType` into
an `IntegerType`, since a column type can only reasonable have a union
with a `NullType`. There is a standard way to using TypeCombinator to
mutate the type of a union with null.
@staabm staabm force-pushed the doctrine-accepts branch from b4af7aa to 0bee888 Compare March 20, 2023 15:42
@staabm staabm enabled auto-merge (squash) March 20, 2023 15:43
@staabm
Copy link
Owner

staabm commented Mar 20, 2023

thank you

@staabm staabm merged commit 1540223 into staabm:main Mar 20, 2023
@hemberger hemberger deleted the doctrine-accepts branch March 20, 2023 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants