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

Deprecate type inference in Query::setParameter #8540

Closed
wants to merge 7 commits into from

Conversation

abame
Copy link

@abame abame commented Mar 14, 2021

Deprecated type inference for Query::setParameter mentioned in issue #8379

@@ -44,6 +44,9 @@ class ParameterTypeInferer
* - Type (\Doctrine\DBAL\Types\Type::*)
* - Connection (\Doctrine\DBAL\Connection::PARAM_*)
*
* @deprecated Infer Type will be removed in the future for setParameter method,
Copy link
Member

Choose a reason for hiding this comment

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

Will this not cause IDEs and static analyzers to mark all calls to this method as deprecated?

Copy link
Author

Choose a reason for hiding this comment

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

yes that's correct but isn't this the desired outcome of the opened issue or did I misunderstood the issue?

Copy link
Member

Choose a reason for hiding this comment

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

I think you misunderstood. IMO what's needed here is some usage of doctrine/deprecations in the cases that should be deprecated and only those.

Copy link
Author

Choose a reason for hiding this comment

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

My bad then. Does this mean, if I understood it correctly now, that we just need to trigger a deprecation warning for the third argument of setParameter method by using doctrine/deprecations?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I believe that's indeed what's needed 👍

Copy link
Author

Choose a reason for hiding this comment

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

thanks for the clarification, I'll update the PR later today

Copy link
Author

Choose a reason for hiding this comment

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

Do I have to update the existing tests or create a new one? How is this usually handled?

Copy link
Member

Choose a reason for hiding this comment

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

It looks like you had the former anyway, but it might be good to do the latter too, to demonstrate that it affects all classes extending AbstractQuery, and not just NativeQuery

Copy link
Author

Choose a reason for hiding this comment

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

I found only two classes that extend the AbstractQuery and the tests for both classes are updated. Can you think about any use-case for which we might need new tests for this?

Copy link
Member

Choose a reason for hiding this comment

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

Since AbstractQuery is neither final nor internal, anybody can extend it outside doctrine/orm if they want to, and they should get the deprecation too.

@@ -381,6 +381,14 @@ public function setParameters($parameters)
*/
public function setParameter($key, $value, $type = null)
{
if ($type === null) {
Copy link
Member

@greg0ire greg0ire Mar 17, 2021

Choose a reason for hiding this comment

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

We should assume every object passed must be an entity or specify a $type as third argument. Only primitive types should work with type inference.

I think the deprecation should fire only in some cases, but I'm not sure how to do that detection: @beberlei please help 🙏

Copy link
Author

Choose a reason for hiding this comment

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

Based on the example of Datetime I guess we should also check if the value is an object and type is null, than we trigger the deprecation, but let's wait and see if we can get some more input

@beberlei
Copy link
Member

sorry didn't see tis earlier, i looked over the code for a few times as well, its sadly quite complicated and i haven't found the perfect location either. I believe processParameterValue should be it, but its a bit difficult.

@abame
Copy link
Author

abame commented Mar 20, 2021

sorry didn't see tis earlier, i looked over the code for a few times as well, its sadly quite complicated and i haven't found the perfect location either. I believe processParameterValue should be it, but its a bit difficult.

@beberlei just for my understanding since from the issue description it says, Only primitive types should work with type inference, and based on this my question is that will it not be enough to check the following in setParameter method:

if(is_object($value) && $type === null) {
   //trigger deprecation notice
}

This way we let the user know that the third argument has to be provided for non-primitive values. What are the implications that prevent us from doing so? In the processParameterValue method we only know about the value and we don't know if the type argument is provided or not in the setParameter method

@beberlei
Copy link
Member

What I want to prevent here is #8113 for example, in AbstractQuery::processParameterValue the MappingException is swallowed. I would say we should put the deprecation message there: "Passing a parameter value of type object that is not an entity is deprecated, unless you also specify its Doctrine\DBAL\Types\Type name."

@abame abame closed this Jan 1, 2023
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.

3 participants