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

Allow use array and multi arguments #100

Closed
wants to merge 6 commits into from
Closed

Allow use array and multi arguments #100

wants to merge 6 commits into from

Conversation

vlastv
Copy link
Contributor

@vlastv vlastv commented Dec 13, 2016

No description provided.

@lcobucci
Copy link
Member

@vlastv please provide a test case as well.

BTW why do you need that? Can't you use ...$myArray on the function call?

@vlastv
Copy link
Contributor Author

vlastv commented Dec 13, 2016

Variable-length arguments implemented in PHP 5.6, library can work with PHP 5.5 based on composer.json.

I use in case:

$restrictions = [];
foreach($filters as $field => $value) {
    $restrictions[] = Criteria::expr()->eq($field, $value);
}

$criteria->andWhere(call_user_func_array([Criteria::expr(), 'andX'], $restrictions]));

IMHO, it variant is more readable:

// loop

$criteria->andWhere(Criteria::expr()->andX($restrictions)));

@Ocramius
Copy link
Member

@vlastv can be bumped to PHP 5.6 as minimum requirement

@vlastv
Copy link
Contributor Author

vlastv commented Dec 13, 2016

@Ocramius If you change the minimum version then this request can be rejected.

@Ocramius
Copy link
Member

@vlastv I'm suggesting to add it to this patch ;-)

@@ -42,7 +42,7 @@ class ExpressionBuilder
*/
public function andX($x = null)
{
return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
return new CompositeExpression(CompositeExpression::TYPE_AND, is_array($x) ? $x : func_get_args());
Copy link
Member

Choose a reason for hiding this comment

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

This should be changed to just accept $x, and the method signature should become ...$x

@@ -52,7 +52,7 @@ public function andX($x = null)
*/
public function orX($x = null)
{
return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
return new CompositeExpression(CompositeExpression::TYPE_OR, is_array($x) ? $x : func_get_args());
Copy link
Member

Choose a reason for hiding this comment

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

This should be changed to just accept $x, and the method signature should become ...$x

);
$this->assertEquals($x, $expr->getExpressionList());

$expr = $this->builder->andX($x);
Copy link
Member

Choose a reason for hiding this comment

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

This seems invalid to me

);
$this->assertEquals($x, $expr->getExpressionList());

$expr = $this->builder->orX($x);
Copy link
Member

Choose a reason for hiding this comment

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

This seems invalid to me

@@ -40,19 +40,19 @@ class ExpressionBuilder
*
* @return CompositeExpression
*/
public function andX($x = null)
public function andX(...$x)
Copy link
Member

Choose a reason for hiding this comment

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

@Ocramius should we use Expression ...$x as well (and drop the mixed)?

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with Expression ...$x, but it will break on HHVM ;-)

Copy link
Member

@lcobucci lcobucci Dec 14, 2016

Choose a reason for hiding this comment

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

HHVM!? pffff 😜

Copy link
Member

Choose a reason for hiding this comment

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

bad idea: we allow strings too, not only Expression objects (as Expression objects are just here to be casted in the end anyway). Typehinting as Expression would be a huge BC break.

Copy link
Member

Choose a reason for hiding this comment

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

@stof are you sure about strings?

public function __construct($type, array $expressions)
seems to say something different...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof strings not allowed here

Copy link
Member

Choose a reason for hiding this comment

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

sorry, I confused the Criteria system in doctrine/collection with the expression builder of the QueryBuilder. Forget my comment.

@stof
Copy link
Member

stof commented Dec 14, 2016

The current state of the PR does not change the API at all. It still does not allow to pass a single array instead of variadic arguments. So it has no benefit for people using Doctrine (it does not allow to do what the description says).

@vlastv if your own code runs on PHP 5.6+, you can avoid call_user_func_array today without any change in Doctrine:

// loop

$criteria->andWhere(Criteria::expr()->andX(...$restrictions)));

@stof
Copy link
Member

stof commented Dec 14, 2016

So the only thing achieved by your PR so far is breaking support for HHVM if you typehint, without any benefit for users of the API...

@vlastv
Copy link
Contributor Author

vlastv commented Dec 26, 2016

I close this PR?

@lcobucci
Copy link
Member

Now that we dropped HHVM support we can rebase this branch and be more strict on the typehint...

@Ocramius
Copy link
Member

This can only target 2.x, sadly. See https://3v4l.org/QIi9Y - it is a BC break due to the ExpressionBuilder being an open class.

@Ocramius Ocramius assigned Ocramius and unassigned Ocramius Mar 24, 2019
@Ocramius Ocramius added this to the 2.0 milestone Mar 24, 2019
@greg0ire greg0ire closed this Jan 19, 2021
@greg0ire greg0ire deleted the branch doctrine:master January 19, 2021 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants