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

Fix Dupplication code on Filters #529

Merged
merged 1 commit into from
May 7, 2016

Conversation

Simperfit
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets no
License MIT
Doc PR no

Since the last commit with both boolean and numeric filters, scrutinizer does not like duplicated code.

And a foreach that scrutinizer does not like.

*
* @return array
*/
protected function getAssociation($property,$alias,&$queryBuilder) : array
Copy link
Contributor

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.

Copy link
Contributor Author

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

@Simperfit Simperfit force-pushed the fix-scrutinizer-issues branch 2 times, most recently from 1ed40ff to 1e0f8d0 Compare May 5, 2016 14:21
*
* @return array
*/
protected function getAssociation($property, $alias, $queryBuilder) : array
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps addJoinsForNestedProperty

Copy link
Contributor

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.

@theofidry
Copy link
Contributor

IMO AbstractFilter is already too big and I don't see the point of having it in an abstract class. It should be separated as a doctrine filter utility instead...

@teohhanhui
Copy link
Contributor

IMO AbstractFilter is already too big and I don't see the point of having it in an abstract class. It should be separated as a doctrine filter utility instead...

Wait for it... Traits 😉

@theofidry
Copy link
Contributor

😱

@Simperfit
Copy link
Contributor Author

What can we do about that, using Traits?

@Simperfit Simperfit force-pushed the fix-scrutinizer-issues branch 2 times, most recently from 0f35626 to 7432fda Compare May 5, 2016 14:57
@theofidry
Copy link
Contributor

What can we do about that, using Traits?

...

using Traits?

kill

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.

@Simperfit
Copy link
Contributor Author

We should do that in this PR ?

@theofidry
Copy link
Contributor

theofidry commented May 5, 2016

In another one IMO, it is a separate issue.

@dunglas
Copy link
Member

dunglas commented May 5, 2016

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).

@theofidry
Copy link
Contributor

theofidry commented May 5, 2016

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.

@theofidry
Copy link
Contributor

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.

@@ -173,4 +175,28 @@ protected function extractProperties(Request $request) : array

return $request->query->all();
}

/**
* @param array property
Copy link
Contributor

@teohhanhui teohhanhui May 6, 2016

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;

    ...
}

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 don't think we need to throw an exception here, is the exception for the people who want to use this function ?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@dunglas
Copy link
Member

dunglas commented May 7, 2016

👍

@Simperfit do you consider this PR ready to be merged?

@Simperfit
Copy link
Contributor Author

@dunglas Yes, we will fix other scrutiner issues on equivalent PRs

@dunglas dunglas merged commit e434720 into api-platform:master May 7, 2016
@dunglas
Copy link
Member

dunglas commented May 7, 2016

Thank you @Simperfit

@Simperfit Simperfit deleted the fix-scrutinizer-issues branch July 14, 2016 15:19
teohhanhui added a commit that referenced this pull request Oct 24, 2016
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
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.

None yet

4 participants