-
-
Notifications
You must be signed in to change notification settings - Fork 844
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
Fix Dupplication code on Filters #529
Conversation
* | ||
* @return array | ||
*/ | ||
protected function getAssociation($property,$alias,&$queryBuilder) : array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary to specify pass by reference for $queryBuilder
. Objects are always passed by reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, yes, I was thinking that this one was not passed by reference when testing this, but it was another problem, ill remove that
1ed40ff
to
1e0f8d0
Compare
* | ||
* @return array | ||
*/ | ||
protected function getAssociation($property, $alias, $queryBuilder) : array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps addJoinsForNestedProperty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please use type hints for parameters.
IMO |
Wait for it... Traits 😉 |
😱 |
What can we do about that, using Traits? |
0f35626
to
7432fda
Compare
...
On a more serious note, this would be a utility for filters. As users are very likely to create custom filters and may need those utility methods, I think allowing them to a utility without any contract (which is what a trait is) would be a very bad idea. A good old class declared as a service do the job here. And performance are not an issue, adding a class to the DIC doesn't cause more performance slowness than copy/pasting code left and right. |
We should do that in this PR ? |
In another one IMO, it is a separate issue. |
I would prefer a trait. It's a perfect use case for a trait. Adding things like this to the DIC do slow down the app (but yes, for just 1 it's not signifiant). |
It's not, you have to add over 1000 services to the Symfony DIC to see 1-2ms difference. It would only be a lazy shortcut to avoid declaring a service to the DIC and introduce an implicit contract, which is always harder to manage than an external one. |
Besides as you have to inject dependencies via a the constructor. Making it a trait is worse than having an abstract class at every level, even in terms of DX. |
7432fda
to
6c9a895
Compare
@@ -173,4 +175,28 @@ protected function extractProperties(Request $request) : array | |||
|
|||
return $request->query->all(); | |||
} | |||
|
|||
/** | |||
* @param array property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// use ApiPlatform\Core\Exception\InvalidArgumentException;
/**
* Adds the necessary joins for a nested property.
*
* @param string $property
* @param string $rootAlias
* @param QueryBuilder $queryBuilder
*
* @throws InvalidArgumentException If property is not nested
*
* @return array An array where the first element is the join $alias of the leaf entity,
* and the second element is the $field name
*/
protected function addJoinsForNestedProperty(string $property, string $rootAlias, QueryBuilder $queryBuilder) : array
{
$propertyParts = $this->splitPropertyParts($property);
if (0 === count($propertyParts['associations'])) {
throw new InvalidArgumentException(sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
}
$parentAlias = $rootAlias;
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to throw an exception here, is the exception for the people who want to use this function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to me looks like an abnormality, hence an exception to raise it. If the developer wants to ignore it, he can always catch that error, but if you ignore it, he has no means to know about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our use cases the exception would not be thrown, in case the user want to use the methods without a nested property he will have to catch the error or/and to test with $this->isPropertyNested
so it's ok to add the exception for me.
6c9a895
to
c4f69c0
Compare
👍 @Simperfit do you consider this PR ready to be merged? |
c4f69c0
to
6961233
Compare
@dunglas Yes, we will fix other scrutiner issues on equivalent PRs |
Thank you @Simperfit |
…sues Fix Dupplication code on Filters
Since the last commit with both boolean and numeric filters, scrutinizer does not like duplicated code.
And a foreach that scrutinizer does not like.